Pr.Pg Next Pg

Page Redirection tutorials

  • Page redirection is generally used for moving to new page or location with new url address.

  • sendRedirect() method of response object provide the simple mechanism for redirecting the request to new page.

  • sendRedirect() method redirect the response to another resource. sendRedirect() method actually makes the client(browser) to create a new request to get to the resource. The client can see the new url in the browser.

 

 

JSP program of page redirection using sendRedirect

Sam Sir

<%-- Program name Redirect.jsp --%>

<%-- JSP program of page redirection using sendRedirect --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h4>Redirect Demo</h4>

<%

response.sendRedirect("http://www.google.com");

%>

</body>

</html>

Output

page redirect

 

 

JSP program for processing the link coming from link.html

Sam Sir

<%--JSP program name linkprocess.jsp--%>

<%-- JSP program for processing the link coming from link.html --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

String google=request.getParameter("go");

String yahoo=request.getParameter("ya");

String oracle=request.getParameter("or");

String location=request.getParameter("custom");

String link=request.getParameter("link");

if(link.equals("Google"))

{

response.sendRedirect("http://www.google.com");

}

if(link.equals("Yahoo"))

{

response.sendRedirect("http://www.yahoo.com");

}

if(link.equals("Oracle"))

{

response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);

response.setHeader("Location","http://www.oracle.com");

}

if(link.equals("Open"))

{

response.sendRedirect("http://"+location.toString());

}

%>

</body>

</html>

 

 

 


 

HTML program for link input

Sam Sir

<!—HTML program name link.html -->

<!—HTML program for link input -->

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Redirect</title>

</head>

<body>

<center><h4>Page Redirect Demo</h4></center>

<hr>

<form method="get" action="linkprocess.jsp">

<table>

<tr>

<td><label id="go">Google :</label> </td>

<td><input type="submit" name="link" value="Google"></td>

<td>Page linked using sendRedirect</td>

</tr>

<tr>

<td><label id="ya">Yahoo :</label></td>

<td><input type="submit" name="link" value="Yahoo"></td>

<td>Page linked using sendRedirect</td>

</tr>

<tr>

<td><label id="or">Oracle :</label> </td>

<td><input type="submit" name="link" value="Oracle"></td>

<td>Page linked using setHeader</td>

</tr>

</table>

<hr>

<h3>Enter Link to Open the Site</h3>

<input type="text" name="custom"><br>

<input type="submit" name="link" value="Open">

</form>

</body>

</html>

Output

send Redirect

 

Send redirect

Pr.Pg border                                              Next Pg