Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

many url-pattern for the same servlet

I need to map the same servlet on two different url. I used netbeans 7.0.1 for managing my whole project, so I used its friendly interface to modify the web.xml file. What netbeans created is this:

<servlet-mapping>     <servlet-name>fred</servlet-name>     <url-pattern>*.jsp</url-pattern>     <url-pattern>/url</url-pattern> </servlet-mapping> 

This is read by tomcat 5.5 without emitting any error, but only the second pattern works, while the first one is ignored.

Googling I found that the right way for tomcat is this one:

<servlet-mapping>     <servlet-name>fred</servlet-name>     <url-pattern>*.jsp</url-pattern> </servlet-mapping> <servlet-mapping>     <servlet-name>fred</servlet-name>     <url-pattern>/url</url-pattern> </servlet-mapping> 

So, my questions: is this a bug in tomcat? What syntax do other containers accept?

like image 537
eppesuig Avatar asked Jan 24 '12 22:01

eppesuig


People also ask

Can a servlet have multiple url patterns?

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping. For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

Can we have multiple url pattern in Web xml?

Yes that works just fine, but that is Servlet 2.4 style and I am trying to avoid the problem of extra typing. because <url-pattern> element is allowed only once under <filter-mapping> . "Multiple <url-pattern> elements should be fine, but the value /einwenig/*.

What is url pattern of a servlet?

The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web.

What is a url pattern?

A URL pattern is a set of ordered characters to which the Google Search Appliance matches actual URLs that the crawler discovers. You can specify URL patterns for which your index should include matching URLs and URL patterns for which your index should exclude matching URLs.


1 Answers

I guess it has more to do with the servlet spec the container/netbeans is using rather than being an issue with the container. Your net beans seems to be using the spec 2.5 to construct the servlet mapping and hence you get

<servlet-mapping>    <servlet-name>fred</servlet-name>    <url-pattern>*.jsp</url-pattern>    <url-pattern>/url</url-pattern> </servlet-mapping> 

Read more about this here. It says

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping.For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

like image 188
Aravind A Avatar answered Sep 21 '22 15:09

Aravind A