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进来。



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值