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 tag of JSTL Core tag library

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.

createUrl.jsp

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<c:url value="displayValues.jsp" var="displayURL">
<c:param name="name" value="${param.name}" />
<c:param name="age" value="${param.age}" />
<c:param name="gender" value="${param.gender}" />
</c:url>
<html>
<head>
<title>c:url CoreJstl Tag</title>
</head>
<body>
<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">
<table>
<tr>
<td>Enter name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter age:</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Enter gender:</td>
<td><input type="text" name="gender" /></td>
</tr>
</table>
<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>
</c:if>
</body>
</html>


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