beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

JSTL <c:redirect> Core Tag

By Chaitanya Singh | Filed Under: JSTL

<c:redirect> is used for redirecting the current page to another URL.

Syntax:

<c:redirect url="http://www.anydomainhere.com/samplepage.jsp"/>

This is how the <c:redirect> tag looks like. We just need to provide the relative address in the URL attribute of this tag and the page will automatically be redirected the URL provided when it gets loaded.

Example

Here we are redirecting the page to a different url based on the value of the variable myurl. If the value is 1 page will be redirected to http://beginnersbook.com and for 2 it will go to http://www.google.com.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> JSTL c:redirect Tag Example</title>
</head>
<body>
  <c:set var="myurl" value="2" scope="request"/>
  <c:if test="${myurl<1}">
     <c:redirect url="http://beginnersbook.com"/>
  </c:if>
  <c:if test="${myurl>1}">
     <c:redirect url="http://www.google.com"/>
  </c:if>
</body>
</html>

Output: Since the value of the variable myurl is 2 the page gets directed to the http://www.google.com.

Enjoyed this post? Try these related posts

  1. fn:indexOf() – JSTL Function
  2. JSTL <c:remove> Core Tag
  3. JSTL <c:url> Core Tag
  4. JSTL <c:if> Core Tag
  5. JSTL <c:import> Core Tag
  6. fn:join() and fn:split() JSTL Functions

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap