🚦Understanding Servlet Dispatcher (Beginner Guide)
In web applications using Java Servlets, sometimes we want one servlet to pass a request to another servlet. This is where Servlet Dispatcher comes in.
🌐 What is Servlet Dispatcher?
A Servlet Dispatcher helps in passing the request from one servlet to another without going back to the network. This is fast and efficient.
There are two types of dispatchers:
-
Include Dispatcher
-
Forward Dispatcher
Another way to move from one servlet to another is by using sendRedirect()
. But sendRedirect()
goes back to the network, which is slower. That’s why RequestDispatcher
is better inside a server.
🔄 Real-Life Example: Why Dispatcher?
Imagine three servlets: Servlet1
, Servlet2
, and Servlet3
.
Without Dispatcher
If a user in the USA calls Servlet1
on a server in India:
-
First, the request goes to India (Servlet1).
-
Next, if the user wants
Servlet2
, the request must go back to the server again. -
This back-and-forth is slow and increases network traffic.
With Dispatcher
If we use a dispatcher:
-
Servlet1
passes the request directly to Servlet2 inside the server. -
No need to go back to the network.
-
It saves time and is called Servlet Chaining.
Also, the request data can travel from one servlet to another. This is not possible with sendRedirect()
.
💡Why Dispatcher is Useful
-
Faster: Works inside the server.
-
Data Sharing: Request data moves from servlet to servlet.
-
Efficient: Great when you need to split work among servlets.
But remember: if there’s a lot of data, or you don’t need to send data, sendRedirect()
might be better.
🧩 Types of Request Dispatcher
1. 🧷 Include Dispatcher
The Include Dispatcher adds the response of another servlet into the current servlet’s response — like calling a helper method.
🔧 Example
-
IncludeServlet
includes the response fromIncludedServlet
. -
The browser sees both outputs combined.
Steps:
-
Create a Java Web Application.
-
Add
IncludeServlet.java
andIncludedServlet.java
.
IncludeServlet Code:
RequestDispatcher dis = getServletContext().getRequestDispatcher("/IncludedServlet");
dis.include(request, response);
IncludedServlet Code:
out.println("<h1>" + request.getAttribute("command") + " Included Servlet response</h1>");
📝 Note: We removed <html>
tags from IncludedServlet
so that HTML tags don’t repeat.
✅ Output:
The browser shows:
Servlet IncludeServlet at /YourApp
As commanded by IncludeServlet Included Servlet response
This message will be included.
After Include
2. 📨 Forward Dispatcher
The Forward Dispatcher sends the request from one servlet to another. But this time, only the second servlet sends the response to the browser.
🔧 Example
-
ForwardServlet
forwards the request toForwardedServlet
. -
The browser only sees output from
ForwardedServlet
.
ForwardServlet Code:
RequestDispatcher dis = getServletContext().getRequestDispatcher("/ForwardedServlet");
dis.forward(request, response);
📝 Note: Any code after dis.forward()
will not run.
ForwardedServlet Code:
out.println("<h1>" + request.getAttribute("forward") + " Forwarded Servlet response</h1>");
✅ Output:
The browser shows:
Servlet ForwardedServlet at /YourApp
As commanded by ForwardServlet Forwarded Servlet response
After Forwarded
🏁 Conclusion
-
Use RequestDispatcher when you want to pass data and control between servlets.
-
Choose Include when you want to combine responses.
-
Choose Forward when you want one servlet to hand over everything to another.
-
Use sendRedirect() only when the client needs to make a brand-new request.
Let me know if you'd like a diagram or flowchart to make this even easier to visualize!