my dog learned polymorphism*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes web.xml vs annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "web.xml vs annotations" Watch "web.xml vs annotations" New topic
Author

web.xml vs annotations

Yasir Karim
Greenhorn

Joined: Jul 21, 2011
Posts: 17

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.


I see the truth in the code.
Christian Ludt
Greenhorn

Joined: Apr 07, 2009
Posts: 11
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

Joined: Jul 21, 2011
Posts: 17

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

Joined: Apr 07, 2009
Posts: 11
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: web.xml vs annotations
 
Similar Threads
how to use @WebServlet like web.xml element
Why is web.xml not prior to annotation in conflict?
Regrading JSP's
How to Start a service automatically when the tomcat starts?
Filter Mapping Problem