December 22, 2024

JSP MVC Application via Maven

After a quick win with the JSP version of Hello World, it seemed that a servlet-based version was only a tweak or two away. Alas, it was not to be.

Coding

After using Maven to create another web project, I added a simple controller and DTO model object based on this article. Not wanting to get involved with Spring, etc., the project required mapping in the web.xml file as well. 

I also updated the web.xml file to version 2.5 of the servlet engine.

A few basic language mistakes later, the code compiled.

Lesson Learned: Unlike .NET’s using statement, the Java import statement includes classes, not packages.

Packaging

Unfortunately, Maven was unable to complete the build because of this error:

error: package javax.servlet does not exist

The solution to this little ditty was only a Google away.  Missing a dependency in Maven’s POM file:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>

Not sure why Maven’s archetype doesn’t include this dependency by default, but adding it to the POM fixed things so Maven was able to package the project into a WAR file.

Deployment

Tomcat was able to unpack the WAR file without incident.

Execution

Hitting the URL a .NET developer might expect:

http://localhost:9999/dtoexample/examplecontroller

did not work.  As memory served, though, I was half-expecting it – Java, like *nix, is a prissy environment when it comes to case, so:

http://localhost:9999/dtoexample/ExampleController

invoked my new controller action.

Unfortunately, the app coughed up a run-time error:

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

The Fix

More Googling lead to this Stack Overflow article – the original poster was close and the top answer helped a lot.  Basically more missing dependencies in the POM:

<dependency>

  <groupId>taglibs</groupId>

  <artifactId>standard</artifactId>

  <version>1.1.2</version>

</dependency>

<dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>jstl</artifactId>

  <version>1.2</version>

</dependency>

and a tweak needed to the JSP provided by Maven, which was missing the jsp segment for version 1.2 of the jstl:

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

After these adjustments, my little project ran, albeit with another repeat of a self-inflicted wound: Would it kill Sun to provide a real property syntax?  Come on!

Leave a Reply