简体   繁体   中英

Get Cookies in java servlet

I Have Problem when I create cookies and store to another page using servlet. I have no create JSESSIONID, I Just Create name, but Why JSESSIONID always create automatic when I refresh the page Please Help me

enter image description here

    public class ControllerHelper extends HelperBase {
private static final long serialVersionUID = 1L;

public ControllerHelper(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        super(request, response);
    }


public void doGet(String address) throws ServletException, IOException {
    request.getSession().setAttribute("helper", this);
    PrintWriter out = response.getWriter();
    Cookie[] ck=request.getCookies();
    response.setContentType("text/html");
    if(ck!=null) {

        String name=ck[1].getValue();
          if(!name.equals("")||name!=null){  
              out.print("<b>Welcome to Profile</b>");  
              out.print("<br>Welcome, "+name);  
          }  

        request.setAttribute("stts_log", "Login");
        request.setAttribute("sess", name);
        request.setAttribute("btn_modal", "exambutton");
        request.setAttribute("btn_href", "#");

    }else {
        request.setAttribute("sess", "");
        request.setAttribute("stts_log", "Logout");
        request.setAttribute("btn_modal", "");
        request.setAttribute("btn_href", "logout");
    }

    request.getRequestDispatcher("/view/header.jsp").include(request, response);
    request.getRequestDispatcher("/view/"+address).include(request, response);
    request.getRequestDispatcher("/view/footer.jsp").include(request,response);
}

Why JSESSIONID always create automatic when I refresh the page

Because of request.getSession().setAttribute("helper", this); that creates your sessionid (the JSESSIONID is a session tracking cookie).

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