Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java Servlets
  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. 

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:

                         

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

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.