• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Chaining » RequestDispatcher in Servlet
Next →
← Prev

RequestDispatcher in Servlet

By Dinesh Thakur

While building a complex web application there might be a need to distribute the request to multiple servlets. This is where request dispatching comes into use. Due to this requirement Servlet container supports request dispatching within the same context.

The javax.servlet.RequestDispatcher is an interface that enables the Servlet Container to dispatch the request from a web application to another within the same context.

 

In order to dispatch the request we need to perform these tasks:

• Get a RequestDispatcher object

• Use the forward() method or include method of RequestDispatcher

 

There are three approaches to create the RequestDispatcher object:

 

i. By using ServletRequest object

ii. By using getRequestDispatcher() method of ServletContext

iii. By using getNamedDispatcher() method of ServletContext

 

Approach 1: (by using reqobj)

 

Example 1

 

In srv1 source code,

 

RequestDispatcherrd=req.getRequestDispatcher(“s2url“);

rd.forward(req,res);

           (or)

Rd.include(req,res);

 

Example 2

 

In source srv2 program

 

RequestDispatcherrd=req.getRequestDispatcher(“/abc.html“);

      (or)

RequestDispatcherrd=req.getRequestDispatcher(“/abc.jsp“);

rd.forward(req,res);

           (or)

rd.include(req,res);

 

Approach 2: (by using ServletContextobj)

 

Example 1

 

In srv1 source code,

 

ServletContext sc=getServletcontext();

RequestDispatcherrd=req.getRequestDispatcher(“s2url“);

rd.forward(req,res);

           (or)

Rd.include(req,res);

 

Example 2

 

In source srv1 program

 

ServletContext sc=getServletcontext();

RequestDispatcherrd=req.getRequestDispatcher(“/abc.html“);

      (or)

RequestDispatcherrd=req.getRequestDispatcher(“/abc.jsp“);

rd.forward(req,res);

           (or)

rd.include(req,res);

 

Approach 3: (by using ServletContextobj)

 

Example 1

 

In srvl source code,

ServletContext sc=getServletcontext();

RequestDispatcherrd=sc.getNameDispatcher(“s2”);

rd.forward(req,res);

           (or)

rd.include(req,res);

 

Example 2

 

In srv1 source code,

 

ServletContext sc=getServletcontext();

RequestDispatcherrd=sc.getNameDispatcher(“j2”);

rd.forward(req,res);

           (or)

rd.include(req,res);

 

• If the source servlet program calls rd.forward(req,res) method, then it performs forwarding request mode of servlet chaining with destination web resource program.

• Servlet configuration in web.xml is mandatory so it contains logical name and URL pattern.

• The JSP program configuration in web.xml file is optional so it mayor may not contain logical name and URL pattern. We cannot configure HTML in web.xml.

You’ll also like:

  1. How to use RequestDispatcher include method
  2. How to use RequestDispatcher Forward method
  3. Difference between the REQUESTDISPATCHER Object and SERVLETCONTEXT Object
  4. Servlet Chaining Between Two Servlet Programs
  5. What is the Servlet container raises the life cycle events in servlet program?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW