Open In App

JSTL Core <c:redirect> Tag

Last Updated : 15 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JSTL – “Java Server Page Standard Tag Library” is a collection of useful JSP tags, that are the core foundation of JSP & aid in the application development. JSP technology is used to design the front-end UI of the web application, and it was developed to provide separation between Presentation logic and Business Logic. JSP can be thought of as – “Java code inside HTML”.

The JSTL <c:redirect> Tag is a part of core tags most commonly used in any Java Server Page. The core tags of JSTL are the building blocks of JSP technology that allow us to perform various functionalities such as – traversing over elements, conditional statements, import libraries, etc. In this article, we will learn about the <c:redirect> Redirection tag of the core tag library of JSTL.

Syntax for redirect tag

<c:redirect url = "URL_to_Redirect_to">

This core tag is mainly used to redirect the user to some other webpage, whose URL is specified inside the double quotes (“”).

Initial requirements: We must add the following tag, to import and use all the core tags in our code :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Example of <c:redirect> Tag

Below is the implementation of <c:redirect> tag:

HTML




<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
  <head>
      <title>Core Tag Example</title>
  </head>
  <body>
  
  <c:redirect url="http://www.example.com">
  </c:redirect>
  
  </body>
</html>


Explanation of the Above Program:

  • We created a simple HTML page, in which we have imported the JSP core tag library
  • And the <c:redirect> URL tag will redirect the user to – www.example.com
  • Note: This is a simple intermediatory redirection page, the output behavior whenever this JSP page loads on the user’s computer will be something like this (in sequence):
  1. User loads the above mentioned JSP page
  2. JSP compiler executes the <c:redirect> tag
  3. For a brief moment, while the www.example.com is loading, on browser user will see the title in tab – “Core Tag Example”.
  4. User’s brower loads the – www.example.com

Other ways to use redirect tag

1. Specifying a path

We can specify the path of the resource rather than directing it just to another web page.

<c:redirect url = "https://www.example.com/${path}" />

2. Conditional Statements

We can also add a conditional statement, and then execute the redirection tag. For example, in the below code, we first check if the user is still logged in then redirect the user to its profile page. This conditional statement is essential because it’s possible in a scenario when a user’s session is logged out then they try to go on their profile which should be prohibited.

<c:if test = "${user != null}">
<c:redirect url = "${user.profileURL}" />
</c:if>

3. With other tags like param

We can pass parameters to the URL to make it more operable. We can access the parameter through its name and extract values from it. In the below example, we gave the param name as the ‘URL’ itself, which that done for numerous reasons :

  • To easily track the redirect path, and debug
  • To allow the user to return to the page (after logging in) they were trying to access before logging in.
  • We can also pass other parameters which can be accessed anywhere else in the code.
<c:redirect url = "https://www.example.com">
<c:param name = "url" value = "${user.profileUrl}"/>
</c:redirect>

Use-cases of <c:redirect> tag

  1. Redirecting users to a different page after they log in or out
  2. Redirecting users to a different page after they complete the form submission
  3. Redirecting users to a different page if they need to update the browser, or some software to proceed further.
  4. Redirecting users if they try to access a resource or a page that doesn’t exist.
  5. Redirecting users to a different page if they try to access the resource that they’re not authorized to.

Let’s look at a more specific real-world example

Assuming the user is shopping on any e-commerce website. Let’s consider the below scenario :

Add-to-cart

Click on – Buy now

The user goes to a specific product & has the following two options – “Add to cart” and “Buy now”. If the user chooses the Add to Cart option, then the user’s product will be added to the cart, and the user can continue shopping for other products. However, if the user chooses the Buy Now option, then the user will be redirected to their cart!

This redirection is done using the – <c:redirect> tag.

Conclusion

In conclusion, the <c:redirect> tag can be used in various scenarios and is a part of core tags in the JSTL Library. These core tags are the fundamentals of JSP that assist in generating an appropriate view page.

  • JSP is a front-end technology built to separate the presentation & business logic
  • Redirection tag is a part of its core tag library
  • Some other tag libraries in JSP are formatting tags, SQL Tags, JSTL Functions, and XML tags.
  • The redirection tag that we learn about in this article redirects the user to the specified URL & the redirection process can be modified by using various other tags like <c:param>


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads