by Dinesh Thakur Category: Servlet Chaining

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.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.