JSTL core tag redirect Example

JSTL core tag redirect Example


Posted in : Java Posted on : April 1, 2012 at 7:41 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL core redirect tag.

JSTL core tag redirect Example

In this tutorial you will learn about the JSTL core redirect tag.

<c:redirect>  tag in jstl facilitates you for redirecting the browser to the specified url, url may either absolute or relative.

Attributes of <c:redirect> tag

  • url : This is an required attribute that is used for specifying either the relative or absolute url.
  • context  : This attribute is used for specifying the web application which is followed by '/'. Default value of this attribute is a current application.

If the url attribute is specified by an absolute url then the browser will make an additional request to the new url and if the specified url is a relative url then the browser will show the original url in its address bar.

This tag also supports the <c:param> tag.

Example :

In the example given below will demonstrate you how to use the <c:redirect> tag in jsp. Here I have uses both types of url i.e absolute and relative urls. In an absolute type of url I have used the address outside the context which also contains the <c:param> tag using which the browser will make a new url and in the relative url I have used the url of the same context it will display the original url.

JstlCoreTagRedirectExample.jsp

<%@ 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">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag redirect Example.</h3>
<!-- Absolute URL redirecting -->

<%-- <c:redirect url="http://www.google.com/search"> --%>
<%-- <c:param name="hl" value="en"/> --%>
<%-- <c:param name="q">devmanuals</c:param> --%>
<%-- </c:redirect> --%>

<!-- Relative URL redirecting -->
<c:redirect url="JstlCoreTagSetExample.jsp"/>
</body>
</html>

How to Run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • At first I have changed the preference of web browser from eclipse 'internal web browser' to 'firefox' because its internal browser will unable to open the requested page.
  • Select JstlCoreTagRedirectExample.jsp page of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlCoreTagRedirectExample.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully an output will be displayed on the firefox web browser as :

Following output is of the relative url redirecting.

Following output is of the absolute url redirecting

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics