Table of contents
1.
Introduction
2.
What is Page Redirection?
3.
How does Page Redirection work in JSP?
4.
Example Code
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Page Redirection in JSP

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We'll talk about JSP page redirection today. We utilize page redirection when a document is moved to a new location, and we need to send the client to that new location. This can be due to load balance or simple randomization.

So without any further ado, let's get started!

What is Page Redirection?

You may have encountered a circumstance where you clicked a URL to go to website A but were instead taken to page B. It occurs as a result of page redirection.

There are various reasons why we would want to redirect a user away from the original page. Following is compiled list of reasons:

  • You've decided to change the name of your domain since you don't like it. In this case, you may want to refer all of your visitors to the new site. You can keep your old domain but create a single page with a page redirection so that all visits to your old domain will go to your new domain.
  • If you have many built-in pages based on browser versions, names, or different countries, you can use client-side page redirection instead of server-side page redirection to lead your viewers to the right page.
  • The search engines may have already indexed your pages. However, you don't want to lose visitors who came through search engines when switching to a new domain. As a result, a client-side page redirection is an option. Remember that this should not be done to deceive the search engine; otherwise, your site may be blocked.

How does Page Redirection work in JSP?

The sendRedirect() method of the response object is the simplest way to redirect a request to another page. This method's signature is as follows:

public void response.sendRedirect(String location)
throws IOException

 

This method transmits the answer, together with the status code and the new page location, to the browser. To achieve the same redirection example, you can combine the setStatus() and setHeader() methods:

....
String site = "http://www.newpage.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
....


You can also read about the JSP Architecture and Lifecycle.

Example Code

This example explains how a JSP redirects a page to a different location:

<%@ page import = "java.io.*,java.util.*" %>

 
<html>
   <head>
      <title>Redirection</title>
   </head>
   
   <body>
      <center>
         <h1>Page Redirection in JSP</h1>
      </center>
      <%
         // New location to be redirected
         String site = new String("https://www.codingninjas.com/");
         response.setStatus(response.SC_MOVED_TEMPORARILY);
         response.setHeader("Location", site); 
      %>
   </body>
</html>

 

Next, put the above code in PageRedirect.jsp and use the URL http://localhost:8080/PageRedirect.jsp to call this JSP. This will direct you to the specified URL, https://www.codingninjas.com.

FAQs

  1. How can one page be redirected to another page in JSP?
    For redirecting a page in JSP, we are calling response. sendRedirect(). By using this method, the server returns the response to the client, from where the next request comes, and it displays that URL. In order to redirect the browser to a different resource, we use the implicit object "response."
     
  2. What is the difference between forward and redirect in JSP?
    JSP Forward is faster than Redirect. The main distinction between forward and sendRedirect is this. When the forwarding is complete, the original request and response objects and any new parameters are transmitted. In JSP Forward, the URL remains intact.
     
  3. What is the redirection in JSP?
    We utilize page redirection when a document moves to a new location, and we need to send the client to that new location. This could be for load balance or simple randomized purposes. The sendRedirect() method of the response object is the easiest to redirect a request to another page.

Key Takeaways

So in this blog, we learn that being taken to a different location when we click a link or open a website is called Page Redirection, and it is done using sendRedirect() method in JSP.

If you haven't read our previous blog on File Uploading, then you can read it here. For more such interesting blogs, stay tuned to Coding Ninjas Blogs.
 

Happy learning!

Live masterclass