简体   繁体   中英

How to pass an arraylist from one servlet to another servlet

I have two Servlets, In first servlet I am creating an arraylist and paasing it to jsp page to display on UI, now I want to pass the same arraylist from the first servlet to another servlet. Below is the code forArraylist of first serlet. How to pass it to anather servlet in post method

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String startName=request.getParameter("startName");
    int noOfSenders=Integer.parseInt(request.getParameter("noOfSenders"));
    String domain =request.getParameter("domain");

    /*Code for List form function*/
    List<String> mails=new ArrayList<String>();

            for(int i=1;i<=noOfSenders;i++){
                StringBuilder sb = new StringBuilder(); 
                sb.append(startName).append(i).append(domain); 
                mails.add(sb.toString());
            }
            System.out.println("response");
            response.getWriter().write(new Gson().toJson(mails));
                        }

You can use setAttribute()

request.setAttribute("mails", mails);

to retrieve :

ArrayList<String> list=(ArrayList<List>) request.getAttribute("mails"); 

set it as session attribute you can access it anywhere inside application like below.

request.getSession().setAttribute("name",mails);

and you can access it in second servlet as

ArrayList<String> list=(ArrayList<List>)request.getSession().getAttribute("name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM