2

我正在阅读一本关于 Servlet 的书来复习,它指出它将自动决定何时附加会话 ID。我在网上阅读了相互矛盾的陈述。当 cookie 被阻止时,它将在多大程度上自动将会话 ID 附加到 URL,这是否包括使用 sendRedirect()。我指的是最新版本。我在 Oracle.com 上查看了 The JAVA Docs,但我不确定它们最近是否更新得足够。

谢谢 :-)

4

1 回答 1

1

不,容器没有智能来检测您写入输出流的模板文本或字符串中的链接。

对于 Ex,请使用以下示例代码:

servletoutputStream.write("<form method=\"post\" action=\"/submit.jsp\");

在上面的代码中,您必须HttpServletResponse.encodeURL("/submit.jsp")在 post action url 中有 session id。

同样,API 文档 HttpServletResponse.encodeRedirectURL()

java.lang.String encodeRedirectURL(java.lang.String url)

   Encodes the specified URL for use in the sendRedirect method or, if encoding 
     is not needed, returns the URL unchanged. The implementation of this method 
     includes the logic to determine whether the session ID needs to be encoded in 
     the URL.Because the rules for making this determination can differ from those 
     used to decide whether to encode a normal link, this method is separated from 
     the encodeURL method.

   All URLs sent to the HttpServletResponse.sendRedirect method should be run 
   through this method. Otherwise, URL rewriting cannot be used with browsers 
   which do not support cookies. 

参考:

于 2012-07-04T05:19:28.840 回答