RequestDispatcher forward method

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

RequestDispatcher forward :

Hello, we are going to learn about RequestDispatcher forward method in servlet API.

Why is it needed?

The need may arise such that when a request is made for some specific resource, and the resource cannot handle the operations those are needed, it can simply delegate those operations to another resource and another resource serves the request with its own response.

How to do it?

Servlet specification defines an interface called RequestDispatcherRequestDispatcher forward can be used for this purpose. As per javadoc,

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.
Forward method:
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.

Step 1: Create a dynamic web project.

Create a new dynamic web project in eclipse named ServletJSPExamples .

Step 2: Create and configure DispatcherServlet.

Create a servlet named DispatcherServlet which will use RequestDispatcher forwardto some other resource, in this case,  Hello.jsp  .

Configure it in web.xml

 Step 3: Create Hello.jsp

Create Hello.jsp  in WebContent folder in eclipse.

 Step 4: Run application.

Deploy your application in tomcat and hit url as per your application. As our application name is ServletJSPExamples , the url will be http://localhost:8080/ServletsJSPExamples/DispatcherServlet

You will see the output as  Hello page.

Explanation:

We got the output as Hello page. that’s fine, but what is happening here. Let us understand step by step.

  • In DispatcherServlet , we created a PrintWriter object so that response can be written.
  • After out.println();  statement, a RequestDispatcher is created for  Hello.jsp  using RequestDispatcher rd = request.getRequestDispatcher("Hello.jsp");
  • rd.forward(request, response); i.e. RequestDispatcher forward method sends HttpServletRequest  and HttpServletResponse objects to the resource for which RequestDispatcher is created. So in this case, it’s Hello.jsp .
  • Control is forwarded to  Hello.jsp  and the contents are written to response. The response is committed and sent to the client.

The figure will elaborate the same.

RequestDispatcher#forward method

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

19 thoughts on “RequestDispatcher forward method

  • Pingback:request.getRequestDispatcher and response.sendRedirect | theJavaGeek

  • March 24, 2015 at 1:02 pm
    Permalink

    Hi sir,
    i’m writing code for 3 servlets like s1,s2 and s3,while changing the url what is procedure i’m not getting.both s2,s3 should have the same url as s1 or what and i’m gettin 404 error

    Reply
    • March 24, 2015 at 5:26 pm
      Permalink

      Hi Priya, I’m not sure what you mean by ‘changing the url, what is the procedure?’. What requestDispatcher does is forward the request to other resource i.e. s1 to s2 without changing the url in browser. Browser requests for s1 but, as it is forwaded, response is served by s2

      Reply
  • April 6, 2015 at 1:31 pm
    Permalink

    Hello,
    I am using mvc architecture and i have one servlet that connect to database and get the result and i have stored result in arraylist then i call request.setAttrbute() method and using request dispatcher forward method control pass to jsp page and i get all result from that servlet but 2 things happen

    1.Url not changed it still servlet name i used both method forward and include.how to get jsp page path in that browser url.

    2.As in url instead of jsp, it shows servlet name, so whatever i designed in jsp using css are not reflected.

    Please provide some solution for it.

    Reply
    • April 7, 2015 at 9:34 am
      Permalink

      In case of RequestDispatcher, control is forwarded to the resource which is used to create RequestDispatcher. Url of servlet is not changed because even if the browser is requesting a servlet, on the server side, it is being forwarded to JSP page.So url will not change when you use requestDispatcher. Regarding your CSS problem, I don’t think it is related to servlet or jsp url. Please check whether your css is working properly or not.

      Reply
  • April 6, 2015 at 1:51 pm
    Permalink

    And using send redirect method i am not getting object that we set in servlet

    Reply
    • April 7, 2015 at 9:31 am
      Permalink

      HI Ashish, using sendRedirect, you are creating a new request object, hence previously set request object is not available. For that you will either need to use requestDispatcher or set attributes in session object and access them in subsequent requests.

      Reply
      • April 7, 2015 at 10:24 am
        Permalink

        Thank you so much for previous suggestion it help a lot.
        I have another problem that I am working on on erp project for mechnical company in that project for yearly 5000 sales invoice(Bill) get generated due to this that much invoice loading in jsp page system get slow or it takes time how we can manage that bills does not takes more time for loading i know this is database issue but any good suggestion for that or we take new schema for each year or same schema we can use further year or any other option for that or any shortcut technique.

        Reply
        • April 8, 2015 at 10:28 am
          Permalink

          are you having problems in loading bills in jsp? I think they will be generated at once, right? After that you only need to access them.

          Reply
          • April 8, 2015 at 10:42 am
            Permalink

            Yes i have problem with loading bills in jsp and they only generated once and after that i have only to access them.

          • April 9, 2015 at 9:41 am
            Permalink

            What is the exact problem you are facing? is it about loading pdf in browser? If it is so, I’m afraid I do not have much information about it. If you are loading all files on one jsp at once, then pagination is something you should google.

  • February 2, 2016 at 5:32 pm
    Permalink

    what are the major scenarios in Application development where we should use sendRedirect() and not forward().

    Reply
  • August 12, 2016 at 5:22 pm
    Permalink

    hi
    when I am using
    String jspName =”/WEB-INF/admin/home.jsp”;
    RequestDispatcher rd = request.getRequestDispatcher(jspName);
    rd.forward(request, response);
    return;
    its redirecting but content is not displaying properly.
    I have used bootstrap and jquery in home.jsp

    Reply
    • August 16, 2016 at 9:02 am
      Permalink

      Hi, there should be some issue with bootstrap css not being applied. Could you please post your code here?

      Reply
  • January 29, 2017 at 8:24 pm
    Permalink

    hello sir
    in requestdispatcher, css and jquery isn’t working or if i kept the pages into a folder then requestdispatcher is unable to forward that page.

    Reply
    • January 31, 2017 at 9:24 am
      Permalink

      are you getting any exceptions or can you see anything in stacktrace or some error ?

      Reply
  • July 8, 2017 at 4:53 am
    Permalink

    Hi, I am Koneti Vamshi. Sir, can please upload the full context of web.xml file and the url patterns which you are defining because, my tomcat server was always throwing the error of Starting tomcatv9.0 server at localhost encountered a problem. I have tried all the possible methods to rectify the problem and even though I am getting same problem. I am really pissed off with this issue. I really lose a hopes of developing a web pages. Later I came to know that web.xml file and url patterns plays a very very crucial role in deploying the web pages. So can you please post the full web.xml file with specific url patterns defined in the project.

    Reply
    • July 10, 2017 at 9:16 am
      Permalink

      As this is quite old article, it will take time for me to find it and upload. Please give me some time and I will upload if anything is missing. Meanwhile, could you please post the error details here? are you getting anything on console?

      Reply
  • March 13, 2018 at 2:14 am
    Permalink

    Hi Sir
    I have a problem using forward in servlets with the following code:

    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
    throws ServletException, IOException {

    //Stores URL the client will be forwarded
    String fwdURL = “/myProject/website/page.html”;
    RequestDispatcher dispatcher = req.getRequestDispatcher(fwdURL);
    dispatcher.forward(req, resp);
    }

    When the servlet is executed the following error occurs

    java.lang.StackOverflowError
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:283)

    Could you help me with this?
    Thanks in advance.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *