欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet redirect/forward/session/cookie 与 HTTP 协议的测试

程序员文章站 2022-07-02 12:57:27
...

 本文直接展示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:
    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进来。


servlet redirect/forward/session/cookie 与 HTTP 协议的测试
            
    
    博客分类: HTTP sessionforwardredirect 
 

  • servlet redirect/forward/session/cookie 与 HTTP 协议的测试
            
    
    博客分类: HTTP sessionforwardredirect 
  • 大小: 39.7 KB