3:58 pm - Saturday February 25, 2017

Request Dispatcher forward 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 forward method can be used to process more than one servlet present inside same server, the output will be displayed from last servlet only

Servlet Code:

<br /><br />RequestDispatcher r=getServletContext().getRequestDispatcher("/Servlet2");<br /><br />r.forward(req,res);<br /><br />

Project Structure:

 

 

 

 

 

 

 

 

 

Forward.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 Forward
 */
public class Forward extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Forward() {
		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("<h1>this is servlet one</h1>");
		RequestDispatcher r = getServletContext().getRequestDispatcher(
				"/Chain2");
		out.println("<font color=blue size=6>");
		r.forward(request, response);

		out.println(" print");
	}

	/**
	 * @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>Forward</display-name>
 <servlet>
 <description>
 </description>
 <display-name>Forward</display-name>
 <servlet-name>Forward</servlet-name>
 <servlet-class>com.candid.Forward</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>Forward</servlet-name>
 <url-pattern>/Forward</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