Java, XMPP, Space and pretty much everything else

Writing Servlets the J2EE 6 / Servlet 3.0 way

In the past whenever you wrote a Servlet you had a lot of work to do. First you wrote your servlet, then you had to add configuration for that servlet into web.xml so that your application would use it.

For simple applications this was fine but the more servlets you wrote the harder it became as web.xml started to become large and unwieldy.

Now with J2EE 6 you got the ability to add annotations to your servlets. No more do you need to add anything to web.xml as the container scans the classpath for any servlet that’s annotated with @WebServlet getting the required configuration from there.

This also has the benefit that you can write libraries of servlets. Those servlets get deployed automatically and you don’t have to worry about duplicating the config.

Both Tomcat 7 and TomEE 1.5.1 support the J2EE 6 Web Profile as standard. I’m not certain of other containers as I’ve only tested this on those two.

Now imaging doing that dozens of times… yes it becomes unmanageable. It’s because of this for the rise of other web frameworks. Well not the sole reason but one thing other frameworks try to do is to make it simpler and more maintainable – this way it isn’t.

Writing a Servlet the new way

In J2EE 6 it’s now simpler. All you need to do is to annotate the Servlet itself with the @WebServlet annotation:

Thats it. Nothing goes into web.xml. When the container deploys the application it sees the @WebServlet annotation and uses that configuration to deploy the servlet.

What about servlets with multiple url mappings you may ask?

Well the urlPatterns property in the annotation is actually an array. In the example above I’ve used shorthand where javac will allow a single value to be written without an array but it’s possible to add multiple patterns easily:

loadOnStartup – set this to 1 and your servlet is loaded when the webapp is. Leave this out and the servlet gets loaded when it’s first requested.

initParams – this is an array of @WebInitParam annotations containing the servlet’s initial parameters.

Although it might seem strange to have initParameters in an annotation when you could do it in code but it is beneficial in that:

You keep the configuration in a single place

Your Servlet might inherit from another one. Placing configuration on the deployed servlet can then be passed on to the super class.

ServletContextListener’s

ServletContextListener‘s can also be annotated with the @WebListener annotation. This is useful if you are deploying a framework which must perform actions when the application is deployed or destroyed.

In the past you had to declare each one in web.xml with something like the following:

The stub above declares a filter which runs against all requests to the application. Like servlets urlPatterns is an array of url patterns although you can also use servletNames instead which declares the servlets this filter is to run against.

The only downside I can see with this is that you cannot determine the order that filters run – in web.xml they run in the order they are declared. This can be a problem for some applications but for most this is enough.

That’s all for now about Servlet 3.0. There’s a couple of topics not covered, like security & multipart upload support but those will be covered by separate articles.

Share this:

Like this:

Related

3 thoughts on “Writing Servlets the J2EE 6 / Servlet 3.0 way”

You mentioned security: Do you have experience with servlet 3.0 and form-login? I didn’t find any examples in the whole wide web but in a tutorial I read something that this would not be possible to do with annotations, when using form login a deployment descriptor is mandatory.