当客户端浏览器中禁止 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.

本文详细介绍了JavaServletAPI中通过Session机制追踪客户状态的方法,包括如何在客户端浏览器不支持Cookie时,通过重写URL将SessionID加入到URL中。文章还解释了使用HttpServletResponse接口中的encodeURL方法实现这一过程。
5832

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



