Updated April 4, 2023
Introduction to JSP Redirect Method
In Java Server Page(JSP) we want to navigate from one page into another page by using some jsp predefined tags redirect is one of the options for reloading the home page or whatever we send the request page and waiting for the response page it will again go to the request page whenever the request authentication is failed for the client request. The page redirect is used to move the redirect response to another resource of the web page. Basically we can call the redirect pages using sendRedirect() method in the jsp on client-side request can be used within the server or outside of the server.
Syntax:
<html>
<body>
<%
------jsp logic codes---
Call the response method like response.sendRedirect("webpage with extension(.html,.jsp");
----- %>
</body>
</html>
The above codes are navigating the client and server interactions using the jsp redirect method. We can also redirect the web page using string objects like string s=new string(“”) call the default response methods like response.setStatus() and response.setHeader() these consecutive methods will be called certain conditions whenever we use the appropriate methods in the jsp.
How Redirect Method Works in JSP?
Jsp we cannot send the request like the post and get methods by using sendRedirect method simply calling we will approach the RequestDispatcher technique to call the forward() method in the jsp requests parameters are same in the web application with the same context generally requestdispatcher using getRequestDispatcher() method for calling the servlets.
Generally jsp use the two different types of approaches like 1.Request forward and 2.Request redirect. The request forwards the option to control and forward the request available inside the web applications. If we use the response.sendRedirect() method the server will return back to the user request-response until wait for the next request comes and it shows using web browser URLs. The Request forward from one jsp into another jsp is in the same part of the web application the transfer of control is fully done by the web containers internally and the browsers or client is not fully involved in the whole application process.
The Request redirect approach is needed for control in the outside of the web application containers. The redirect does not work in the same application whenever the request is sent by the user but wherever the application machine is in idle that time the approach is access to the controls for accessing different domains in the same machine. Redirection is one of the types of responses is sent back from the client to the server browsers that they have own instruction for fetching web pages the address will be changed in the URL browsers.
The page redirection is also same as redirection whenever the web document or pages or any files are to be deployed in the servers regarding file transfers from one location into another location that time we can maintain the load balancer in the servers regarding more number of users accessing the web page at the same time. In the sendRedirect() method after the user sent the request wherever in the client machine it goes to the web container the container only decides whether the request will go to the particular servlet it can handle the request or not. If it is not then the servlet will decide the request will move to the other servlet or jsp or any other extension files based on the project requirements.
After receiving the request the servlet or jsp calls the sendRedirect() method will use for sending the response object it also moves to the response to web browsers along with the status codes. The user browsers will saw the particular status codes and it looks whether the request and response resources will handle them or not else the browser will send again the new request but the request name and then the resources and the output result will be shown in the web browsers. But this process are unable to identified in the client machine it knows only the performance of the web applications and then the net result will be shown or not in the browser.
Examples to Implement of JSP Redirect
Example #1
Code :
<html>
<body>
<form action="custom.jsp" method="POST">
Name: <input type="text" name="name"><br />
City: <input type="text" name="city" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Page Redirect Example</title>
</head>
<body>
<%
String name = request.getParameter("name");
String city = request.getParameter("city");
if(name.equals("siva") && city.equals("tup"))
{
response.sendRedirect("welcome.html");
}
else
{
response.sendRedirect("error.html");
}
%>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success</title>
</head>
<body>
<h2>Welcome To My Domain</h2>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error</title>
</head>
<body>
Please Try Again
</body>
</html>
Output:
Example #2
Code:
<%@ 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>
<body>
<%
String site = "https://www.facebook.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
response.sendRedirect(site);
%>
</body>
</html>
Output:
Example #3
Code:
<html>
<head>
<script type = "text/javascript">
function sample() {
window.location = "https://www.facebook.com";
}
document.write("The page is redirected in 10 seconds.");
setTimeout('sample()', 10000);
</script>
</head>
<body>
<p>Click the button, is automatically redirected to home page.</p>
<form>
<input type = "button" value = "click" onclick = "sample();" />
</form>
</body>
</html>
Output:
The above three examples we will use the redirect option in the three different styles in the jsp first one normal user authentication after validation, second example basic jsp codes for redirection in the same web page after server response its call the sendRedirect method() and a final example using javascript function is called after automatically redirect the web page without using redirect method.
Conclusion
In conclusion, jsp will navigate the web pages by using a different set of tags like redirect, forward, and include. Each tag have their own implementations and functionalities for the jsp applications forward and include behaves the same feature and workflow in the web application but whenever we receive the error response from the server is loaded the same page but in a redirect, it specifies automatically in the home page.
Recommended Article
This is a guide to JSP Redirect. Here we discuss the Introduction and how to Redirect Method Works in JSP along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –