Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestDispatcher forwarding is getting rerouted back to the same Servlet

I have a servlet called User.java. It is mapped to the url pattern

    <servlet-mapping>
        <servlet-name>User</servlet-name>
        <url-pattern>/user/*</url-pattern>
    </servlet-mapping>

Inside the Servlet, the path following the slash in user/ is analyzed, data about that user is retrieved from the database, set in attributes, and then the page user_home.jsp is to be displayed. The code to make this happen is:

            User user = UserManager.getUserInfoById(userPath);  
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("resources/jsp/user_home.jsp").forward(request, response);

The problem is, that rather than opening this user_home.jsp, the request is mapped once again to the same servlet User.java. It does nothing.

I've put output statements at the beginning of the doGet method, so I can see that the URL is

http://localhost:8080/myproj/user/resources/jsp/user_home.jsp

so it seems the obvious problem is that it's mapping right back to the user/* pattern.

How do I get the Servlet to display this page without going through URL mapping, and properly display the jsp I need it to?

like image 464
CodyBugstein Avatar asked Sep 05 '14 02:09

CodyBugstein


People also ask

What is the use of forward () of RequestDispatcher?

forward. Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

What is the difference between RequestDispatcher forward () and include () method?

Junior developers often get confused between the include and the forward methods of the RequestDispatcher. The key difference between the two is the fact that the forward method will close the output stream after it has been invoked, whereas the include method leaves the output stream open.

What is RequestDispatcher explain the two methods from RequestDispatcher?

The RequestDispatcher interface provides the option of dispatching the client's request to another web resource, which could be an HTML page, another servlet, JSP etc. It provides the following two methods: public void forward(ServletRequest request, ServletResponse response)throws ServletException, java. io.

What is difference between redirect and RequestDispatcher scenario?

The RequestDispatcher interface allows you to do a server side forward/include whereas sendRedirect() does a client side redirect. SendRedirect() will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content.


1 Answers

If the path passed to request.getRequestDispatcher() does not begin with a "/", it is interpreted as relative to the current path. Since your servlet's path is /user/<something>, it tries to forward the request to /user/resources/jsp/user_home.jsp, which matches your servlet mapping and therefore forwards to the same servlet recursively.

On the other hand, if the path passed to request.getRequestDispatcher() begins with a "/", it is interpreted as relative to the current context root. So assuming that the resources directory is located at the root of your webapp, try adding a "/" at the beginning of the path, e.g.:

request.getRequestDispatcher("/resources/jsp/user_home.jsp").forward(request, response);
like image 55
David Levesque Avatar answered Sep 24 '22 05:09

David Levesque