servlet redirect/forward/session/cookie 與 HTTP 協議的測試

 本文直接展示servlet的測試結果。

 

servlet的代碼中使用sendRedirect:

  

public class SessionTest extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
...
response.sendRedirect("http://localhost:8080/web/");
...}

 

 

查看瀏覽器交互過程,先是請求該servlet:

 

  1. Request URL:
    http://localhost:8080/web/SessionTest
  2. Request Method:
    GET
  3. Status Code:
    302 Found
  4. Request Headersview source
    1. Accept:
      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
    4. Cache-Control:
      max-age=0
    5. Connection:
      keep-alive
    6. Host:
      localhost:8080
    7. User-Agent:
      Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
  5. Response Headersview source
    1. Content-Length:
      0
    2. Content-Type:
      text/html;charset=UTF-8
    3. Date:
      Thu, 06 Jun 2013 07:16:55 GMT
    4. Location:
      http://localhost:8080/web/
    5. Server:
      Apache-Coyote/1.1
    6. Set-Cookie:
      JSESSIONID=8C8DCD7355CC62C75C9BD154B2ADEDD0; Path=/web/; HttpOnly

可以看到response返回了302,並帶上了location, 指示瀏覽器做第二次跳轉請求,瀏覽器得到提示後進行第二次請求。

 

同樣的servlet,將代碼改爲forward,實現了在servlet容器內部的跳轉,對瀏覽器實際上是不可見得。

 

request.getRequestDispatcher("/index.jsp").forward(request, response);

 

看一下tomcat服務器的響應,一個200  OK之後,直接返回了內容,很直接

  1. Request URL:
    http://localhost:8080/web/SessionTest
  2. Request Method:
    GET
  3. Status Code:
    200 OK
  4. Request Headersview source
    1. Accept:
      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
    4. Connection:
      keep-alive
    5. Cookie:
      JSESSIONID=4D4C34D45462707E53A475C060E2D2A2; test=test
    6. DNT:
      1
    7. Host:
      localhost:8080
    8. User-Agent:
      Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
  5. Response Headersview source
    1. Content-Length:
      218
    2. Content-Type:
      text/html;charset=UTF-8
    3. Date:
      Thu, 06 Jun 2013 09:12:04 GMT
    4. Server:
      Apache-Coyote/1.1
    5. Set-Cookie:
      JSESSIONID=0FE64A4D2D1777BD44F08F2F8186384B; Path=/web/; HttpOnly

 

再看cookie的保存,將以上代碼改爲將cookie加入response:

 

        Cookie cookie = new Cookie("test","test");
        response.addCookie(cookie);
        PrintWriter out = response.getWriter();

 

 

再看HTTP head:

 

 

  1. Request URL:
    http://localhost:8080/web/SessionTest
  2. Request Method:
    GET
  3. Status Code:
    200 OK
  4. Request Headersview source
    1. Accept:
      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
    4. Cache-Control:
      max-age=0
    5. Connection:
      keep-alive
    6. Cookie:
      JSESSIONID=FB43328EF48C78B7122454F84B57F164
    7. DNT:
      1
    8. Host:
      localhost:8080
    9. User-Agent:
      Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
  5. Response Headersview source
    1. Content-Length:
      186
    2. Content-Type:
      text/html;charset=UTF-8
    3. Date:
      Thu, 06 Jun 2013 07:47:13 GMT
    4. Server:
      Apache-Coyote/1.1
    5. Set-Cookie:
      JSESSIONID=BC3FDA2D942F2F4B50757D9C436D21A9; Path=/web/; HttpOnly
    6. Set-Cookie:
      test=test

Tomcat通過HTTP協議的Set-Cookie將servlet的cookie放入了響應頭,查看瀏覽器的cookie,除了jsessionid,又加了一個test/test的cookie進來。



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章