1:19 am - Wednesday January 25, 2017

Request Dispatcher include tutorial

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Request Dispatcher include method can be used to process more than one servlet present in same server and the output will be displayed from all the servlet.

Servlet Code:


RequestDispatcher r=getServletContext().getRequestDispatcher("/Servlet2");

r.include(req,res);

Project Structure:


 

 

 

 

 

 

 

Include.java

package com.candid;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Include
 */
public class Include extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Include() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws

	ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<body bgcolor=\"pink\">");
		out.println("<Font color=blue size=7>");
		out.println("this is servlet one");

		out.println("</font>");
		RequestDispatcher r = getServletContext().getRequestDispatcher(
				"/Chain2");
		r.include(request, response);
		out.println("</br>");
		out.println("<font color=brown size=6> print</font>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws

	ServletException, IOException {
		// TODO Auto-generated method stub
	}
}

web.xml:  

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Include</display-name>
	<servlet>
		<description>
		</description>
		<display-name>Include</display-name>
		<servlet-name>Include</servlet-name>
		<servlet-class>com.candid.Include</servlet-class>
	</servlet>
	<servlet>
		<description>
		</description>
		<display-name>Chain2</display-name>
		<servlet-name>Chain2</servlet-name>
		<servlet-class>com.candid.Chain2</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Include</servlet-name>
		<url-pattern>/Include</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Chain2</servlet-name>
		<url-pattern>/Chain2</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

OUTPUT:  

 

DOWNLOAD+ SOURCE CODE                                                                                                                                                      BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply