• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet-mapping

 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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??
 
giang nguyen
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
 
Yorck Zhou
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic