Consider the following hierarchy :
When I send redirect like this :
response.sendRedirect("error404.jsp"); // no path here !!!
I reach the page error404.jsp
.
But when I use a path :
String addressPath = "/WEB-INF/results/admin/adminPage.jsp";
response.sendRedirect(addressPath); // with path !!!
I get 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
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. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root. 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").
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.
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
Location: http://whateverhost.com/WEB-INF/results/admin/adminPage.jsp
which you don't have a handler for, so 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.
Because stuff under WEB-INF is not accessible to the client. Don't put your JSPs under WEB-INF if you want them to be accessible.
Alternatively, to make anything under WEB-INF accessible to the client, you would have to map it to a URL in web.xml:
<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:
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. In short, you probably have no real reason to put your JSPs under WEB-INF.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.