java encodeURL 和 encodeRedirectURL 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4673425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:27:28  来源:igfitidea点击:

Difference between encodeURL and encodeRedirectURL

javajakarta-ee

提问by Abdullah Jibaly

The javadocfor javax.servlet.http.HttpServletResponseis a little vague on the difference between what rules encodeURLand encodeRedirectURLfollow, are there any examples of what those exact rules are? When will the output of encodeURLdiffer from encodeRedirectURL?

的javadocjavax.servlet.http.HttpServletResponse是什么之间的规则差异有点含糊encodeURLencodeRedirectURL随访,还有什么确切的规则是什么例子?的输出何时会encodeURLencodeRedirectURL?

For a concrete example, I am already generating a redirect url that I will use with response.sendRedirect(url). I get that url already encoded, but I want to add a parameter to it which has a value that is another url:

对于一个具体的例子,我已经生成了一个重定向 url,我将与response.sendRedirect(url). 我得到那个 url 已经编码,但我想向它添加一个参数,它的值是另一个 url:

param2=http://google.com

Which of the two functions do I use to encode this?

我使用这两个函数中的哪一个来编码?

采纳答案by Marvin Rabe

encodeURL()is used for all URLs in a servlet's output. It helps session ids to be encoded with the URL.

encodeURL()用于 servlet 输出中的所有 URL。它有助于使用 URL 对会话 ID 进行编码。

encodeRedirectURL()is used with res.sendRedirect only. It is also used for encoding session ids with URL but only while redirecting.

encodeRedirectURL()仅与 res.sendRedirect 一起使用。它还用于使用 URL 对会话 ID 进行编码,但仅在重定向时使用。

回答by Ismail Marmoush

Salam Alekom Abduallah, I looked and looked for an answer I knew I would find it in either stackoverflow or coderanch and there I found the answerfrom Charles Lyons the author of the book in my hand right now it was a cheerful coincidence.

Salam Alekom Abduallah,我寻找并寻找了一个答案,我知道我会在 stackoverflow 或 coderanch 中找到它,在那里我找到了本书作者 Charles Lyons 的答案,现在我手里拿着这本书,这是一个令人愉快的巧合。

posted 8/9/2008 11:41 AM Exactly - the difference being that encodeURL always writes the session ID into the URL (if required e.g. because cookies are disabled), while encodeRedirectURL contains additional logic to determine if it is desirable to write the session ID in. It is a really bad idea to give foreign websites the session ID for example, since then they could impersonate your user's session. Hence encodeRedirectURL will only put the jsessionid on the URL if that URL lies within the current Web application, and not perform any rewriting otherwise.

2008 年 9 月 9 日上午 11:41 发布 完全正确 - 不同之处在于 encodeURL 总是将会话 ID 写入 URL(如果需要,例如因为 cookie 被禁用),而 encodeRedirectURL 包含额外的逻辑来确定是否需要写入会话ID in。例如,给外国网站提供会话 ID 是一个非常糟糕的主意,因为这样他们就可以冒充您用户的会话。因此,如果 URL 位于当前 Web 应用程序中,encodeRedirectURL 只会将 jsessionid 放在 URL 上,否则不会执行任何重写。

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004) Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340)

Charles Lyons(SCJP 1.4,2003 年 4 月;SCJP 5,2006 年 12 月;SCWCD 1.4b,2004 年 4 月) OCEJWCD Study Companion for Oracle Exam 1Z0-899(ISBN 0955160340)的作者

also I found this answertoo which was posted earlier,

我也找到了之前发布的这个答案

posted 4/19/2006 8:02 AM Quote Report post to moderator Hi,

The encodeURL is used to encode the url for session tracking in forward and include mechanism. The encodeRedirectURL encodes the specified URL for use in the sendRedirect method.

The main difference between two is, the implementation of encodeRedirectURL method includes the logic to determine whether the session ID needs to be encoded in the URL in the case when you are redirecting the URL to different context where the session information is not required or invalid. The encodeURL method do not appent the seesion id if the cookies are enabled. In addition to this encodeRedirectURL do not append the session information if the URL is redirected to the different context (web application). Because the rules for making this determination can differ from those used to decide whether to encode a normal link, this method is separete from the encodeURL method.

Hope this help you.

Thanks

Narendra Dhande

2006 年 4 月 19 日上午 8:02 发布 引用报告发布给版主 您好,

encodeURL 用于在转发和包含机制中对用于会话跟踪的 url 进行编码。encodeRedirectURL 对在 sendRedirect 方法中使用的指定 URL 进行编码。

两者的主要区别在于,encodeRedirectURL 方法的实现包含了在将URL重定向到不需要会话信息或会话信息无效的不同上下文的情况下,确定是否需要在URL中编码会话ID的逻辑。如果启用了 cookie,encodeURL 方法不会附加 seesion id。除了此 encodeRedirectURL 之外,如果 URL 被重定向到不同的上下文(Web 应用程序),则不要附加会话信息。因为进行此确定的规则可能与用于决定是否对普通链接进行编码的规则不同,所以此方法与 encodeURL 方法不同。

希望这对你有帮助。

谢谢

纳伦德拉·丹德