本文直接展示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:
- Request URL:
http://localhost:8080/web/SessionTest
- Request Method:
GET
- Status Code:
302 Found
- Request Headersview source
- Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding:
gzip,deflate,sdch
- Accept-Language:
en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
- Cache-Control:
max-age=0
- Connection:
keep-alive
- Host:
localhost:8080
- User-Agent:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
- Accept:
- Response Headersview source
- Content-Length:
0
- Content-Type:
text/html;charset=UTF-8
- Date:
Thu, 06 Jun 2013 07:16:55 GMT
- Location:
http://localhost:8080/web/
- Server:
Apache-Coyote/1.1
- Set-Cookie:
JSESSIONID=8C8DCD7355CC62C75C9BD154B2ADEDD0; Path=/web/; HttpOnly
- Content-Length:
可以看到response返回了302,并带上了location, 指示浏览器做第二次跳转请求,浏览器得到提示后进行第二次请求。
同样的servlet,将代码改为forward,实现了在servlet容器内部的跳转,对浏览器实际上是不可见得。
request.getRequestDispatcher("/index.jsp").forward(request, response);
看一下tomcat服务器的响应,一个200 OK之后,直接返回了内容,很直接
- Request URL:
http://localhost:8080/web/SessionTest
- Request Method:
GET
- Status Code:
200 OK
- Request Headersview source
- Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding:
gzip,deflate,sdch
- Accept-Language:
en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
- Connection:
keep-alive
- Cookie:
JSESSIONID=4D4C34D45462707E53A475C060E2D2A2; test=test
- DNT:
1
- Host:
localhost:8080
- User-Agent:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
- Accept:
- Response Headersview source
- Content-Length:
218
- Content-Type:
text/html;charset=UTF-8
- Date:
Thu, 06 Jun 2013 09:12:04 GMT
- Server:
Apache-Coyote/1.1
- Set-Cookie:
JSESSIONID=0FE64A4D2D1777BD44F08F2F8186384B; Path=/web/; HttpOnly
- Content-Length:
再看cookie的保存,将以上代码改为将cookie加入response:
Cookie cookie = new Cookie("test","test"); response.addCookie(cookie); PrintWriter out = response.getWriter();
再看HTTP head:
- Request URL:
http://localhost:8080/web/SessionTest
- Request Method:
GET
- Status Code:
200 OK
- Request Headersview source
- Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Encoding:
gzip,deflate,sdch
- Accept-Language:
en-US,en;q=0.8,zh;q=0.6,ko;q=0.4,ja;q=0.2
- Cache-Control:
max-age=0
- Connection:
keep-alive
- Cookie:
JSESSIONID=FB43328EF48C78B7122454F84B57F164
- DNT:
1
- Host:
localhost:8080
- User-Agent:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
- Accept:
- Response Headersview source
- Content-Length:
186
- Content-Type:
text/html;charset=UTF-8
- Date:
Thu, 06 Jun 2013 07:47:13 GMT
- Server:
Apache-Coyote/1.1
- Set-Cookie:
JSESSIONID=BC3FDA2D942F2F4B50757D9C436D21A9; Path=/web/; HttpOnly
- Set-Cookie:
test=test
- Content-Length:
Tomcat通过HTTP协议的Set-Cookie将servlet的cookie放入了响应头,查看浏览器的cookie,除了jsessionid,又加了一个test/test的cookie进来。