简体   繁体   English

Java Servlet RequestDispatcher没有转发url

[英]Java Servlet RequestDispatcher didn't forward the url

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 我认为问题出在路上,但我不确定

From the ServletRequest#getRequestDispatcher javadoc : 来自ServletRequest#getRequestDispatcher javadoc

The pathname specified may be relative, although it cannot extend outside the current servlet context. 指定的路径名​​可能是相对的,但它不能扩展到当前的servlet上下文之外。 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容器无法返回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不是以"/"开头那么它将是相对的,因此转发被发送到"/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,以使路径相对于上下文根。

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

相关问题 为什么Servlet名称在RequestDispatcher.forward之后保留在url中? - Why does servlet name stay in url after RequestDispatcher.forward? Servlet 中的 RequestDispatcher.forward(req, res) 没有任何效果 - RequestDispatcher .forward(req, res) in Servlet doesn't have any effect 如何将requestdispatcher转发到远程URL - How to forward the requestdispatcher to a remote URL RequestDispatcher不在Java Servlet中重定向到JSP - RequestDispatcher not redirecting in java servlet to JSP 在RequestDispatcher的前向URL中使用哈希符号 - Using hash symbol in forward URL with RequestDispatcher Servlet过滤器-传递无法与RequestDispatcher#forward一起使用的属性 - Servlet filters - passing an attribute not working with RequestDispatcher#forward 如何使用RequestDispatcher从Servlet转发到内部页面链接 - How to forward to an internal page link from a servlet with RequestDispatcher 使用RequestDispatcher将请求从Servlet转发到JSP不会隐藏目标URL - Forwarding a request from servlet to JSP using RequestDispatcher doesn't hide the target URL RequestDispatcher:Servlet映射无法正常工作 - RequestDispatcher: Servlet mapping doesn't work properly 当通过Ajax POST调用servlet时,RequestDispatcher.forward()不转发 - RequestDispatcher.forward() not forwarding when servlet called via Ajax POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM