Home Jsp Simple-jsp-example sendRedirect In JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

sendRedirect In JSP

Advertisement
sendRedirect() method is a method of HttpServletResponse interface. This tutorial will helps you to explain about sendRedirect().

sendRedirect in JSP

In this tutorial we will discuss about sendRedirect in JSP. This method is used to redirect the user to some other location it may be different server or different context. This is done by the help using browser which transfer the request. The request is visible as a new request. This transfer is done by web container internally in which browser is not involved.

SendRedirect ():  This method is declared in HttpServletResponse Interface.

Signature  : void sendRedirect(String url)

The transfer is done by web container. While forwarding, it send a header back to the browser. The header contain resource URL to be redirected by the browser. Then the browser start a new connection.

Example : Using this example we will help you to understand the concept more easily, so what we are doing is, we have created one JSP page  named "SendredirectDemo.jsp"  in which one text box we have created and when you enter your name in that textbox then, it will redirect you to "welcom.jsp". Using the response.sendredirect() method.

Code for "SendredirectDemo.jsp"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.net.URL,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP sendRedirect</title>
</head>
<body bgcolor="#C0C0C0">
<form>
<p>Enter name :
<input type="text" name="name" />
Enter password :
<input type="password" name="pwd" />
<input type="submit" value="submit" /></p>
</form>
<%
String username = request.getParameter("name");
String password = request.getParameter("pwd");
if ((username != null)||(password!=null)) 
{
request.setAttribute("name", username);
request.setAttribute("pwd",password);
if((username.equalsIgnoreCase("RoseIndia"))&&(password.equalsIgnoreCase("RoseIndia")))
{
response.sendRedirect("Welcome.jsp");
out.println("hello"+username);
out.println("welcome"+password);
} else 
{
out.println("Hey "+ request.getAttribute("username")+ ", Sorry you cant be redirected<br>");
}
}
%>
</body>
</html>

Code  for "Welcome.jsp"

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<h3>Success.</h3>
<h4>Welcome to Rose India</h4>
</body>
</html>

When you execute this program then the output will be as follows:

After providing correct username and password, this page will redirect you to "Welcome.jsp".

When you enter wrong input then the output is as follows:

Download Source Code

Advertisements

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: July 4, 2013

Ask Questions?    Discuss: sendRedirect In JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com