• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

web.xml vs annotations

 
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic