• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on @WebServlet

 
varun Dosapati
Ranch Hand
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
varun Dosapati
Ranch Hand
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Saloon Keeper
Pie
Posts: 2344
90
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
varun Dosapati
Ranch Hand
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2344
90
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya



Yes I tried and it is throwing an error by the container.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic