Explain Codes LogoExplain Codes Logo

Maven dependency for Servlet 3.0 API?

java
maven
dependency-management
servlet-api
Nikita BarsukovbyNikita Barsukov·Dec 5, 2024
TLDR

To add the Servlet 3.0 API to your Maven project, include this snippet in your Maven pom.xml:

<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> <!-- Here's the magic. No rabbit in the hat though! --> </dependency>

Setting <scope> to provided ensures that while the API is present during compilation, it won't get included in the final build—the container will supply it.

'Provided' scope demystified

Maven's provided scope is crucial for web applications running in a servlet container like Tomcat or JBoss. What provided communicates to Maven is:

  • This dependency is necessary for compilation (Like coffee for coding!).
  • It should be supplied at runtime (JIT: Just In Time).
  • It should not be included in the final war file.

Matching API version to your environment

For Servlet 3.0, javax.servlet-api:3.0.1 is a great fit. But it's a good idea to match the version to your server and Java version:

  • For Tomcat 7, javax.servlet-api:3.0.1 is a match made in heaven.
  • If you are looking at Tomcat 8, opt for javax.servlet-api:3.1.0 to benefit from extra features.
  • If you're a JBoss rider, the jboss-servlet-api_3.0_spec is just the ticket for you.

Dodge these bullets: Common pitfalls

When dealing with dependencies, maintain a clean house:

  • Avoid including the entire Java EE 6 API if you need only the Servlet API.
  • Don't bundle JSTL with your application when using a container like Tomcat that provides it for you.
  • Keep a weather eye open for updates and version changes on the Maven repository - Ahoi, no surprises after upgrades!

Know your resources: Various alternatives & libraries

Java EE 6 has more to offer

The java.net repository is a great place to find other APIs such as JSF 2.0 that complement the Servlet API.

Glassfish Server alternatives

If Glassfish server is your jam, org.glassfish:javax.servlet might be your new best friend. Stay tuned to changes in Java EE API distribution to pick the right Glassfish version.

Java EE 6 Profiles & Javadocs

Interested in reducing dependencies? https://repository.jboss.org/nexus/content/groups/public/ hosts Javadocs and updates on Java EE 6 Profiles that can help.