当客户端浏览器中禁止 Cookie,Servlet 容器无法从客户端浏览器中取得作为 Cookie 的 Session ID,也就无法跟踪客户状态。
Java Servlet API 中提出了跟踪 Session 的另一种机制,如果客户端浏览器不支持 Cookie,Servlet 容器可以重写客户请求的 URL,把 Session ID 添加到 URL 信息中。
HttpServletResponse 接口提供了重写 URL 的方法:public java.lang.String encodeURL(java.lang.String url)
该方法的实现机制为:
● 先判断当前的 Web 组件是否启用 Session,如果没有启用 Session,直接返回参数 url。
● 再判断客户端浏览器是否支持 Cookie,如果支持 Cookie,直接返回参数 url;如果不支持 Cookie,就在参数 url 中加入 Session ID 信息,然后返回修改后的 url。
我们可以对网页中的链接稍作修改,解决以上问题:
修改前:
<a href=“maillogin.jsp“>
修改后:
<a href=“<%=response.encodeURL(“maillogin.jsp“)%>“>
Which method is used by a servlet to place its session ID in a URL that is written to the servlet’s response output stream?
(译:那个方法是 servlet 用于将其 session ID 入在一个 URL 中,该 URL 写入 servlet 的响应输出流)
A. The encodeURL method of the HttpServletRequest interface.
B. The encodeURL method of the HttpServletResponse interface.
C. The rewriteURL method of the HttpServletRequest interface.
D. The rewriteURL method of the HttpServletResponse interface.
本文介绍Java Servlet API中的Session机制,详细解释了Servlet容器如何通过Session ID追踪客户端状态,包括使用Cookie和URL重写两种方式。
5826

被折叠的 条评论
为什么被折叠?



