• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1262
  • Last Modified:

Combo boxes & request.getParameterValues

I have written a JSP page which has a combo box with multiple select options. The combo box's name parameter is "sel_courses".
I retrieve the information selected the combo box using the following code in a servlet.

String[] sel_crs=request.getParameterValues("sel_courses");
for(int i=0;i<sel_crs.length;i++){
out.println("sel_crs"+sel_crs[i]);
}

Actually it should print the course list with the selected courses in the combo boxes.
But it doesn't print anything.
Can someone tell me what is the problem?

Thanks
0
icbwubr
Asked:
icbwubr
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
ManishLeadCommented:
Ru getting other values of JSP field?
Check output in SOP..
0
 
ManishLeadCommented:
Can u paste ur JSP code..?
0
 
suprapto45Commented:
Hi,

As what karanw said....can you post your code please? Is the "sel_courses" within the form tag?

Regards
Dave
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
koppchaCommented:
Please check this
1>If your combo or drop down box is with in the form
2>Name specified for the combo box is same you are using in request.getParameterValues("sel_courses");
3>First of all check if you can see other form field if they are any.
4>Post your form that includes the combo Box
0
 
patbinCommented:
As what Karanw said cun u paste ur code
0
 
icbwubrAuthor Commented:
This is the servlet code where I am retrieving the information (i.e the courses the user has selected):

package c4125_proj;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import java.util.*;
import javax.servlet.http.*;
import c4125_proj.beans.*;

public class AcadInfoServlet extends HttpServlet implements ServletContextListener {
   
 
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

String action=request.getParameter("action");
    if(action==null || action.equals("")){
       this.request =request;
        this.response=response;
        response.setContentType("text/html");
        out = response.getWriter();
        session=request.getSession(false);
        db = new DbConnection("cook","1521", "dbrs","dvijaymy","q0h5fvy");
        db.getConnection();
      System.out.println("test");
        dispatcher = request.getRequestDispatcher("/courseInfoTestor.jsp");
      System.out.println("dispatcher is" + dispatcher);
      String option = "checklist";//request.getParameter("action");
          System.out.println("option is: "+option);
            executeOption(option);
                }
    else if(action.equals("courseInfo")){
      
       String[] sel_crs = request.getParameterValues("sel_courses");
        for(int i=0;i<sel_crs.length;i++){
            out.println("sel_crs"+sel_crs.length);
            }
        
       }
    }




       
      
        /*if(session==null){
            //int accountNumber = Integer.parseInt(request.getParameter("accountNum"));
            //nt ssNumber = Integer.parseInt(request.getParameter("SSN"));
            //int option=Integer.parseInt(request.getParameter("transaction"));
                  String firstname = request.getParameter("fname");
                  String lastname = request.getparameter("lname");
                  int sid = Integer.parseInt(request.getParameter("stuid"));
            boolean valid=checkValid(firstname,lastname,sid);
           
            if(valid){
                session=request.getSession();
                session.setAttribute("f_name",new Integer(firstname));
                session.setAttribute("l_name",new Integer(lastname));
                session.setAttribute("stu_id",new Integer(sid));
                out.println("<BR>"+"<BR>"+"Select what u wanna do?");
               
                out.println("<BR>"+"<BR>"+"<input type=submit>"+"<input type=reset>");

                executeOption(option,firstname,lastname);
            }
            else{
                out.println("<html><body>");
                out.println("<BR><BR>"+"Login Incorrect");  
                out.println("<BR>"+s_oraURL+" "+s_userName+" pass "+s_password);
                out.println("<BR><a href=\"./LoginServlet\">");
                out.println("<img SRC=\"../images/return.gif\" height=24 width=24></a>");
                out.println("Back to Main Page");
                out.println("</body></html>");
            }
        }
        else{
            //int option=Integer.parseInt(request.getParameter("page"));*/

   
   
      
      public void contextInitialized(ServletContextEvent ce) {
            ServletContext context=ce.getServletContext();
                s_oraURL=context.getInitParameter("s_oraURL");
                s_userName = context.getInitParameter("s_userName");
                s_password=context.getInitParameter("s_password");
      }

      public void contextDestroyed(ServletContextEvent ce) {}

   
   /* private boolean checkValid(int accountNumber, int ssNumber){
       
        boolean valid = db.checkValidAccount(,);
       return valid;
       
    }*/

 

   

    private void executeOption(String option) throws ServletException,IOException{
       



        if(option.equals("checklist")) {
            Vector vec = db.course_info();
            CourseInfoBean crsbean = new CourseInfoBean();
            crsbean.setCourseInfo(vec);
            request.setAttribute("resultBean", crsbean);
            dispatcher.forward(request, response);
            /*out.println("<html><body>");
            out.println("<form action=./WithdrawDepositServlet method=POST>");
            out.println("<BR> Your Current Balance: "+db.getBalance(accountNumber));
            out.println("<BR>"+"<BR> Enter Amount"+"<input type=text name=amount>");
            out.println("<BR>"+"<BR>"+"<input type=submit>"+"<input type=reset>");
            out.println( "<input type=hidden name=userOption value="+option+">" );
            out.println("</form>");
            out.println("</body></html>");*/
        }
       
     
   
   
       }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

        private static String s_oraURL;
      private static String s_userName;
      private static String s_password;
        private HttpSession session;
        private DbConnection db;
        private PrintWriter out;
         private RequestDispatcher dispatcher;
        private HttpServletRequest request;
        private HttpServletResponse response;
     
    }

This the class where I have written the code for the combo box:
/*
 * AccountActivityTag.java
 *
 *
 */


package c4125_proj.tags;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.util.*;
import c4125_proj.beans.*;
/**
 *
 *
 */
public class CourseInfoTag extends TagSupport {
      public int doStartTag(){
         try{
            CourseInfoBean crsBean = (CourseInfoBean) pageContext.getRequest().getAttribute(m_beanName);
            JspWriter out = pageContext.getOut();
            if(crsBean != null){
                    Vector vec=crsBean.getCourseInfo();
                    out.println("<html><body>");
                    //out.println("<BR><BR>  Courses Table");
                    out.println("<table border=1>");
                   
                    //out.println("<tr>");
                    //out.println("<td>"+"Course Number"+"</td>");
                    //out.println("<td>"+"Course Name"+"</td>");
                    //out.println("</tr>");
                //out.println("</table>");
                    //out.println("</tr>");
                if (vec.size() == 2){
                  //out.println("<tr>");
                  out.println("<td>"+vec.get(0)+"</td>");
                         out.println("<td>"+vec.get(1)+"</td>");
                }
                else{
                int i;
                int j;
                String actionName = "courseInfo";
               
                out.println("<form action=./c4125_proj.AcadInfoServlet>");
                out.println("Courselist");
                out.println("<select multiple name=sel_courses value=value>");
                    for( j=1,i=0;  j<vec.size();j=j+2,i=i+2){
                        //out.println("<tr>");
                  String courseinfo = (vec.get(i) +" "+ vec.get(j));
                  out.println("<option value=courseinfo>"+courseinfo+"</option>");
                      // out.println("<td>"+vec.get(i)+"</td>");
                      //out.println("<td>"+vec.get(j)+"</td>");
                      //out.println("</tr>");
                }
                }
                out.println("</select>");
                    out.println("<input type=hidden name=action value=courseInfo>");
                out.println("<BR>"+"<BR>"+"<input type=submit>");
             
                //out.println("<BR><a href=\"./AcadInfoServlet\">");
                //out.println("<img SRC=\"../images/return.gif\" height=24 width=24></a>");
                //out.println("Back to Main Page");
             
          out.println("</form>");
            out.println("</body></html>");
                }
               
            else
                 out.println("Bean " + m_beanName + " is null in CourseInfoBeanTag");
         }catch(IOException ioe) {
            pageContext.getServletContext().log(
                  "Error with the bean");}
            return SKIP_BODY;
        }
      // Bean name. The JSP page is expected to set this.
      public void setBeanName(String beanName)
      {
            m_beanName = beanName;
      }
      private String m_beanName;
   
}

0
 
koppchaCommented:
change your out.println statements
out.println("<option value=" + courseinfo + ">"+courseinfo+"</option>");

0
 
patbinCommented:
I think u have got problem over here

else if(action.equals("courseInfo")){
     
      String[] sel_crs = request.getParameterValues("sel_courses");
       for(int i=0;i<sel_crs.length;i++){
           out.println("sel_crs"+sel_crs.length);/// error it should be             out.println("sel_crs"+sel_crs[i]);
           }
       
       }
    }

still if it dosent work try this one

out.println("<select multiple name='sel_courses' value='value'>");
                    for( j=1,i=0;  j<vec.size();j=j+2,i=i+2){
                        //out.println("<tr>");
               String courseinfo = (vec.get(i) +" "+ vec.get(j));
               out.println("<option value='courseinfo'>"+courseinfo+"</option>");
                   // out.println("<td>"+vec.get(i)+"</td>");
                   //out.println("<td>"+vec.get(j)+"</td>");
                   //out.println("</tr>");
             
0
 
patbinCommented:
thanks for grade
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now