Back to: JSP Tutorials for Beginners and Professionals
Page Redirection Application in JSP
In this article, we will learn How to develop a Page Redirection Application in JSP i.e. how to redirect a page with JSP. Please read our previous article where we develop the Registration and Login Form Application in JSP. It is generally used when we need to send the client to a new location. There are following two ways to redirect page with JSP:
Page Redirection in JSP By using sendRedirect()
In this method, the server returns the response to the client where the next request comes from and it displays that URL. Here, “response” (implicit object) is used to redirect the browser.
pageRedirect.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String redirectURL = "https://dotnettutorials.net/"; response.sendRedirect(redirectURL); %> </body> </html>
Output
Page Redirection in JSP By using setStatus() and setHeader()
We can use both methods together to achieve the same redirection example. This method sends the response back to the browser along with the status code and the new location.
pageRedirect.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% // New location to be redirected String site = new String("https://dotnettutorials.net"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>
Output
In the next article, I am going to discuss How to develop Hit Counter Application in JSP. Here, in this article, we develop Page Redirection Application in JSP and I hope you enjoy this Page Redirection Application in the JSP article.