RequestDispatcher Mechanism in Servlet

All calls to other resources in a web application should go through the container. RequestDispatcher is a mechanism provided by the container for that purpose. Container will give us an implementation of the RequestDispatcher interface and we can use it to delegate control to other resources in the application.

You can obtain RequestDispatcher object from

  • ServletContext object through the

    • getRequestDispatcher(String urlPathToResource) or

    • getNamedDispatcher(String servletLogicalName) methods,

  • ServletRequest object through the

    • getRequestDispatcher(String urlPathToResource) method.

Note: The getRequestDispatcher method of ServletContext can accept only a context relative url starting with forward slash (/) whereas the ServletRequest version can accept servlet relative url without a forward slash (/)  as well.  

RequestDispatcher has two methods

  • ‘void forward(ServletRequest request, ServletResponse response)’

    • to forward the ServletRequest to the specified resource in RD completely delegating the response and

  • ‘void include(ServletRequest request, ServletResponse response)’

    • to include the runtime evaluation of the specified resource in RD and execution continues in the current servlet.

 

Important rules and recommendations while using RequestDispatcher

Please refer to the demo to understand these rules better:

  1. An included servlet's contents are added to the current servlets output.

  2. Included servlets contents are added to the current servlets output. If we call more includes, data from all includes will be included in the response unless any of the include servlet or even the parent servlet do a commit. If a commit is made, no exception is thrown for subsequent calls to include. However, any data after commit (including from the included servlet and parent servlet) are ignored and are not sent to client.

  3. Whenever you make a request to forward, all data already in the response buffer is cleared and not sent in response.

  4. You should not forward after a commit. When you forward, the response is commited by container before returning back, and hence you cannot forward again.

  5. If you do any commit before forward even unknowingly (e.g. from inside the include servlet), you will still get the exception.

  6. Similarly, if you try to forward after a call to out.flush() or out.close(), you will get the same exception.

  7. Any data after commit (including from the included servlet) are ignored and are not sent.

  8. Even if you try to write more contents to the initial servlet's out after a commit (after a forward, or after a call to out.flush() or out.close()), the data is discarded.

  9. If we have already called getWriter in the FirstServlet, calling getOutputStream will give an exception: java.lang.IllegalStateException: getWriter() has already been called for this response.

  10. [Recommendation] It is recommended to call forward before any call to getWriter or getOutputStream, as we don’t know which one will  be called in the forwarded resource. 

  11. You may include other servlets from the forwarded servlets and the rules remain same for including a servlet from any servlet. All included contents after a forward and until a commit or end of that forward will be part of response.

Tags: 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)