深刻了解Servlet中的Cookie

1.聲明

本文屬於我的理解,若是以爲理解有誤的話還請各位指出。html

2.問題描述

cookie是什麼?servlet中cookie是什麼?servlet中如何設置cookie?何時會設置cookie?爲何第一次訪問jsp頁面會設置一個cookie?爲何HttpServletRquest.getSession();會設置一個JSESSIONID的cookie?java

3.什麼是cookie?

cookie 嚴格來說是http cookie。cookie是http狀態管理的原理。rfc6365規範nginx

4.servlet中cookie是什麼?servlet如何設置cookie?

在Servlet中javax.servlet.http.Cookie就是http cookie的實現。git

servlet中如何設置cookie?github

參考:api

A simple cookie example in servlet——mkyong瀏覽器

5.何時會設置cookie?

通常會在訪問網站的首頁即域名指向的頁面,如訪問www.oschina.net,會設置以下cookietomcat

Cache-Control:no-cache
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html;charset=UTF-8
Date:Sat, 29 Oct 2016 07:15:49 GMT
Pragma:no-cache
Server:Qnginx/1.2.0
Set-Cookie:_user_behavior_=d4d09369-debd-459e-be95-5eb7acb38477; Domain=.oschina.net; Expires=Sun, 29-Oct-2017 07:15:49 GMT; Path=/; HttpOnly
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-NWS-LOG-UUID:1877b550-129c-406d-8252-83bf00fb05c8

也有可能在登入成功後設置一些cookie,oschina登入成功後會添加 oscid的cookiecookie

6.爲何第一次訪問jsp頁面會設置一個cookie?

這裏所說的第一次是指當客戶端瀏覽器與服務端尚未創建回話或者回話超時。若是jsp沒有顯示的使用<%@page Session=」false」%>關閉session的話,則jsp頁面默認都是開啓Session的,也就是jsp存在session內置對象的緣由。session

參考:

也能夠到tomcat下看下jsp生成的對應的Servlet,關鍵代碼以下

  • 設置<%@page Session=」true」%>或者不設值
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);//第五個參數true啓用session
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();//這個其實調用的就是HttpServletRquest.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\n");
      out.write("\n");
      out.write("<html>\n");
      out.write("<head>\n");
      out.write("    <title>$Title$</title>\n");
      out.write("</head>\n");
      out.write("<body>\n");
      out.write("$END$\n");
      out.write("</body>\n");
      out.write("</html>\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
  • 設置<%@page Session=」false」%>
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}

    final javax.servlet.jsp.PageContext pageContext;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, false, 8192, true);//第五個參數false不啓用session
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
       //不會生成session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\n");
      out.write("\n");
      out.write("<\n");
      out.write("<html>\n");
      out.write("<head>\n");
      out.write("<title>$Title$</title>\n");
      out.write("</head>\n");
      out.write("<body>$END$\n");
      out.write("</body>\n");
      out.write("</html>\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }

7.爲何HttpServletRquest.getSession();會設置一個JSESSIONID的cookie?

相關文章
相關標籤/搜索