SendRedirect in servlet

Page redirection is generally used when a document or any files moves to a new location and we need to send the client to this new location or may be to balance the load of server caused by more users accessing the page.


The simplest way of redirecting a request to another page is using method sendRedirect() of response object.


In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not. If not then the servlet decides that the request can be handle by other servlet or jsp or html. Then the servlet calls the sendRedirect() method on the response object and sends back the response to the browser along with the status code. Then the browser sees the status code and look for the resource which can now handle the request. Again the browser makes a new request, but with the name of that resource which can now handle the request and the result will be displayed to you by the browser. In all this process the client is unaware of the processing.

Page redirection is generally used when a document or any files moves to a new location and we need to send the client to this new location or may be to balance the load of server caused by more users accessing the page.


The simplest way of redirecting a request to another page is using method sendRedirect() of response object.


In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not. If not then the servlet decides that the request can be handle by other servlet or jsp or html. Then the servlet calls the sendRedirect() method on the response object and sends back the response to the browser along with the status code. Then the browser sees the status code and look for the resource which can now handle the request. Again the browser makes a new request, but with the name of that resource which can now handle the request and the result will be displayed to you by the browser. In all this process the client is unaware of the processing.



Project Structure

Servlet Passing Parameter

The code of the program is given below:

login.jsp

package com.jwt.hibernate;

import java.io.Serializable;

public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private int sal;
	private String city;
	private int phone;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSal() {
		return sal;
	}

	public void setSal(int sal) {
		this.sal = sal;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getPhone() {
		return phone;
	}

	public void setPhone(int phone) {
		this.phone = phone;
	}

}

RedirectServlet.java

package com.jwt.servlet;

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;

public class RedirectServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		String name = request.getParameter("username");
		String password = request.getParameter("password");
		if (name.equals("mukesh") && password.equals("kumar")) {
			response.sendRedirect("validUser");
		} else {
			pw.println("u r not a valid user");
		}
	}
}

ValidUser.java

package com.jwt.servlet;

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;

public class ValidUser extends HttpServlet {

	private static final long serialVersionUID = 1L;
	

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw = response.getWriter();
		pw.println("Welcome to javawebtutor.com
"); } }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<servlet>

		<display-name>SendRedirect</display-name>
		<servlet-name>SendRedirect</servlet-name>
		<servlet-class>com.jwt.servlet.RedirectServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SendRedirect</servlet-name>
		<url-pattern>/sendRedirect</url-pattern>
	</servlet-mapping>
	<servlet>

		<display-name>ValidUser</display-name>
		<servlet-name>ValidUser</servlet-name>
		<servlet-class>com.jwt.servlet.ValidUser</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ValidUser</servlet-name>
		<url-pattern>/validUser</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>
Run the Application

Right Click on the project and select Run as -> Run on Server and select the Tomcat Server and add your project in the server and click Finish.

Output in Browser

Servlet Passing Parameter





comments powered by Disqus