Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

web.xml vs annotations

 
Yasir Karim
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A WebServlet is declared in web.xml with name A and also is annotated as follows:

@WebServlet (name="A",
urlPatterns={"/next"})
@WebInitParam(name="test",value="annotation")
public class GenServlet extends HttpServlet{
...


Which servlet configuration will be applicable in this case i.e. when both configs are configuring the same Servlet class under same name?
Also, which servlet configuration will be applicable in this case when both configs are configuring the same Servlet class under different names?

Assume that metadata-complete is false.

Thanks.
 
Christian Ludt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yasir,

the Servlet 3.0 specification is quite clear about that, have a look at chapter 8: Annotations and pluggability, there is nice example at the end of section 8.2.3 Assembling the descriptor from web.xml, web-fragment.xml and annotations.

When both configs are configuring the same Servlet class under the same name, the web.xml takes precedence. However, the init-parameters are additive unless they have the same name, then, the descriptor takes precedence again (see 8.2.3.n.iii). So, if you had a different init parameter specified in your web(-fragment).xml, you'd see them both.

When both configs are configuring the same Servlet class under different names, then you'll end up with two Servlet instances.

Unfortunately, Glassfish 3.1.1 does not seem to respect that part of the specification: it only creates one Servlet instance, the one defined in the descriptor, but merges the init-params!


BTW, you must use @WebInitParam inside of @WebServlet

@WebServlet (name="A", urlPatterns={"/next"}, initParams = {@WebInitParam(name="test",value="annotation")})

Best regards
Christian
 
Yasir Karim
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christian for your explanation. Your observation regarding Glassfish 3.1 is very helpful since I was trying to test this on Glassfish 3.1 and was experiencing something against the specifications.

Regarding @WebInitParam annotations: I believe both of the following will do the job. Correct me if wrong.


@WebServlet (name="A",
urlPatterns={"/next"})
@WebInitParam(name="test",value="annotation")


@WebServlet (name="A", urlPatterns={"/next"}, initParams = {@WebInitParam(name="test",value="annotation")})

Regards/
 
Christian Ludt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yasir,

the spec for @WebInitParam (8.1.3 @WebInitParam) says:

This annotation is used to specify any init parameters that must be passed to the Servlet or the Filter. It is an attribute of the WebServlet and WebFilter annotation.


It does not say it can be used outside of @WebInitParam; hence, you cannot rely on that as it is not part of the spec.

Regards
Christian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic