Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling JavaServerPages Standard Tag Library (JSTL) in JSP

Tags:

jsp

jstl

I feel like I am missing something - from what it seems, JSP comes out of the box with support for tags, as this question's answer shows (the guy was asking a pure-jsp question and got an answer involving tags). But if I try to run the given code

<c:out value="${myString}"/>

(with myString defined before, of course), the jsp just writes the above line into the html.

Do I have to do something extra to enable it?

like image 605
olamundo Avatar asked Mar 08 '10 08:03

olamundo


People also ask

Which of the following declaration will add JSTL SQL library in your JSP?

Install JSTL Library To use any of the libraries, you must include a <taglib> directive at the top of each JSP that uses the library.

What is the use of JSTL tags in JSP?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

Which is the correct JSP tag library directive to include the standard CQ tag library?

jsp" %> directive informs the JSP compiler to include a complete file into the current file. It is as if the contents of the included file were pasted directly into the original file. With the <cq:include script="myScript. jsp"> tag, the file is included at runtime.

What is the difference when using JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).


2 Answers

JSTL support is dependent on the server/servletcontainer used. Some ships with JSTL, others don't. This is regardless of the JSP/Servlet version. Usually, normal JEE servers such as WildFly/Payara/TomEE already ship with JSTL out the box, but barebones servletcontainers such as Tomcat/Jetty/Undertow don't. For them you'll need to install JSTL yourself.

It's actually pretty simple (assuming you're using Servlet 2.5 or newer):

  1. Install JSTL as per instructions in this answer: How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved.

  2. Declare the tags in top of JSP as per this JSTL documentation (click any of the taglibs to see the declaration examples). For JSTL core it's the following:

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

That's all. If you're (still) on Servlet 2.4, then you'll need to download jstl.jar and standard.jar instead (which are part of JSTL 1.1). Remaining steps are the same (just put in classpath and declare in top of JSP).

You may notice that some poor online tutorials would suggest to extract the JAR file and clutter the webapp's web.xml with the TLD declarations. You should never do that, this is a wrong suggestion which is caused by the change in taglib URI's during the JSTL 1.0 -> JSTL 1.1 step. Instead of updating the taglib URI's in JSP, ones decided to redefine the old taglib URI's in web.xml and it became a myth.

JSP itself ships with only the <jsp:xxx> tags out of the box. These are not part of JSTL.

See also:

  • What exactly is Java EE?
  • How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved
like image 127
BalusC Avatar answered Sep 28 '22 22:09

BalusC


You need to declare the taglib at the top of the JSP:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
like image 21
skaffman Avatar answered Sep 28 '22 22:09

skaffman