将请求参数发送到相同的页面和相同的servlet

问题描述 投票:0回答:1

所以,我试图从下拉列表中获取值并将它们添加到ArrayList中,以便以后可以显示它,但是我不知道该怎么做?

HTML(form.html)

<html>
    <head>
        <title>Form</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="fruits" method="get">
            <table>
                <tr>
                    <td>select fruit(s)</td>
                    <td> 
                        <select name="Values">
                            <option value="a">APPLE</option>
                            <option value="b">BANNANA</option>
                            <option value="o">ORANGE</option>
                        </select>
                    </td>
                </tr>
            </table>
            <input type="submit" value="next" name="send">
            <input type="submit" value="done" name="list">
        </form>    
    </body>
</html>

基本上是当用户按下next(发送)按钮时,该servlet应该采用values参数,并将其存储到arraylist中供以后使用,并且应将它们定向回表单页面,以便他们可以进行更多选择或通过按一下done(list)按钮查看他们选择了哪些选项

Servlet(水果Servlet)

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");

            String wasSent = request.getParameter("send");
            String Submitted = request.getParameter("list");
            ArrayList<Double> fruitsList = new ArrayList<Double>();


            if (wasSent != null) {
                String fruits = request.getParameter("Values");
                fruitsList.add(fruits);
                request.setAttribute("List", fruitsList);
                RequestDispatcher rs = request.getRequestDispatcher("form.html");
                rs.forward(request, response);

            } else if (Submitted != null) {

                //get the list and display all the values
            }

        }
java servlets
1个回答
0
投票

在您的servlet中,您可以使用session设置数组中的值,这样,该值将被保存,直到您从会话中将其删除为止。您的代码如下所示:

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");

           String fruits = request.getParameter("Values");
            ArrayList<String> fruitsList1 = new ArrayList<String>();
           //get values which are in session
            ArrayList<String> fruitsList = (ArrayList<String>)request.getSession().getAttribute("List");

              //if attribute value is not null 
            if (fruitsList != null) {
               //add new values
                fruitsList.add(fruits);
                request.getSession().setAttribute("List", fruitsList);
                RequestDispatcher rs = request.getRequestDispatcher("form.html");
                rs.forward(request, response);

            } else if (fruitsList== null) {
               //adding value selected in array list
                fruitsList1.add(fruits);
                 //setting values
                request.getSession().setAttribute("List", fruitsList1);
                RequestDispatcher rs = request.getRequestDispatcher("form.html");
                rs.forward(request, response);
            } else{

             //remove values 
         }

        }
© www.soinside.com 2019 - 2024. All rights reserved.