SELECT标记值没有进入Servlet

时间:2013-03-22 11:15:24

标签: jsp servlets

我的JSP页面中有表单字段。它具有少量值的Select标签。我只需要选择一个值,并根据我的servlet中的值转发到其他页面。 这是我选择标记的JSP代码

<select name="options">
    <option>Choose Your Option</option>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
    <option value="option4">Option 4</option>
</select>

在我的servlet中,我正在根据所选值进行处理。这是我的servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
String options[]=request.getParameterValues("options");
        if(options[0].equals("option1"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option1.jsp");
            rd.forward(request, response);
        }
        else if(options[0].equals("option2"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option2.jsp");
            rd.forward(request, response);
        }
        else if(options[0].equals("option3"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option3.jsp");
            rd.forward(request, response);
        }
        else if(options[0].equals("option4"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option4.jsp");
            rd.forward(request, response);
        }
    }

真正的问题是,当我选择选项1或选项2时,它将转发到相应的页面。但是,如果我选择选项3或选项4,它将无法正常工作。问题是什么?我该怎么办?感谢

2 个答案:

答案 0 :(得分:1)

String option = request.getParameter("options")

这将给出选定的值

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
       String selectedValue=request.getParameterValues("options");
        if(selectedValue.equals("option1"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option1.jsp");
            rd.forward(request, response);
        }
        else if(selectedValue.equals("option2"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option2.jsp");
            rd.forward(request, response);
        }
        else if(selectedValue.equals("option3"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option3.jsp");
            rd.forward(request, response);
        }
        else if(selectedValue.equals("option4"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option4.jsp");
            rd.forward(request, response);
        }
    }

答案 1 :(得分:0)

你只能选择一个选项,所以真正的anser是:

String selectedValue=req.getParameter("genre");
if(option.equals("option1"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option1.jsp");
            rd.forward(request, response);
        }
        else if(option.equals("option2"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option2.jsp");
            rd.forward(request, response);
        }
        else if(option.equals("option3"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option3.jsp");
            rd.forward(request, response);
        }
        else if(option.equals("option4"))
        {
            RequestDispatcher rd=request.getRequestDispatcher("Option4.jsp");
            rd.forward(request, response);
        }
    }