我正在阅读一本关于要检查的Servlet的书,它说它会自动决定何时附加会话ID。我在网上看到了相互矛盾的陈述。当cookie被阻止时,会在多大程度上自动将会话ID附加到URL,这将包括使用sendRedirect()。我指的是最新版本。我已经在Oracle.com上查看了JAVA Docs,但我不确定它们最近是否已经更新。
谢谢: - )
答案 0 :(得分:1)
不,容器没有智能来检测您写入输出流的模板文本或字符串中的链接。
对于Ex,请参考以下示例代码:
servletoutputStream.write("<form method=\"post\" action=\"/submit.jsp\");
在上面的代码中,您必须HttpServletResponse.encodeURL("/submit.jsp")
在帖子操作网址中设置会话ID。
同样,HttpServletResponse.encodeRedirectURL()
的API文档说明了
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.
<强>参考文献:强>