tags:

views:

157

answers:

2

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?

+1  A: 

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

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
skaffman
Thanks! this looks like a step in the right direction, but still not an exact answers, as I get the exception `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` If I need to install something, that's fine - but I thought this taglib comes "out of the box" with jsp.Can you consider adding the answer to this to your answer?
noam
@Noam: It only comes out of the box on certain versions of JSP (2.0 and older, in this case). But you didn't tell us anything about your setup, so I can't give you a better answer.
skaffman
skaffman, this is actually not true. This is dependent on the server used.
BalusC
+2  A: 

JSTL support is dependent on the appserver/servletcontainer used. Some ships with JSTL, others doesn't. This is regardless of the JSP/Servlet version. For example Sun Glassfish (as being a full fledged Java EE implementation) ships with JSTL out of the box and for example Apache Tomcat (as being a simple JSP/Servlet implementation) doesn'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. Download jstl-1.2.jar.
  2. Put/copy it in webapp's /WEB-INF/lib folder (which is part of webapp's default classpath).
  3. 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). Remnant of the 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. You can find them all in this document (which is indeed pretty old, but has not changed up to with the current JSP 2.2).

BalusC