-1

我的 JSP 页面中有表单字段。它的 Select 标记值很少。我只需要选择一个值,然后根据我的 servlet 中的值,我将转发到其他页面。这是我的 Select 标签的 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,它将不起作用。问题是什么?我该怎么办?谢谢

4

2 回答 2

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);
        }
    }
于 2013-03-22T11:27:33.107 回答
0

您只能选择一个选项,因此真正的分析器是:

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);
        }
    }
于 2015-10-10T17:39:48.297 回答