简体   繁体   English

Servlet不执行response.sendRedirect(addressPath); ,但会执行没有路径的response.sendRedirect()

[英]Servlet doesn't execute response.sendRedirect(addressPath); , but does execute response.sendRedirect() without path

Consider the following hierarchy : 考虑以下层次结构:

在此处输入图片说明

When I send redirect like this : 当我这样发送重定向时:

    response.sendRedirect("error404.jsp");    // no path here !!!

I reach the page error404.jsp . 我到达页面error404.jsp

But when I use a path : 但是当我使用路径时:

    String addressPath = "/WEB-INF/results/admin/adminPage.jsp";
    response.sendRedirect(addressPath);   // with path !!!

I get 404 : 我得到404:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/7.0.50

What am I doing wrong here ? 我在这里做错了什么?

Much appreciated ! 非常感激 !

See the javadoc javadoc

This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. 此方法可以接受相对URL; Servlet容器必须在将响应发送到客户端之前将相对URL转换为绝对URL。 If the location is relative without a leading '/' the container interprets it as relative to the current request URI. 如果位置是相对的而没有前导“ /”,则容器会将其解释为相对于当前请求URI的相对位置。 If the location is relative with a leading '/' the container interprets it as relative to the servlet container root. 如果位置与前导“ /”相对,则容器将其解释为相对于servlet容器根。 If the location is relative with two leading '/' the container interprets it as a network-path reference (see RFC 3986: Uniform Resource Identifier (URI): Generic Syntax, section 4.2 "Relative Reference"). 如果位置与两个前导“ /”相对,则容器将其解释为网络路径引用(请参阅RFC 3986:统一资源标识符(URI):通用语法,第4.2节“相对引用”)。

Note that the argument is not a path within the servlet context like a RequestDispatcher would use, it as a URL used in the Location header of the 302 response. 请注意,该参数不是Servlet上下文中的路径(如RequestDispatcher所用),它是302响应的Location标头中使用的URL。

So this 所以这

String addressPath = "/WEB-INF/results/admin/adminPage.jsp";
response.sendRedirect(addressPath);   // with path !!!

will be transformed into a 302 response with the header 将被转换为带有标头的302响应

Location: http://whateverhost.com/WEB-INF/results/admin/adminPage.jsp

which you don't have a handler for, so 404. 您没有处理程序,所以是404。

On the other hand, this 另一方面,这

response.sendRedirect("error404.jsp");    // no path here !!!

becomes

Location: http://whateverhost.com/context-path/error404.jsp

Since error404.jsp is outside WEB-INF , it is accessible and therefore rendered by the JSP servlet and returned as a response. 由于error404.jspWEB-INF外部,因此它是可访问的,因此由JSP servlet呈现并作为响应返回。

Because stuff under WEB-INF is not accessible to the client. 因为客户端无法访问WEB-INF下的内容。 Don't put your JSPs under WEB-INF if you want them to be accessible. 如果您希望JSP可以访问,则不要将它们放在WEB-INF下。

Alternatively, to make anything under WEB-INF accessible to the client, you would have to map it to a URL in web.xml: 或者,要使客户端可以访问WEB-INF下的任何内容,您必须将其映射到web.xml中的URL:

  <servlet>
    <servlet-name>adminPage</servlet-name> 
    <jsp-file>/WEB-INF/results/admin/adminPage.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>adminPage</servlet-name>
    <url-pattern>/adminPage/*</url-pattern>
</servlet-mapping> 

Then use the url-pattern you mapped: 然后使用您映射的url模式:

response.sendRedirect("./adminPage/");

Which is rather pointless. 这是毫无意义的。 You could achieve basically the same thing with your JSP outside of WEB-INF and using a URL Rewrite Filter. 使用WEB-INF之外的JSP并使用URL重写过滤器,您可以实现基本相同的操作。 In short, you probably have no real reason to put your JSPs under WEB-INF. 简而言之,您可能没有真正的理由将JSP置于WEB-INF下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM