In doGet() we display a Form to the user giving the option to either
test the forward() method or the include() method. To determine user
clicked which button we provide a hidden input field with name of 'mode' and a value of
'forward' ( for forwarding ) or 'include' ( for including ).
We handle the user response in our doPost() method and depending on the
type of button clicked we either forward the request to the second Servlet or include it's
response in the first Servlet.
We also set the attribute "mode" in the HttpServletRequest object to a
value depending on which button user clicked. We will display this value in the second
Servlet.
TestDispatcherServlet2
Create a new TestDispatcherServlet2.java file in CATALINA_HOME/webapps/star/WEB-INF/classes/com/stardeveloper/servlets/
folder. Copy and paste the following code in it :
package com.stardeveloper.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestDispatcherServlet2 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Nothing important here
out.print("<html><head><style>");
out.print("p,a{font-family:tahoma;font-size:10pt;");
out.print("color:black;}");
out.print("input{width:20;height:20;}");
out.print("</style></head><body>");
out.print("<p><a ");
out.print("href=\"/servlet/com.stardeveloper.servlets.");
out.print("TestDispatcherServlet1\">");
out.print("Go to TestDispatcherServlet1</a>.</p>");
out.print("</body></html>");
out.close();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String mode = (String)req.getAttribute("mode");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><head><style>");
out.print("p,a{font-family:tahoma;font-size:10pt;");
out.print("color:black;}");
out.print("input{width:20;height:20;}");
out.print("</style></head><body>");
out.print("<p><b>");
out.print("TestDispatcherServlet2</b> :</p>");
if(mode != null) {
out.print("<p>Attribute value received : ");
out.print( mode );
out.print("</p>");
} else {
out.print("<p>I have been invoked directly.</p>");
}
out.print("</body></html>");
out.close();
}
}
Explanation
Our Servlet above extends HttpServlet class and overrides doGet()
and doPost() methods. doGet() doesn't do much except displaying a
link to the first Servlet.
In doPost() we receive the HttpServletRequest attribute 'mode'
and display it's value to the user.
Now point your browser to http://localhost:8080/star/servlet/ com.stardeveloper.servlets.TestDispatcherServlet1
to view the demo on your computer.
Summary
In this article we learned another feature of Servlet API, including and forwarding
response to other resources. We discussed forward() and include()
methods of RequestDispatcher interface. We then built a Dispatcher demo
application in which first Servlet forwards or includes response from second Servlet
depending on the user's choice. We also learned how to set and get Request
level attribute objects.
|