当客户端浏览器中禁止 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.
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。


