In this tutorial you will learn about how to use the sendRedirect() in JSP.
JSP Response sendRedirect
In this tutorial you will learn about how to use the sendRedirect() in JSP.
Method of HttpServletResponse sendRedirect() is used to redirect the user of the current page to the other page. In this process the control of user is transferred from the current location to the another location using the specified URL and clears the buffer. Other location could be located on a different server or different context.
Syntax :
void sendRedirect(java.lang.String location)
Argument of this method is a specified relative URL.
Example :
An example that is being given here will help you in how to use the sendRedirect() method. In this example at first I have created JSP pages "jspResponseSendRedirect/jspResponseSendRedirect.jsp" where designed a form to take the input by the user. Then wrote the java code using the scriptlets to check for the name (here I have hard coded the name=dev) if this statement returns true the control will be redirected to the another jsp page which is created in the "jspResponseContainsHeader/welcome.jsp" i.e it is contained by the another context or it will return some messages.
/jspResponseSendRedirect/jspResponseSendRedirect.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 Response sendRedirect</title> </head> <body> <form> <table> <tr> <td>Enter name :</td> <td><input type="text" name="name" /> </td> </tr> <tr> <td></td> <td><input type="submit" value="submit" /> </td> </tr> </table> </form> <% String name = request.getParameter("name"); if (name != null) { // resource will be moved to another location request.setAttribute("name", name); if (name.equals("dev")) { response.sendRedirect("/jspResponseContainsHeader/welcome.jsp"); } else { out.println("Hey " + request.getAttribute("name") + ", you can't be redirected.<br>"+ "There is a problem to fill up the"+ " name in text box.<br> Name must be the 'dev'."); } } %> </body> </html>
/jspResponseContainsHeader/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>Welcome, You have been successfully redirected.</h3> </body> </html>
Output :
When you will execute the jspResponseSendRedirect.jsp following output will be displayed :
1. At first a page will opened to you to enter the value as follows :
2. When you will give the value as above and will click on submit button then you will be redirected to the /jspResponseContainsHeader/welcome.jsp as follows :
3. But when you will give the value other than the "dev" then like as follows :
4. And you will clicked on submit button then the output will be as follows :
Download jspResponseSendRedirect/jspResponseSendRedirect.jsp
[ 0 ] Comments