- Sends a temporary redirect response to the client using the specified redirect location URL.
- This is a client side reDirect.
- When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost.
- sendRedirect() method is a method of HttpServletResponse interface.
- When a client sends a request for a particular page to a server and server sees that this request is can’t be performed by this page, then it sends a error code to the browser specifying that the request can’t be performed by this page. Along with the error code it also gives the address of the page which will be able to perform the request of the client.
- In sendRedirect() the object of request will be generated again with the location of page which will perform the request of the client.
Understanding sendRedirect()
index.jsp
<html>
<head>
<title>Using sendRedirec()</title>
</head>
<body>
<a href="getList">Click here to get the list of URL</a>
</body>
</html>
Destination.java
package javaskool;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Destinations extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>SEND REDIRECT EXAMPLE</title></head>");
out.println("<form action=\"sendRed\">");
out.println("<INPUT type=\"radio\" name=\"website\" value=\"http://www.javaskool.com\">JavaSkool");
out.println("<br>");
out.println("<INPUT type=\"radio\" name=\"website\" value=\"http://www.google.com\">Google");
out.println("<br>");
out.println("<INPUT TYPE=\"submit\" VALUE=\"SUBMIT..\">");
out.println("</form>");
out.println("</html>");
}
}
SendRedirect.java
package javaskool;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendRedirect extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, java.io.IOException
{
String site = request.getParameter("website");
response.sendRedirect(response.encodeRedirectURL(site));
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>h1</servlet-name>
<servlet-class>javaskool.Destinations</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>h1</servlet-name>
<url-pattern>/getList</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>h2</servlet-name>
<servlet-class>javaskool.SendRedirect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>h2</servlet-name>
<url-pattern>/sendRed</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Click Here to download the sendRedirect() Example
Recent Comments