简体   繁体   中英

Can I access a cookie created by a servlet in a html page?

I am trying to create a booking form that needs the User ID in all the pages. I wanted to know how to handle session when I redirect the pages as [HTML->servlet->HTML]

You can used HttpSession.setAttribute & HttpSession.getAttribute for eg:

JSP Page :

       //getting id value 
        String id= request.getParameter("user_id");
        //the variable which is set in session scope you can use anywhere 
         request.getSession().setAttribute("id",id);
          request.getRequestDispatcher("/yourServleturl").forward(request,response);

Now to get that session attribute write like below in servlet :

   HttpSession session=request.getSession();
      //getting value in session
    String id=session.getAttribute("id").toString();
    //do further processing

Hope this helps !

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