Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Send Redirect in Servlet 
 

When we want that someone else should handle the response of our servlet, then there we should use sendRedirect() method.

 

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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     <input type=
"text" name="username" size="20"></p>
  <p>Enter your password&nbsp; <input type="text" name="password" size="20"></p>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  <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:

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

i like very much ...
i need this kind of info could u mail to me regular

Posted by Senthilkumar on Thursday, 12.4.08 @ 03:56am | #82372

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

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.