encodeURL和encodeRedirectURL有什么区别?

Hes*_*sey 4 java servlets

我已经看到了存在的问题:差异化的encodeURLencodeRedirectURL.但它并没有真正回答这个问题.在我的测试中,这两种方法看起来像是一样的.无论我使用什么,print或者sendRedirect他们都工作正常.

那么真的有什么区别吗?我想看看源代码,所以也许我可以找到差异,但是HttpServletResponse没有实现的接口.实施代码在哪里?

Bal*_*usC 5

但是HttpServletResponse没有实现的接口.实施代码在哪里?

它是servletcontainer本身,它是具体的Servlet API实现.在例如Apache Tomcat的情况下,具体实现是org.apache.catalina.connector.Response.以下是相关摘录:

 1128       /**
 1129        * Encode the session identifier associated with this response
 1130        * into the specified redirect URL, if necessary.
 1131        *
 1132        * @param url URL to be encoded
 1133        */
 1134       public String encodeRedirectURL(String url) {
 1135   
 1136           if (isEncodeable(toAbsolute(url))) {
 1137               return (toEncoded(url, request.getSessionInternal().getIdInternal()));
 1138           } else {
 1139               return (url);
 1140           }
 1141   
 1142       }
Run Code Online (Sandbox Code Playgroud)
 1159       /**
 1160        * Encode the session identifier associated with this response
 1161        * into the specified URL, if necessary.
 1162        *
 1163        * @param url URL to be encoded
 1164        */
 1165       public String encodeURL(String url) {
 1166           
 1167           String absolute = toAbsolute(url);
 1168           if (isEncodeable(absolute)) {
 1169               // W3c spec clearly said 
 1170               if (url.equalsIgnoreCase("")){
 1171                   url = absolute;
 1172               }
 1173               return (toEncoded(url, request.getSessionInternal().getIdInternal()));
 1174           } else {
 1175               return (url);
 1176           }
 1177   
 1178       }
Run Code Online (Sandbox Code Playgroud)

差异非常微妙.encodeURL()只要给定(相对)URL为空,就使用完整的绝对URL.