• 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

servlet-mapping

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have included the following code in web.xml...so which servlet will be excuted if i call
http://localhost/abc/blue/abc.jsp
<servlet-mapping>
<servlet-name>BlueServlet</servlet-name>
<url-pattern>/blue/*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Color/ervlet</servlet-name>
<url-pattern>/blue/*.jsp</url-pattern>
</servlet-mapping>
in this code the servlet name is different but the mapping is same..So which one will be called??
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andy Smith:
I have included the following code in web.xml...so which servlet will be excuted if i call
http://localhost/abc/blue/abc.jsp
<servlet-mapping>
<servlet-name>BlueServlet</servlet-name>
<url-pattern>/blue/*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Color/ervlet</servlet-name>
<url-pattern>/blue/*.jsp</url-pattern>
</servlet-mapping>
in this code the servlet name is different but the mapping is same..So which one will be called??


The mapping you specified is wrong, no servlet will be call with your request. Look at the following rule:
1.A string beginning with a / and ending with the /* characters is used for determining
a servlet path mapping.
2. A string beginning with a *. prefix is used to map the request to a servlet that
handles the extension specified in the string. For example, the following mapping
will direct all the requests ending with .pdf to pdfGeneratorServlet:
<servlet-mapping>
<servlet-name>pdfGeneratorServlet</servlet-name>
<url-pattern>*.pdf</url-pattern>
</servlet-mapping>
3. All other strings are used as exact matches only. For example, the following
mapping will direct http://www.mycompany.com/report to report-
Servlet. However, it will not direct http://www.mycompany.com/
report/sales to reportServlet.
So for the above mappings, only the exact matches of request URI will do. If there're more than one matches for the request you send, the first match will be chosen
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,giang nguyen,
you said:

So for the above mappings, only the exact matches of request URI will do. If there're more than one matches for the request you send, the first match will be chosen


but i have made an example in my tomcat, and found that what you said is not true.the truth is that
If there're more than one matches for the request you send, the most fitable or the last match will be chosen.
my example:
...
<servlet-mapping>
<servlet-name>Header</servlet-name>
<url-pattern>/1208/header</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/1208/header</url-pattern>
</servlet-mapping>
...
in this sample, the servlet response to hello will be chose when you request http://localhost/abc/1208/header

...
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/1208/more/header</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Header</servlet-name>
<url-pattern>/1208/more/*</url-pattern>
</servlet-mapping>
...
but in this sample, if you request http://localhost:8080/abc/1208/more/header, the hello servlet will response to it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic