<c:url> JSTL tag is used for url formatting or you can say url encoding. This is mainly used when we need to open a JSP page based on the user input or based on the value of a variable. It basically converts a relative url into a application context’s url. It may sound confusing now but follow the given examples in this tutorial and you will be able to grasp it quite easy.
Syntax:
Basic syntax looks like this – The attribute “value” is a required attribute for the <c:url> tag
<c:url value="/file1.jsp" />
There are three other optional attributes exist for this tag which are as follows –
- var: Variable name to store the formatted url (resultant url).
- context: Used for specifying the application (or project name). Don’t get it? We will see this with the help of an example later.
- scope: The scope in which the var attribute would be stored. It can be request, page, application or session.
Let’s understand the use of this tag and attributes with the help of an example –
Example 1: value attribute of <c:url>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>JSTL c:url Tag Example</title> </head> <body> <c:url value="/file1.jsp"/> </body> </html>
Output: The above code produced below output. Note: BeginnersBook is my project name (in other words JSP application name).
/BeginnersBook/file1.jsp
Example 2: var attribute of <c:url> tag
Let’s modify the example 1 like this. We have added a variable myurl in <c:url>. Now the result of <c:url> would be stored in variable myurl.
<c:url var="myurl" value="/file1.jsp"/> ${myurl}
Output:
/BeginnersBook/file1.jsp
Example 3: context attribute
By default this tag takes the current application as the context, however we can explicitly specify the context in c url as shown in the below example –
Note: The value of the context should always starts with “/” otherwise you will get the below exception message –
HTTP Status 500 – javax.servlet.ServletException: javax.servlet.jsp.JspTagException: In URL tags, when the “context” attribute is specified, values of both “context” and “url” must start with “/”.
<c:url var="myurl" value="/file1.jsp" context="/MyJSPProject"/> ${myurl}
Output:
/MyJSPProject/file1.jsp
Example 4: scope attribute
<c:url var="myurl" value="/file1.jsp" context="/MyJSPProject" scope="session"/> ${requestScope.myurl}
Output: Output screen will be blank as we have stored the myurl in session scope and we are trying to print the value after fetching from requestScope.
Correct usage: This is how it should be coded. Here we have stored in session and fetching from sessionScope so it would work fine.
<c:url var="myurl" value="/file1.jsp" context="/MyJSPProject" scope="session"/> ${sessionScope.myurl}
Output:
/MyJSPProject/file1.jsp
Pedro Cardoso says
Hi, its a nice work this site.
I’m a beginer with jsp and javaee deveop and I find very useful this posts.
I have a question, about the use of c:url, I found a situation in which c:url is not working, in the next code:
<img src="” alt=”Fotografia” class=”img-thumbnail img-responsive” title=”Fotografia” />
The c:url does not add the context of my app, I am using c:url in a lot of other places in my views and it works perfectly just here not, and the only difference is the value comes from a property of a bean. Is there an special thing I have to do in this case or am I doing anything worng?