aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes servlet-mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "servlet-mapping" Watch "servlet-mapping" New topic
Author

servlet-mapping

Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
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??


Share Knowledge to gain it.
SCJP 2, SCWCD 2, SCDJWS, IBM 141 (In Progress), IBM 486 (Next)
giang nguyen
Ranch Hand

Joined: May 13, 2003
Posts: 42
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


SCJP 1.4, SCWCD
Yorck Zhou
Greenhorn

Joined: Jan 04, 2003
Posts: 22
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.


----------------------------------<br />SCJP SCWCD<br />dive into EJB, drinking hot Java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet-mapping