Tomcat and Servlet 3.0 Web Configuration

Tomcat 7 is now in production, and one of its new features is support for Servlet 3.0. Servlet 3.0 is…well, hard to remember what’s in there. But actually, it does have some ease of deployment features that open up the boring old world of web.xml, and they might even be useful in some cases.

For java libraries and frameworks these days, if you don’t have annotations, the sun does not shine down upon you. Annotations get used for just about everything, and Servlet 3.0 lets you use them to configure your web components (ie, servlet stuff) instead of putting them into web.xml. In fact, if you create a new dynamic web project in Eclipse, it will not even create a web.xml file. Use the New… wizard to create a servlet and it will go ahead and add the @WebServlet annotation for you.

@WebServlet("/MainWebFragmentServlet")
public class MainServlet extends HttpServlet

So now you can configure your servlets in annotations…is it a good idea? Keep in mind, the annotations are only skin-deep. You still have the same rules about what interfaces need to be implemented (HttpServlet, ServletContextListener, etc.) and what methods overriden. If you have much to configure, you’ll end up with something like this…

@WebServlet(
urlPatterns = {
"/AnotherTest",
"/AnotherTst",
"/AntherTest"
},
initParams = {
@WebInitParam(name = "testparm1", value = "test1"),
@WebInitParam(name = "testparm2", value = "test2")
})
public class AnotherTest extends HttpServlet {

So, pick your poison, annotations or xml. The annotations can get hideous, but for simple cases, they do reduce some clutter. If you aren’t going to use annotations, you probably want to set metadata-complete to true in your web.xml (<web-app metadata-complete="true"…) in order to avoid the time during start-up spent scanning classes for annotations.

Another enhancement in the Servlet 3.0 spec is the ability to define web-fragments separately from the main web.xml. You define a web-fragment.xml, normally in the META-INF folder. It looks mostly the same as the web.xml except that its root element is web-fragment and it has a different xml schema. In Eclipse you can create a Web Fragment Project that will generate the fragment file for you and also add your new project as a dependency to your main web project.

Web-fragments give you some flexibility and make your project components more modular. They’ll make it a little easier and tidier to use frameworks as they start getting added.

One note- if you are as lazy as I am and use Eclipse to create your new Servlet 3 project then copy over a web.xml file from an older project (remember, it won’t create one for you if you’ve selected Servlet 3), be sure to change the xsd and version from 2.5 to 3.0. Otherwise, the web-fragments you add in dependent projects will not work at all and you will feel like a dope when it occurs to you why.

This entry was posted in java, software and tagged . Bookmark the permalink.

3 Responses to Tomcat and Servlet 3.0 Web Configuration

  1. Pingback: Tomcat and Servlet 3.0 Web Configuration

  2. Benjamin Muschko says:

    Thanks for this posting. I was trying to set up a Servlet 3.0 web application using Spring’s 3.1 support for it. Spring scans the classpath for an implementation of WebApplicationInitializer whenever the JAR spring-web is found. This works all fine I all my dependencies have been added as JAR using WebappLoader#addRepository(java.lang.String). However, I have an issue when I try to add my compile sources including my impl. of WebApplicationInitializer as directory. Then it cannot be found. Right now I am trying to find out where the issue is. Tomcat’s classloading or the way Spring scans for implementations of WebApplicationInitializer on the classpath. Do you have any experience you can share with this kind of setup? Also a full example would be great.

  3. Pingback: What to do with annotations after setting metadata-complete=“true” (which resolved slow Tomcat 7 start-up)? | PHP Developer Resource

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>