• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

sendRedirect in Servlet

February 1, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

In this article we shall see how to redirect your current page to some other page. The easiest way to achieve this is by using the sendRedirect() method of class javax.servlet.http.HttpServletResponse.

sendRedirect() method redirects your current page to another servlet or jsp page. This method is used to redirect response to another resource. This method accepts relative as well as absolute URL.
Using sendRedirect() method whenever the client makes any request it goes to the container, then the container decides whether servlet can handle request or not. If it is not handled then that request cab be handled by other servlets.

Some important points on sendRedirect() method:

  • In the sendRedirect method existing request and response objects are lost.
  • sendRedirect methods happens on the client side.
  • In this method request gets transferred to resource outside the application.
  • sendRedirect method works on response object.
  • sendRedirect method is transfer to another resource to different domain.
  • In the sendRedirect method it call old request and response object is list because it is treated as new request.
  • sendRedirect method is slower because extra round trip is required because completely new request is created and old object request is lost.

sendRedirect Example

Now let us try an example as below. In this exmaple the sendRedirect() method would take you to the URL mentioned https://javabeat.net

Listing 1: SendRedirectExample

package javabeat.net.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendRedirectExample extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

				response.sendRedirect("https://javabeat.net");
	}
}

Listing 2: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_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<servlet>
		<servlet-name>SendRedirectExample</servlet-name>
		<servlet-class>javabeat.net.servlets.SendRedirectExample</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>SendRedirectExample</servlet-name>
		<url-pattern>/SendRedirectExample</url-pattern>
	</servlet-mapping>

</web-app>

Previous Tutorial : Request Dispatcher in Servlet  || Next Tutorial : ServletConfig in Servlet

Category: Java EETag: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Display Your System Font Families in Java
Next Post: ServletConfig in Servlet »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact