JSTL Core Tag url Example

JSTL Core Tag url Example


Posted in : Java Posted on : April 2, 2012 at 7:27 PM Comments : [ 0 ]

In this example you will read about how to use the JSTL core URL management tag.

JSTL Core Tag url Example

In this example you will read about how to use the JSTL core URL management <c:url> tag.

<c:url> tag is used to create an url. It is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page. <c:param> tag may used as a subtag of <c:url> to add the parameters in the returned URL. Using these parameters <c:url> encodes the URL.

Attributes of <c:url> tag

  • var : This attribute is used for providing a name of variable using which a processed URL can be accessed.
  • value : This is a required attribute to provide a URL.
  • context : This attribute is used for specifying the name of a web application.
  • scope : This attribute is used for specifying the scope of a accessible URL.

<c:url> tag automatically rewrite an URL. An automatically rewriting process of URL is perform when the JSP container detects that the cookies is not storing the users current session ID. The rewritten URL will encode the session ID however, it is not necessary

Example :

Here I am going to give a simple example which will demonstrate you how to use the <c:url> tag. In this example I have used the <c:url> tag to rewrite the url. Here I have also used the <c:param> to add the parameter in the url. In case of if the client has not disable the cookies then <c:url> tag will create the url with the help of these param values and/or may include session ID and if the client has disable the cookies then an url will be created with the session ID and or may include the parameter values.

JstlCoreTagURLExample.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 url Example.</h3>
<c:url value="a.jsp" var="myURL">
<c:param name="name1" value="Dev"/>
<c:param name="name2" value="Manuals"/>
</c:url>
<p>Generated URL = ${myURL}</p>
</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 :

  • Select JstlCoreTagURLExample.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlCoreTagURLExample.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 -> Run On Server -> 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 browser as :

1. When cookies has disabled by the client then the URL will be created including the session ID

2. When cookies has not disabled the client then the URL will be created using the parameter.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics