1

I am just learning JSP and am testing using JSTL on Tomcat 7. I am able to successfully define and use custom tags. But when I attempt to implement JSTL the container throws two exceptions. How can I fix this so the jsp will translate properly?

java.lang.ClassNotFoundException: javax.servlet.jsp.tagext.TagLibraryValidator java.lang.NoClassDefFoundError: javax/servlet/jsp/tagext/TagLibraryValidator

I'm using the following jar files.

javax.servlet.jsp.jstl-1.2.1.jar javax.servlet.jsp.jstl-api-1.2.1.jar

I have placed the two JSTL 1.2.1 jars in WEB-INF/lib of the test web app and the CLASSPATH for my JRE. I have also marked the two jars for export in the build path options in Eclipse.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <c:out value="JSTL works."></c:out>
    </body>
</html>
cyotee doge
  • 1,128
  • 4
  • 15
  • 33

2 Answers2

4

java.lang.NoClassDefFoundError: javax/servlet/jsp/tagext/TagLibraryValidator

This class is part of JSP 2.0. Tomcat is a Servlet 3.0 / JSP 2.2 container. Make sure that your web.xml is declared conform Servlet 3.0.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->

</web-app>

And also that you don't have arbitrary JAR files of a different container make/version like jsp-api.jar from Tomcat 5.5 or so in your /WEB-INF/lib. It would otherwise only conflict. See also How do I import the javax.servlet API in my Eclipse project?


I have placed the two JSTL 1.2.1 jars in WEB-INF/lib of the test web app

That should be sufficient. If cleaning the web.xml and the classpath still doesn't fix the problem, perhaps you don't have the right JARs at all. In our JSTL wiki page you can find a download link to a single jstl-1.2.jar file. Drop it in /WEB-INF/lib and remove the other JARs.


and the CLASSPATH for my JRE.

Please don't do that. Just dropping the JSTL JAR in /WEB-INF/lib is sufficient. Also make sure that you don't drop arbitrary servletcontainer-specific JARs in your JRE/lib or JRE/lib/ext. Just don't touch those folders. Webapp-specific libraries should go in /WEB-INF/lib. If you're using an IDE, it will then do automatically all the necessary magic. You don't even need to fiddle in buildpath properties. The CLASSPATH environment variable is only used when you run java command in your command console and even then only when you run it without the -cp, -classpath and -jar arguments.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My issue was that the IDE wasn't doing anything automatically. The issue was that having the JSTL jars in my CLASSPATH included then for use in my IDE, Eclipse, but would not deploy them with the web app. And when I included the jars in the WEB-INF of the development environment I would get errors for having redundant classes. The best option was to include them as a separate build path library. And then mark that library for export with dependent projects. – cyotee doge May 14 '12 at 21:59
  • I've never experienced like that in Eclipse. The errors for having redundant classes is caused by something else (and doesn't seem to be Eclipse specific). Are you sure that your project is been setup right? It's by the way the `/WEB-INF/lib` folder where the JAR needs to be dropped. If you have created a "Dynamic Web Project". – BalusC May 14 '12 at 22:01
  • Well, I used my solution, and now Eclipse deploys my web apps on the implemented Tomcat server just fine. Same with the mysql JDBC connector. I'm still learning, so I'm not sure what it's actually doing now, but it all works reliably and is portable. – cyotee doge May 15 '12 at 23:26
  • Added 1.2.1 links to the tag - I guess only the `javax.servlet.jsp.jstl-1.2.1.jar` is needed ? – Mr_and_Mrs_D Oct 06 '12 at 23:53
  • I guessed wrong - both are needed - please have a look at my edit there - especially if the info on the servlet spec for 1.2.1 is ok. The addition of the download links for 1.2.1 I consider mandatory though (people is just confused by not seeing 1.2.1 in the wiki - as it is out for quite some time now). – Mr_and_Mrs_D Oct 07 '12 at 00:34
0

For running on apache tomcat 7 adding these into your POM is probably appropriate. These jars don't reference javax.servlet jars like the glassfish ones do, so there is no need for exclusions.

<dependency>
    <groupId>org.apache.taglibs</groupId>
    <artifactId>taglibs-standard-spec</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.taglibs</groupId>
    <artifactId>taglibs-standard-impl</artifactId>
    <version>1.2.1</version>
</dependency>
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • Are those tow apache jars equivalent to jstl.jar? or equivalent to javax.servlet.jsp.jstl-api-1.*.*.jar? – gfan Oct 10 '16 at 03:10
  • For me, I think the sentence means noting. somebody can put all the JSTL specification into one jar. some have to split it into 4 jars(apache taglibs have 4 jars to download). Do I have to include those four all the time? or which part of JSTL specification in which jar out of four? What's the difference between the four? – gfan Oct 10 '16 at 14:37