Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Servlet 3.1 is the latest greatest Servlet API version. I needed to figure out the correct Maven dependencies for using a Servlet 3.1 container, Tomcat 8 specifically. This task was made more difficult because the Java Servlet dependencies tend to move over time. It would be so much better if they were consistent and all one had to do was to change the version numbers. Or better yet, why can’t I include the dependency for Expression Language 3.0 and have it pull in all of its transitive dependencies?

The Tomcat documentation had the following:

Tomcat Servlet API JSP API JSTL API Expression Language API
8.0 3.1 2.3 1.2 3.0

It took a bit of tinkering, but I think I have the Maven dependencies for Servlet 3.1 worked out.

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

Most of the Servlet 3.1 dependencies are packaged by Tomcat 8 and should have their scope marked as provided. The JSTL library is not packaged by Tomcat and cannot be scoped as provided. If you do mark it as provided, you will get the following exception when you try to use JSTL

HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

type Exception report

message Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
	org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1302)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:977)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
	org.springframework.web.servlet.support.JstlUtils.exposeLocalizationContext(JstlUtils.java:101)
	org.springframework.web.servlet.view.JstlView.exposeHelpers(JstlView.java:135)
	org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:142)
	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
	org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.30 logs.

Apache Tomcat/8.0.30

By using the above Maven dependencies, I was able to write a JSP page that used both Expression Language (EL) and JSTL Core Tags. All that is left now is to get to coding the next great webapp that is going to change the world.

April 12th, 2016

Posted In: Glassfish, Java, java ninja, Javaninja, JSP, Servlet Spec, Tomcat

Leave a Comment

The servlet 3.0 spec defines a way to share webapp JSP resources via a dependent jar (See Servlet 3.0 spec, section 10.5)

If you have a Maven dependency that is a jar, you can package resources and have them available to webapps that use them as a dependency.

To share, /css/test.css, you need to place it in:
/mavenModule/src/main/resources/META-INF/resources/css/test.css

Any webapp that depends on that jar can access /css/test.css as if it was in the root of it’s own webapp.

<link rel="stylesheet" type="text/css" href="<c:url value="/css/test.css" />">

Caveat, IntelliJ does not resolve /css/test.css as being within the current webapp. There was a ticket that was filed in 2011, but it doesn’t appear that it will ever be fixed.

Note, that if you are trying to use the included file as a tag file, it will need to have /WEB-INF/tags at the front of the included path.

January 24th, 2014

Posted In: IntelliJ, Maven, Servlet Spec

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com