Home Servlets Send Redirect in Servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Send Redirect in Servlet

Advertisement
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. 

The output of the program is given below:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Redirecting the page</title>
</head>
<body>
	<form action = "/ServletProject/SendRedirect" method = "post">
	<tr>
		<td>Enter your name :</td>
		<td><input type = "text" name = "username"></td>
	</tr><br>
	<tr>
		<td>Enter your password :</td>
		<td><input type = "password" name = "password"></td>
	</tr><br>
	<tr>
		<td><input type = "submit" name = "submit"></td>
	</tr>
	</form>
</body>
</html>

 

import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendRedirect extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public SendRedirect() {
super();

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub
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("/ServletProject/ValidUser");
}
else
{
pw.println("u r not a valid user");
}

}

 

import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: ValidUser
*
*/
public class ValidUser extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ValidUser() {
super();


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub



/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter pw = response.getWriter();
pw.println("Welcome to roseindia.net<br>");
pw.println("how are you");

}

The code of the program is given below:

 

 

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 25, 2008

Related Tutorials

Discuss: Send Redirect in Servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
adamjones
April 6, 2011
The output of the program

in GenericServlet.. public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { PrintWriter out=arg1.getWriter(); String n=arg0.getParameter("name"); String p=arg0.getParameter("pass"); if(n.equals("sai")&&p.equals("sai")) <a href="http://www.thesiswritinghelp.co.uk">thesis writing help</a>
dgfj
June 1, 2011
hii

Good example .I got the code & able to redirect the request to servlet
shahid ata
October 30, 2011
sendRedirect

in sendRedirect() client is aware of the processing please make change in your last line , in Requestdispatcher client is not aware.
Akshay
March 12, 2012
question

Can I compare name and password from database. please explain me with source code...
DMCA.com