You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Peter Schuller <pe...@infidyne.com> on 2000/07/16 04:20:02 UTC

Mapping "/" and *only* "/"

Hello,

What is the correct procedure for mapping "/" (only "/") to a servlet? With
the following servlet mapping, *all* requests are mapped to the servlet, and
I don't know why (since "/*" does that too; why does "/" do the same?):

<servlet-mapping>                       
	<servlet-name>                               
		fileviewer     
	</servlet-name>       
	<url-pattern>                       
		/          
	</url-pattern>
</servlet-mapping>                            

Thanks!

-- 
/ Peter Schuller, InfiDyne Technologies HB

PGP userID: 0x5584BD98 or 'Peter Schuller <pe...@infidyne.com>'
Key retrival: Send an E-Mail to getpgpkey@scode.infidyne.com
E-Mail: peter.schuller@infidyne.com Web: http://scode.infidyne.com


Re: Mapping "/" and *only* "/"

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Peter Schuller wrote:

> Hello,
>
> What is the correct procedure for mapping "/" (only "/") to a servlet?

There is no mechanism to do this with <servlet-mapping>.  However, if you are
trying to emulate the way that a web server will serve an "index.html" file to
someone who asks for the directory itself, you can use the <welcome-file-list>
to identify the names of welcome files -- typically something like index.jsp --
that is displayed for this purpose.  I haven't ever tried this with a servlet,
but it ought to work.

> With
> the following servlet mapping, *all* requests are mapped to the servlet, and
> I don't know why (since "/*" does that too; why does "/" do the same?):
>

The "/*" and "/" mappings do not quite do the same thing.  The key issue is the
order in which Tomcat, or any servlet container conforming to servlet 2.2, will
try to match your request to a servlet based on the mappings:

1)  Exact match
2)  Longest prefix match against a pattern that ends in "/*"
3)  Extension match (like *.jsp to the JSP servlet)
4)  Default servlet (mapped to "/").

As you can see, the "default servlet" mapping is only used if no other servlet
mapping matches.  If you use a "/*" pattern, that will match everything (except
for exact matches) so that any extension matching you want to do won't happen,
because the "/*" match happens earlier in the priority order.

>
> <servlet-mapping>
>         <servlet-name>
>                 fileviewer
>         </servlet-name>
>         <url-pattern>
>                 /
>         </url-pattern>
> </servlet-mapping>
>

In fact, the "default servlet" in Tomcat 3.1 is the servlet that serves static
resources, which it defines as "anything for which no other servlet or JSP page
matched."

>
> Thanks!
>
> --
> / Peter Schuller, InfiDyne Technologies HB
>

Craig McClanahan