Scriptico

First Experience with Servlet 3.0

Das Ostern is finished and finally, I’ve been assigned to a project. As you know, sometimes it takes a few days to prepare environment, setup all accounts, and so on. Thus, during the last two days my schedule is pretty much relaxed and I can spend some time for self-education – I am reading the Servlet 3.0 specification, writing some simple application in order to try discovered features, and wondering about two things: how really cool servlet 3.0 is and how I could missed it?! If you did not touch it as well, take a look on the following briefly

Ok, I am not a I-like-xml-configs guy so for me the fact that a big part of the servlet configuration can be done by using Java annotattions means a lot. Unfortunately, not everything is configurable this way and the old school descriptor file is still there. It has a priority over the annotations (good for tests, isn’t it?). Take a look at the following code:

@WebServlet(description = "extremly usefull description", urlPatterns =
{ "/my-servlet" })
public class TestServlet extends HttpServlet
{...}

The @WebServlet annotation marks a class a servlet and is processing by the application container. There’s neither a special rule where the servlet classes have to be placed nor xml parameter that defines a package to scan (at the begining I expected something like the context:component-scan base-package parameter in Spring MVC). You can read more about the @WebServlet annotation right here.

Well, what’s next? Listeners and Filters. As you can assume, these two guys can be configured the same way

@WebListener
public class TestListener implements HttpSessionListener
{...}
@WebFilter("/my-servlet")
public class TestFilter implements Filter
{...}

Obviosly, the specification describes a number of new features and I am not going to copy/paste all 230 pages. Just try to create a simple web application, install a container that supports the specification (I use Tomcat 7.x), and play around. You will like it!

Category: Development, Java, Servlets. JSP, WebApp

Tagged:

Comments are closed.