Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between value and urlPatterns attribute in WebServlet annotation

 
Faiz A Chachiya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does anyone has any idea what is difference between the value and urlPatterns attributes in WebServlet annotation, where both are used to define URL...

is it that with value you can define just single URL and with urlPatterns you can define many URLS.

 
Milesh Bhana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's pretty much correct.

you use value if you only want to specify a single URL pattern and no other attributes.

this is incorrect (can't use value with another attribute)
@WebServlet("/myServlet", asyncSupported=false)

value only
@WebServlet("/myServlet")

you have to do this if you want to use multiple attributes
@WebServlet(urlPatterns={"/myServlet"}, asyncSupported=true)

notice that i had to use URL patterns for a single value because i wanted multiple attributes.

hope this helps.
 
Faiz A Chachiya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for making it clear, Well I tried this

this is incorrect (can't use value with another attribute)
@WebServlet("/myServlet", asyncSupported=false)

value only
@WebServlet("/myServlet")

and it works as expected but there is still some confusion

I tried the annotation below with the value attribute instead of urlPatterns

@WebServlet(value={"/2/*","/2/2/*"}, asyncSupported=true, initParams={@WebInitParam(name="param1", value="value1"), @WebInitParam(name="param2", value="value2")} )

and it works for multiple URLs and it does not gives any error at runtime. any idea???

 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiz A Chachiya wrote:Thanks for making it clear, Well I tried this

this is incorrect (can't use value with another attribute)
@WebServlet("/myServlet", asyncSupported=false)

value only
@WebServlet("/myServlet")

and it works as expected but there is still some confusion

I tried the annotation below with the value attribute instead of urlPatterns

@WebServlet(value={"/2/*","/2/2/*"}, asyncSupported=true, initParams={@WebInitParam(name="param1", value="value1"), @WebInitParam(name="param2", value="value2")} )

and it works for multiple URLs and it does not gives any error at runtime. any idea???



Replying to this as it is one of the first links when I Googled this.

From the Spec.

It is recommended to use value when the only attribute on the annotation is the url pattern and to use the urlPatterns attribute when the other attributes are also used.


So it is just a recommended guideline - it won't cause any issues with the mappings.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic