0

The new annotation introduced in Servlet API 3.0 are so useful for configuring servlets/filters implemented by yourself. But... is there any way to configure servlets/filter provided by dependencies?

For instance, using Resteasy (actually many frameworks) is quite common to require something like:

<web-app version="3.0"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  </listener>

  (...)

</web-app 

Therefore you cannot make use of annotation there. Due some distribution issues, I'd like to be able to use an empty web.xml, so would be nice to find how programmatically declare such mappings.

wikier
  • 2,517
  • 2
  • 26
  • 39

2 Answers2

0

Check the specs, section 8.1 "Annotations and pluggability":

In a web application, classes using annotations will have their annotations processed only if they are located in the WEB-INF/classes directory, or if they are packaged in a jar file located in WEB-INF/lib within the application.

And the stuff about the metadata-complete.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
0

You can do this - but that doesn't necessarily mean you should :).

If you don't need to create any ServletContextListeners, then you can write a ServletContextListener and use the programmatic API of the ServletContext to add Servlets, Filters, HttpSessionListeners etc. to your web application. Annotate your ServletContextListeners with @WebListener and you are done.

If you need to create ServletContextListeners then you have to use a ServletContainerInitializer. Again use the programmatic API of the ServletContext. In this case=, you might as well do everything in the ServletContainerInitializer.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60