• 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

Difference between value and urlPatterns attribute in WebServlet annotation

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic