Apr 24

How to Reference and Use JSTL in your Web Application

Tag: development,glassfish,java,jboss,jsp,jstl,spring,tomcatpmularien @ 6:06 am

As a frequent contributor to the Spring Framework user forums, I have noticed a common trend among people new to Spring MVC – they really don’t understand how to use JSTL and EL in their Spring-driven JSPs.

Although Spring MVC supports flexibility in choosing a view technology, in my [back of the napkin] estimate, at least 80% of the time it is paired with JSP and JSTL. Unfortunately, since JSP was pushed out about 4-5 years ago, a lot of the information that you find on the web is extremely dated, often going back to JSTL 1.0 syntax (or, gasp, using scriptlets!). In this article I’ll clear up the confusion around how to use JSTL with various app servers and webapp versions.

Since JSP implementation and support varies widely among app server vendors (and versions of an app server), a lot of Spring MVC newbies get stuck just getting simple JSTL expressions to work. Since Spring relies on JSTL EL expressions for output of bound fields (assuming you’re not using the form taglibs), people often wrongly assume that something is wrong with Spring when their Spring-bound data doesn’t show up on the page.

Here’s a hint: if you can’t get a simple (non-Spring-related!) expression like ${2+2} to work, no expressions will work! (In a properly functioning servlet container, the prior expression should output “4″ on the page).

I set out to take some common application server configurations, combine them with various flavors of JSP/JSTL support, and see what happened.

The Importance of Servlet Version and web.xml

Let us review the following reference table:

JSP/Servlet Version

Servlet Version JSP Version JSTL Version Java EE Version
2.5 2.1 1.2 5
2.4 2.0 1.1 1.4
2.3 1.2 1.0 1.2

What Does this mean to me?

The most important thing is to figure out what version of the Java EE web stack (Servlet/JSP) you are using. There are 2 aspects that factor into this:

  • What version of Java EE / servlet spec does your servlet container support?
  • What version of Java EE / servlet spec have you declared in your deployment descriptor (web.xml)?

Here’s an example of what to look for in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  id="WebApp_ID" version="2.5">
  <display-name>web-app-25</display-name>
...
</web-app>

You can see the ‘version=”2.5″‘ designation in here. This means that within this web application, we will be able to use JSP 2.1 and JSTL 1.2 features.

OK, How do I use JSTL in my Page?

A very common problem that I have seen with new Spring users is that they don’t understand how to reference the JSTL tag libraries on their pages. Important!: You need to identify the version of web application you are using first.

Web Application v2.5 and v2.4

To use EL Expressions: You do not need <c:out>. Simply insert EL expressions onto the page: ${2+2}
To use JSTL tag libraries (c, fmt, etc): Reference as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Web Application v2.3

To use EL Expressions: You do need <c:out>. Raw EL expressions on the page will not work. e.g. <c:out value=”${2+2}”>
To use JSTL tag libraries (c, fmt, etc): Reference as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/core" %>

What about the _rt Taglibs Like core_rt?

The following type of URI will also work, in JSTL 1.2 and 1.1:

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

However, this is not desired. You should never have to reference the _rt versions of taglibs (e.g. core_rt).

Do I need to include a JSTL Implementation with my Web Application?

Obviously, there are a lot of application servers out there. I tested this with the following:

  • Tomcat 6.0
  • Tomcat 5.5
  • Tomcat 5.0
  • JBOSS 4.2
  • Glassfish 2

Of these, JBOSS and Glassfish ship with JSTL implementations out of the box. Tomcat does not ship with a JSTL implementation. I have previously blogged about this here.

I did not test other application servers, simply because these are the ones I most commonly see referenced in the Spring forums. Websphere is also used, but I didn’t have access to it (and, frankly, didn’t want to spend the 8 hours and tens of gigs of downloads it would take to install it ;) ).

Testing Methodology

For those who are interested, here’s the testing methodology I used to come to the conclusions above.

I created 6 web applications. The 6 web applications are as follows:

  • webapp-25: Servlet version 2.5 declared in web.xml, JSTL RI not included in WEB-INF/lib
  • webapp-25-jstlri: Servlet version 2.5 declared in web.xml, JSTL 1.2 RI included in WEB-INF/lib
  • webapp-24: Servlet version 2.4 declared in web.xml, JSTL RI not included in WEB-INF/lib
  • webapp-24-jstl11: Servlet version 2.4 declared in web.xml, JSTL 1.1 RI included in WEB-INF/lib
  • webapp-23: Servlet version 2.3 declared in web.xml, JSTL RI not included in WEB-INF/lib
  • webapp-23-jstl10: Servlet version 2.3 declared in web.xml, JSTL 1.0 RI included in WEB-INF/lib

In each web application, I created 4 JSP pages with the following content:

		Some simple math: ${2+2}
		<br/>
		Some simple math with c:out: <c:out value="${2+2}"/>
		<br/>
		Some simple math with c2:out: <c2:out value="${2+2}"/>
		<br/>

You can see there are 3 tests in the page. The goal of the tests are as follows:

  1. ${2+2}: does inline EL evaluation work with this webapp version?
  2. <c:out value=”${2+2}”/>: is the ‘c’ namespace automatically provided by the container?
  3. <c2:out value=”${2+2}”/> is the ‘c2′ namespace explicitly provided by the given taglib declaration? (see next section for how the c2 taglib is declared)

For each of the 4 JSP pages, I varied how the JSTL core taglib was declared:

  • test_no_taglib_decl.jsp: Contained no taglib declarations at all
  • test_c2_jsp_jstl_core_taglib_decl.jsp: Contained the taglib declaration:
    <%@taglib prefix="c2" uri="http://java.sun.com/jsp/jstl/core" %>
  • test_c2_jstl_core_taglib_decl.jsp: Contained the taglib declaration:
    <%@taglib prefix="c2" uri="http://java.sun.com/jstl/core" %>
  • test_c2_jstl_core_rt_taglib_decl.jsp: Contained the taglib declaration:
    <%@taglib prefix="c2" uri="http://java.sun.com/jstl/core_rt" %>

In a typical scenario where the container supports JSP 2.0+, what you would expect to see is the following:

Some simple math: 4
Some simple math with c:out:
Some simple math with c2:out: 4

What Happens if You Have the Wrong Declarations

Some of the errors you may get if you don’t have things declared right:

On Tomcat
Declaring the wrong taglib:

org.apache.jasper.JasperException: /test_c2_jstl_core_taglib_decl.jsp(11,32) According to TLD or attribute directive in tag file, attribute value does not accept any expressions
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)

No JSTL implementation:

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)

On JBOSS

Since JBOSS also uses the Apache Jasper JSP compiler, the errors are basically exactly the same as those listed above.

On Glassfish

org.apache.jasper.JasperException: /test_c2_jstl_core_taglib_decl.jsp(11,32) PWC6236: According to TLD or attribute directive in tag file, attribute value does not accept any expressions

Related Reading

I referenced the following sources when researching this entry:

Unfortunately, as I’ve mentioned in the beginning of this article, there aren’t a lot of articles that I have found specifically covering JSP that have been written recently.

42 Responses to “How to Reference and Use JSTL in your Web Application”

  1. Sekar says:

    Thank you. Very good description. I have burned so many hours trying to fix such errors as I migrated from one servlet container to another.
    Sekar.

  2. lumpynose says:

    Thanks for the writeup; I’ve also pulled out too much hair trying to understand the different versions. And suffered with the ancient writeups and books.

    Is there a 1.2 version of this page:

    http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html

    I sometimes use this page to refresh my memory (it’s a starting point in their tutorial):

    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html

    The one recurring problem that I have is that it seems that I ought to be able to do some simple templating system with jstl, common headers and footers for pages, with substitutable elements, for example, but haven’t had much success.

  3. millerjam :: links for 2008-04-25 says:

    [...] It’s Only Software » How to Reference and Use JSTL in your Web Application (tags: java jstl jsp) [...]

  4. Alex says:

    My biggest frustration with JSTL is it’s semi-abandoned state. There are some heavy-ugly JEE stacks like GF and JBOSS that support it out of the box, but lighter (and more useful) ones like Tomcat and Jetty — don’t even have instructions on how to set JSTL 1.2 up properly. Moreover, JSTL 1.2 jars are nowhere to be found.

    This is what I did to make Tomcat 6 to “like” JSTL 1.2 — you basically take appserv-jstl.jar (from either GF or JEE /lib) and “trim” the javaee.jar to only have javax.servlet.jsp.jstl package (otherwise you risk running into conflicts with other JEE crap-libs in Tomcat) and by including these into web app — you’ll have JSTL 1.2. EL is already in Tomcat.

    On the side note this is of course a dirty hack. I don’t have too much JSTL code yet so I might be up for some unpleasant discoveries very shortly. On the side note I think JSTL is the best thing that happened to JSP — a compromise between simplicity and functionality that makes PHP/Ruby bleak. Look how many custom tag libs spawned on Apache when it was introduced! I have no idea why community and Sun suddenly lost interest and not maintaining this technology anymore. Struts/Spring being “golden handcuffs” and JSF with its complexity being the “EJB for the web” — Looks like we’re going back to plain old JSPs being the only thing you can get some actual work done with (that involves a little more than CRUD functions).

  5. Matt says:

    I agree, there isn’t any quality documentation on the web regarding actual implementations. I would be interested to see your tests performed with Unified Expression Language. I’ve seen example where they claim you can place #{} references anywhere on the page, but whenever I try that I get an error about including deferred values in template text. I’m using servlet 2.5, JSP 2.1 (apparently glassfish 2 supports this, but this error doesn’t seem to indicate that) and JSTL 1.2. Looking at glassfish I found multiple jars containing JSTL implementations and the TLD doesn’t have tag atributes that can handle deferred values as per the examples. Also, accessing the nested variable created by the forEach loop isn’t accessible using #{} syntax as a number of examples claim. the loop does however iterate the correct number of times with #{} in the items attribute. It appears I have the correct software stack in place, but am now re downloading containers etc to work out is anything has been added to them by my dev tools that have messed with the implementations used.
    I was originally trying to use JSF managed beans with the JSTL loop tag, but even with plain JSPs have access issues.
    If you have experience with the JSP 2.1 EL #{} it would be good to know if you’ve had success with placing it in template text and also using with

  6. Roberto says:

    Thank you very much for posting this information. I had been banging my head trying to figure out the issue with expression language. Now that I’ve read this and doing some testing like what you did, I’ve figured where the incompatibility problem was. Thanks!!!

  7. Joakim Andersson » Blog Archive » How to Reference and Use JSTL in your Web Application says:

    [...] » How to Reference and Use JSTL in your Web Application – It’s Only Software.   [...]

  8. charles lane says:

    Quick clarification? what did you mean by this “Unfortunately, since JSP was pushed out about 4-5 years ago, a lot of the information that you find on the web is extremely dated, often going back to JSTL 1.0 syntax (or, gasp, using scriptlets!).”

    I do find that the books that I read sometimes feel out of step with the developer forums. I am trying to raise my proficiency at J2EE development and am focusing on JSP. The other widely used presentation tool seems to be ASP, which I am allergic to.
    Do you think JSP is on the rise? Or on the decline? I really was not sure how to take your comments.
    I also get the impression that nobody refers to “EL”, it being merged or overtaken by JSTL. Is that correct?
    Thanks! Charles

  9. Matt says:

    Thanks for the article. None of my EL expressions were evaluating. Turns out it was the web-app tag in the web.xml.

  10. Bruno ARLIGUY › S’y retrouver dans les numéros de versions de Java / JEE / JSP / JSTL says:

    [...] compliqué de s’y retrouver dans toutes ces versions, alors voilà un résumé extrait de cet article intéressant sur [...]

  11. chris says:

    Thankyou so much for this info! I can’t believe they changed the url, because there are so many websites that still say to use the old URL and it caused me so many issues.

  12. Eric P says:

    The JSTL jars are in the main Maven repo.
    http://repo2.maven.org/maven2/javax/servlet/jstl/

  13. Amith says:

    Really helped in fixing my problems with JSTL. My application server was using JSP 2.4 and i was using an incompatible version of JSTL. The table you have come up with is helpful. You saved me a lot of time with this well written information. I appreciate your time and effort. Thanks very much. Keep it up :) !

    Cheers,
    Amith Nambiar

  14. Kjetil says:

    Using raw EL expressions may lead to XSS vulnerabilities in your application.

    http://www.owasp.org/index.php/J2EE_Bad_Practices:_JSP_Expressions

  15. Tsetso says:

    Hi Peter,

    Thank you for this post. It helped me to fix my prblem :)

  16. Daily Relevance says:

    [...] Here are some posts that I used to come to this much needed conclusion.  The second post being more helpful than the first due to the fact that much of Spring Security 3′s book examples use eclipse and tomcat. http://www.mularien.com/blog/2008/04/24/how-to-reference-and-use-jstl-in-your-web-application/ [...]

  17. Shashank says:
  18. Dave says:

    Great article. Well done!

  19. Playing with Jetty, Part One « Bit Motif says:

    [...] a Web Application Context Tip: Enabling JSP support in Jetty.7.0.0 How to Reference and Use JSTL in your Web Application Tutorial: How to set up Tomcat 6 to work with JSTL [...]

  20. JT says:

    I followed your blog but still I am getting the below error message. I followed your other blog post and included appserv-jstl.jar and javaee.jar in my tomcat/lib directory. In the ant file I have included this as part of my classpath. I don’t know what’s happening. Your suggestions would be really appreciated.

    “org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application”

    Technologies working with: GWT + JSP + JSTL + Tomcat6

  21. Amit says:

    It helped me understand the issue. Got eresolved by user core_rt taglib

  22. Peter says:

    You clearly made an excellent job making this easier for everyone. I now have a better understanding of how to use JSTL and EL after reading your post. Thanks!

  23. JSTL c:set problem says:

    [...] (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}document.write(''); Have a read here. The URI is dependent on the version of the library you are using (see link). It's a marker, not a [...]

  24. Svante says:

    Hi,

    it seems you mixed up some namespace and location in the root element wrong, try this:

    Cheers,
    Svante

  25. Porridge says:

    Well, I have Spring 3, Tomcat 7, downloaded the JSTL1.2 libs from the Glassfish dev.java page and all your tests work with basic arithmetic. However, what this article does not cover, surprisingly as it talks about Spring, is how vars put into ModelAndView come through to JSTL. Whilst my JSTL expressions are working, I cannot output a String passed in a ModelAndView. I have used JSTLView as the viewClass on the view resolver, but not luck. Totally stuck :(

  26. Brian says:

    Thank you, I put the to jar files in the Tomcat lib directory and the JSTL is no longer erroring out.

  27. David says:

    A question from a different angle: we’ve moved from a web application with servlet 2.5 and jstl 1.1.2 and tomcat 5.5 to tomcat 6.0.35. Some EL stopped working. Especially, some of them have turned to come out UPPER CASE. Anyone has met this weird behavior? Thanks, David

  28. » Back to bascis of JSTL Room Work Blog says:

    [...] plupart de mes informations proviennent de ce post : http://www.mularien.com/blog/2008/04/24/how-to-reference-and-use-jstl-in-your-web-application/ « Gitblit : premier [...]

  29. Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core” « rspydir says:

    [...] more information, see http://www.mularien.com/blog/2008/04/24/how-to-reference-and-use-jstl-in-your-web-application/ Like this:LikeBe the first to like this post. Posted by rspydir Filed in Uncategorized Leave a [...]

  30. Jan Hruska says:

    Hello i have problem with my jsp file, it is almost the same like you have here but i have in code:

    tolog is a languege using in query in XTM and LTM files, when i put this into my code it after shows that error:

    The absolute uri: http://psi.ontopia.net/jsp/taglib/tolog cannot be resolved in either web.xml or the jar files deployed with this application

    i dont absolutely know what to do, please help

  31. Jan Hruska says:

    Hello i have problem with my jsp file, it is almost the same like you have here but i have in code:

    <%@ taglib uri='http://psi.ontopia.net/jsp/taglib/tolog' prefix='tolog'%>

    tolog is a languege using in query in XTM and LTM files, when i put this into my code it after shows that error:

    The absolute uri: http://psi.ontopia.net/jsp/taglib/tolog cannot be resolved in either web.xml or the jar files deployed with this application

    i dont absolutely know what to do, please help

  32. Baruch Atta says:

    IS THE ONLY PLACE ON THE WEB TO GET THE JSTL JAR ?
    http://repo2.maven.org/maven2/javax/servlet/jstl/1.2/

    I have been searching for DAYS for this.
    When i use the tag

    the link is broken. It redirects to
    http://www.oracle.com/technetwork/java/index-jsp-135995.html
    which has the link
    https://jstl.dev.java.net/
    which is broken.
    No where else on the web have I found this, and I have been looking for days.
    WHAT GIVES?

  33. Baruch Atta says:

    Ok i have calmed down a little bit.
    The tag did not appear in my previous post.
    %@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %
    New question: This tag caused the page to error out. By adding the jstl.jar to my Tomcat lib folder, the link now works??????? amazing. I hate it when the error message given has nothing to do with the actual problem.

  34. Nick Larsson says:

    I can verify that this also works with websphere 8. I tried for loops with EL and running methods on object that I had to attach to the request object in either a preprocessing servlet or preceding scriptlet. My development environment is IBM RAD 8.

  35. JavaBeans erstellen und in JSP benutzen › Der Blog von Benny Neugebauer says:

    [...] Links: How to Reference and Use JSTL in your Web ApplicationRelated Links:No Related Links Ähnliche Artikel: Java Singleton [...]

  36. Dave Whaley says:

    Thank you = very useful!

  37. James Reynolds says:

    Thanks, was trying to show someone how to do a basic servlet example passing information to a JSP and then problems with version starting causing issues. Reading this managed to sort my problem so I could get back to the point. Cheers, helped a lot!

  38. java training in chennai says:

    version reference table is very useful. Thanks for sharing.

  39. Java EE & Spring Cheatsheet | Gerry's Blog says:

    [...] this blog post for more [...]

  40. Johnchi says:

    These two links to the central maven repo are to the folders just above the latest versions (1.2), so they might stay current a bit longer, and you can easily see the versions available

    API
    Implementation

    So if you wanted 1.2.1 then you’d download:
    javax.servlet.jsp.jstl-api-1.2.1.jar from the API folder
    javax.servlet.jsp.jstl-1.2.1.jar from the Implementation folder

  41. Java EE & Spring Cheatsheet | Otherwise I'll Forget says:

    […] this blog post for more […]

  42. Rick Grover says:

    You seem to be knowledgeable and I thank you for creating this page, however, it lacks a complete date. We know you are an early riser by the time stamp of 6:06 am. We know it was written April 24th, but what year? Mentioning Java 1.2, 1.4 and 5, I suspect it is several years old. Is the info still valid?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>