You are not logged in!
Stardeveloper.com
Home · Articles · Forums · Advertise · Contact
Article Categories
.NET  .NET
  ASP (14)
  ASP.NET (15)
  ADO (16)
  ADO.NET (5)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (2)

J2EE  J2EE
  JSP (14)
  Servlets (9)
  Web Services (1)
  EJB (4)
  E-Commerce (1)
  J2ME (1)
Product Spotlight
Sponsor Ad
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Site Log
  Submit Article

Powered by Securewebs.com

 
Home : J2EE : Servlets : General : Forwarding and Including Response from other Servlets
 

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.


Previous ( 1 Gone )( No Further Pages )

Related Articles
  1. Introduction to Servlets, Your first Java Servlet
  2. Examining Java Servlets in detail. Servlet life cycle, HttpServlet class, ServletConfig and ServletContext Objects
  3. Managing Sessions with Java Servlets

Comments/Questions ( Threads: 1, Comments: 1 )
  1. How to forward request to servlet running on a different server from first servlet ( 0 Replies ).

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you aren't a member yet then signup, else login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

 
© 1999 - 2003 Stardeveloper.com, All Rights Reserverd. Hosted by SecureWebs.