Saturday 12, Apr 2025
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
1.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

1.<servlet>
2.    <servlet-name>Your Servlet Name</servlet-name>
3.    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
4.  </servlet>
5.  <servlet-mapping>
6.    <servlet-name>Your Servlet Name</servlet-name>
7.    <url-pattern>/URL to Call Servlet</url-pattern>
8.  </servlet-mapping>

web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
03.  <display-name>ServetExample</display-name>
04.  <welcome-file-list>
05.    <welcome-file>index.html</welcome-file>
06.    <welcome-file>index.htm</welcome-file>
07.    <welcome-file>index.jsp</welcome-file>
08.    <welcome-file>default.html</welcome-file>
09.    <welcome-file>default.htm</welcome-file>
10.    <welcome-file>default.jsp</welcome-file>
11.  </welcome-file-list>
12.  <servlet>
13.    <servlet-name>SendRedirectServlet</servlet-name>
14.    <servlet-class>com.javatutorialscorner.servlet.SendRedirectServlet</servlet-class>
15.  </servlet>
16.  <servlet-mapping>
17.    <servlet-name>SendRedirectServlet</servlet-name>
18.    <url-pattern>/SendRedirectServlet</url-pattern>
19.  </servlet-mapping>
20.</web-app>

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

SendRedirectServlet.java

01.package com.javatutorialscorner.servlet;
02. 
03.import java.io.IOException;
04.import javax.servlet.ServletException;
05.import javax.servlet.http.HttpServlet;
06.import javax.servlet.http.HttpServletRequest;
07.import javax.servlet.http.HttpServletResponse;
08. 
09./**
10. * Servlet implementation class SendRedirectServlet
11. */
12.public class SendRedirectServlet extends HttpServlet {
13. private static final long serialVersionUID = 1L;
14. 
15. /**
16.  * @see HttpServlet#HttpServlet()
17.  */
18. public SendRedirectServlet() {
19.  super();
20.  // TODO Auto-generated constructor stub
21. }
22. 
23. /**
24.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
25.  *      response)
26.  */
27. protected void doGet(HttpServletRequest request,
28.   HttpServletResponse response) throws ServletException, IOException {
29.  // TODO Auto-generated method stub
30.  response.sendRedirect("http://www.google.com/search?q=java tutorials corner");
31. }
32. 
33. /**
34.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
35.  *      response)
36.  */
37. protected void doPost(HttpServletRequest request,
38.   HttpServletResponse response) throws ServletException, IOException {
39.  // TODO Auto-generated method stub
40. }
41. 
42.}

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

Related Posts:

  • Servlet - Process From Data In this tutorials we are going see about submit form with all input type and how to read the values in servlet. 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project … Read More
  • How to display Image in JSP/HTML from file system using Servlet In this tutorial we are going to see how to display image in web page (HTML , JSP) from web server file system. 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project … Read More
  • Servlet – Exception Handling In this tutorial we are going to see about Exception Handling in Servlet with example program. Servlet Exception Handling In general doGet(), doPost() methods of servlet throws ServletException, IOException. When a servlet t… Read More
  • Servlet – Attribute in Application Scope using ServletContext In this tutorial we are going to see about , how to set and get attributes in application scope using ServletContext. 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Pr… Read More
  • Hello World HttpServletIn this tutorial we are going to see about HttpServlet with example program. A Servlet is just normal class which implements the Interface Servlet.javax.servlet.ServletThe following classes implements the Servlet Interfac… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

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