Java Servlet RequestDispatcher 没有转发 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13216145/
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
Java Servlet RequestDispatcher didn't forward the url
提问by Agung Setiawan
I have problem with RequestDispatcher in Java Servlet, it didn't forward to the specific url if the servlet path is not in root path
我在 Java Servlet 中的 RequestDispatcher 有问题,如果 servlet 路径不在根路径中,它不会转发到特定的 url
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath=request.getServletPath();
String view = null;
if(userPath.equals("/admin")) //it's okay, forwarded
{
view="admin";
}
else if(userPath.equals("/admin/tambahArtikel")) //it's not forwarded
{
view="tambahArtikel";
}
else if(userPath.equals("/kategori")) //it's okay, forwarded
{
view="kategori";
}
String url="WEB-INF/view/"+ view +".jsp";
request.getRequestDispatcher(url).forward(request, response);
}
and this is my web.xml
这是我的 web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>ServletController</servlet-name>
<servlet-class>com.agung.webhakakses.servlet.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletController</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletController</servlet-name>
<url-pattern>/admin/tambahArtikel</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletController</servlet-name>
<url-pattern>/kategori</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
i think the problem is in the path but i'm not sure
我认为问题出在路径上,但我不确定
采纳答案by ElderMael
From the ServletRequest#getRequestDispatcher javadoc:
从 ServletRequest#getRequestDispatcher javadoc:
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
指定的路径名可能是相对的,但它不能扩展到当前 servlet 上下文之外。如果路径以“/”开头,则它被解释为相对于当前上下文根。如果 servlet 容器无法返回 RequestDispatcher,则此方法返回 null。
In your code, you build the url this way:
在您的代码中,您以这种方式构建 url:
String url="WEB-INF/view/"+ view +".jsp";
So, as the javadoc also says:
因此,正如 javadoc 还说的:
The difference between this method and ServletContext#getRequestDispatcher is that this method can take a relative path.
该方法与 ServletContext#getRequestDispatcher 的区别在于该方法可以采用相对路径。
So if your request URI is "/admin/tambahArtikel"
and your forwarding URI does not start with a "/"
then it will be relative, so the forward is sended to "/admin/" + "WEB-INF/view/"+ view +".jsp"
因此,如果您的请求 URI 是"/admin/tambahArtikel"
并且您的转发 URI 不以 a 开头,"/"
那么它将是相对的,因此转发将发送到"/admin/" + "WEB-INF/view/"+ view +".jsp"
If you need to forward to a resource in the WEB-INF
directory start your URI with a "/" so the path will be relative to the context root.
如果您需要转发到WEB-INF
目录中的资源,请以“/”开头您的 URI,因此该路径将相对于上下文根。