Share on Google+Share on Google+

Create URL using tag of JSTL Core tag library

Here in this example we are going to create a url according to the user's given parameter by using tag.

Create URL using <c:url> tag of JSTL Core tag library


Here in this example we are going to create a url according to the user's given parameter by using <c:url> tag.







<C:url> : This is a member of JSTL core tag library and used to create url with some optional parameters.
Attributes of the tag <c:url>  
var  Declare a scoped variable that stores the url.
scope  Define the scope for declared variable like page or request or session or application.
value  Value attribute define a String that is used to specify a url that will process as url later.
context  We can provide context name as attribute that is prepended with the url. For this url and 
 context must start with forward slash. If url is not started with forward slash, it is assumed 
 to be a relative url, in this situation context name is unnecessary.

We have also used tag <c:param> to add parameters to the specified url. Attribute of this tag are name  that is used to provide a name to parameter and value
is used to initialize parameter of this name.

In the code given below, first we will create a web page having three text fields for name, age and sex. When user enters all these information and click on submit button then <c:url> tag calculate the url with the given paramaters and show the calculated url  in the same web page.


<%@ taglib uri="" prefix="c"%>
<c:url value="displayValues.jsp" var="displayURL">
<c:param name="name" value="${}" />
<c:param name="age" value="${param.age}" />
<c:param name="gender" value="${param.gender}" />
<title>c:url CoreJstl Tag</title>
<font size="4" color="green">This page takes 3 values that user entered
in the text box given <br>
below and when click on submit button shows url with given parameters.</font>
<form method="post">
<td>Enter name:</td>
<td><input type="text" name="name" /></td>
<td>Enter age:</td>
<td><input type="text" name="age" /></td>
<td>Enter gender:</td>
<td><input type="text" name="gender" /></td>
<input type="submit" value="submit" /></form>
<c:if test="${pageContext.request.method=='POST'}">
Here application receives the values that you have submitted, 
<br>and creates a URL that contains
these values.
<p /><strong>URL is </strong><font size="3" color="red"><c:out
value="${displayURL}" /></font>

Steps to run this example :

1:  Download the zip file of code and unzip this file, you will get a folder named  'catch_exception_jstlCoreTag'.
2:  Paste this folder in 'Apache Tomcat 6.0.16-->webapps' or generally in directory 'C:\apache-tomcat-6.0.16\webapps'.
3:  Start tomcat server by click on startup.bat file in 'C:\apache-tomcat-6.0.16\bin'.
4: Open browser and type url 'http://localhost:8080/create_url_jstlCore/createUrl.jsp' or click on this link.

Output of the program

When user clicks on submit button, response will be...

Download Source Code



Posted on: August 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create URL using tag of JSTL Core tag library  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
surendra s savadekar
December 11, 2011

i am a student to be an online employer i have a url or domain name for any work please help me i have start work today. thank you. yours faithfully surendra s savadekar.