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 Question on @WebServlet 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 "Question on @WebServlet" Watch "Question on @WebServlet" New topic
Author

Question on @WebServlet

varun Dosapati
Ranch Hand

Joined: Nov 18, 2010
Posts: 30

in servlet3.0 on @webServlet if the name is specified the urlpattern is being ignored.

But I don't see anything in the spec mentioning about it.

Please advise.

Thanks
Varun
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

name is specified the urlpattern is being ignored.


"name"(Which describes servlets unique name) and "urlpattern" of a servlet are different in @webServlet.

and urlpattern or value atttribute is mandatory in that annotation.

What exactly is your question?

OCPJP
varun Dosapati
Ranch Hand

Joined: Nov 18, 2010
Posts: 30


What exactly is your question?


Below in my scenario I don't understand why it fails when I am trying to access with "sample.do"

For example
scenario 1:

In the below scenario
localhost:8081/contextroot/websample.do - works
localhost:8081/contextroot/sample.do - fails

web.xml
<servlet-mapping>
<servlet-name>ServletExample1</servlet-name>
<url-pattern>/websample.do</url-pattern>
</servlet-mapping>

ServletExample1.java

Scenario 2 :

If the content from web.xml is commented out

localhost:8081/contextroot/sample.do - works
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

When you define an URL-pattern in both the web.xml and in a @WebServlet annotation for the same Servlet, the URL-pattern in the deployment descriptor overrides the URL-pattern of the annotation.

From Servlet 3.0
8.2.3 Assembling the descriptor from web.xml, webfragment.xml and annotations
n. iv.) url-patterns, when specified in a descriptor for a given servlet name overrides the url patterns specified via the annotation.
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

The container looks at the name given to the servlet in the DD and the @WebServlet and ignore the configuration in the @WebServlet if the same name exists in the DD (regardless of the mapping). I suggest you change the name of the servlet in the @WebServlet annotation, or leave it out all together, the only required attribute is the urlPatterns. By the way, if you do not name the servlet in the annotation then a default name is given which is the fully qualified name of the class.


Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
varun Dosapati
Ranch Hand

Joined: Nov 18, 2010
Posts: 30

Frits Walraven

Thanks for pointing....


Alex Theedom

I agree with your explanation alex but I was trying to find the reason but once frits pointed it make more sense for me.

Thanks though ....
varun Dosapati
Ranch Hand

Joined: Nov 18, 2010
Posts: 30

When you define an URL-pattern in both the web.xml and in a @WebServlet annotation for the same Servlet, the URL-pattern in the deployment descriptor overrides the URL-pattern of the annotation.

From Servlet 3.0

8.2.3 Assembling the descriptor from web.xml, webfragment.xml and annotations
n. iv.) url-patterns, when specified in a descriptor for a given servlet name overrides the url patterns specified via the annotation.



Frits I wonder what would be the preference if we declare the servlet in web.xml and add pro-grammatically with same servlet name and different url-pattern.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

I am not sure I understand what you are asking, but my preference would be to use the annotation (and no web.xml)
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219


I wonder what would be the preference if we declare the servlet in web.xml and add pro-grammatically with same servlet name and different url-pattern.


Have you tried it out..? If not then please try it and tell us what happens. I think web.xml url-pattern should take over the priority or an ambiguity error may be thrown by the container.
Please do tell us the results if you have tried it.
varun Dosapati
Ranch Hand

Joined: Nov 18, 2010
Posts: 30

Ishan Pandya



Yes I tried and it is throwing an error by the container.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on @WebServlet
 
Similar Threads
@WebServlet annotation is not working
call another servlet in a web application
Why is web.xml not prior to annotation in conflict?
The requested resource (/Person/) is not available.
problem with @webservlet