Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : Servlets : 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.

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 )

See all comments and questions (post-ad) posted for this tutorial.


Related Articles
  1. Introduction to Java Servlets, Developing 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: 8, Comments: 10 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. calling java apllication from servlet
  2. how to process a servlet's getParameter(); method in a different servlet.
  3. Ragrding usage of ur site
  4. does requestDispatcher.include() commits a response
  5. how to retrieve output on a called servlet from a included servlet when used requestDispatcher
  6. develop one JSP page to modify an XML document
  7. How Can I display windows media player from servlet
  8. How to forward request to servlet running on a different server from first servlet ( 2 Replies )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise 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.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.