File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between value and urlPatterns attribute in WebServlet annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between value and urlPatterns attribute in WebServlet annotation " Watch "Difference between value and urlPatterns attribute in WebServlet annotation " New topic
Author

Difference between value and urlPatterns attribute in WebServlet annotation

Faiz A Chachiya
Greenhorn

Joined: Jun 05, 2007
Posts: 12

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

Joined: Feb 18, 2010
Posts: 5
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.


OCJP6 88%, OCWCD6 92%
Faiz A Chachiya
Greenhorn

Joined: Jun 05, 2007
Posts: 12
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???

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between value and urlPatterns attribute in WebServlet annotation