5:32 pm - Thursday August 17, 2017

Servlet SendRedirect Tutorial with Example

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

Send Redirect can be used to communicate between two servlet present in different servers, the output will be same as request dispatcher forward example but the url of the page will be changed to redirected page

Servlet code:

res.sendRedirect(“http://localhost:8086/chaining/Servlet2″);

Project Structure:

 

 

 

 

 

 

 

 

 

SendRedirect.java:

package com.candid;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class SendRedirect
 */
public class SendRedirect extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public SendRedirect() {
		// 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>");
		response.sendRedirect("http://localhost:8090/Chaining/Chain2");
		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>SendRedirect</display-name>
	<servlet>
		<description>
		</description>
		<display-name>SendRedirect</display-name>
		<servlet-name>SendRedirect</servlet-name>
		<servlet-class>com.candid.SendRedirect</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SendRedirect</servlet-name>
		<url-pattern>/SendRedirect</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