Send Redirect in Servlet

When we want that someone else should handle the
response of our servlet, then there we should use sendRedirect()
method.
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. Then the
servlet calls the sendRedirect() method of 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 that servlet which can now handle the
request. Again the browser makes a new request, but with the name of
that servlet 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.
In this example we are going to make one html in which
we will submit the user name and his password. The controller will check if the
password entered by the user is correct or not. If the password entered by the
user is correct then the servlet will redirect the request to the other servlet
which will handle the request. If the password entered by the user is wrong then
the request will be forwarded to the html form.
The code of the example is given below:
html file for this program:
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="/SendRedirect/SendRedirectServlet">
<p>Enter your name
<input type="text" name="username" size="20"></p>
<p>Enter your password <input type="text" name="password" size="20"></p>
<p>
<input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>
|
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendRedirectServlet extends HttpServlet{
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("James")&& password.equals("abc")){
response.sendRedirect("/SendRedirect/ValidUserServlet");
}
else{
pw.println("u r not a valid user");
}
}
}
|
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ValidUserServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("Welcome to roseindia.net " + " ");
pw.println("how are you");
}
}
|
web.xml file for this program:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Zulfiqar</servlet-name>
<servlet-class>SendRedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Zulfiqar</servlet-name>
<url-pattern>/SendRedirectServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>ValidUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/ValidUserServlet</url-pattern>
</servlet-mapping>
</web-app>
|
The output of the program is given below:
Download this example:

|
Current Comments
3 comments so far (post your own) View All Comments Latest 10 Comments:The example is very very good.But i have one doubt...
can u explain when we need RequestDispatcher and when we nee senRedirect?
Posted by Taslim Arif on Friday, 08.31.07 @ 05:00am | #24564
Nice Ciding using Normalisation;
Posted by Dinesh kr Pandey on Thursday, 07.19.07 @ 16:29pm | #21573
hello
i have some query plz help me
if i want to call one jsp file through servlet file then how will i do???
I have tried using request dispatcher but i m getting some error if i want to use response.sendRedirect Method so plz guide me
Posted by shenaz on Wednesday, 07.11.07 @ 12:03pm | #21065