We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 30 March 2014

Servlet – SendRedirect with example

In this tutorial we are going to see sendRedirect in servlet with example program.
Send Redirect
void sendRedirect(java.lang.String location) throws java.io.IOException

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called ServletExample as given below.

Servlet-dynamic web project

3. Create package called com.javatutorialscorner.servlet under ServletExample.

4. Create Servlet called SendRedirectServlet as shown in figure.

Create Servlet

http servlet

5. Click Next it will show URL mapping.You can edit Servlet URL if you need.

6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.

http servlet Services

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml

Servlet Mapping

<servlet>
    <servlet-name>Your Servlet Name</servlet-name>
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Your Servlet Name</servlet-name>
    <url-pattern>/URL to Call Servlet</url-pattern>
  </servlet-mapping> 

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">
  <display-name>ServetExample</display-name>
  <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>
  <servlet>
    <servlet-name>SendRedirectServlet</servlet-name>
    <servlet-class>com.javatutorialscorner.servlet.SendRedirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SendRedirectServlet</servlet-name>
    <url-pattern>/SendRedirectServlet</url-pattern>
  </servlet-mapping>
</web-app>

8. Add the required code inside doGet() method.

SendRedirectServlet.java

package com.javatutorialscorner.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

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

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.sendRedirect("http://www.google.com/search?q=java tutorials corner");
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

9. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

10.call the following URL .

http://localhost:8080/ServletExample/SendRedirectServlet

Output

image

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – SendRedirect with example Rating: 5 Reviewed By: eHowToNow