You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jeremy <je...@archaeopteryx.freeserve.co.uk> on 2000/06/23 14:19:32 UTC

Default servlet?

Hello all,

Minor problem. When I set a default servlet mapping, by mapping from "/" to
a (for example) a servlet called Login, all works fine except that this
messes up static file paths and so my static files (eg. *.jpg's) no longer
display.

Is there a default servlet that handles static file serving (as there is in
Jigsaw) that I should map my *.jpg's and *.gif's to?

I couldn't see anything mentioned in the archives (except
DefaultServlet.java which appears to be a different thing), and there's
nothing in the user guide.

(Remarkably few problems otherwise, a really nice server, thanks all.)

Jeremy.



Re: Default servlet?

Posted by Jeremy <je...@archaeopteryx.freeserve.co.uk>.
Hi Craig,

I spoke to soon, there appears to be a problem with the welcome-file
feature, and this might be a bug, it's obscure enough not to have been tried
before.

As you  suggested I setup a welcome file in my web apps web.xml, which
points to a .jsp file containing a one line redirect to "/login.html", which
is an alias to my login servlet. My web app is mapped to root, so this
should be the default page for the server.

When I open the browser and ask for "http://www.acme.com/login.html"
**directly** the servlet loads OK, and when I then try loading
http://www.acme.com (with or without the following "/") the welcome-file
redirect works as it should, and the servlet returns.

However, when I quit and re-open the browser and then try
http://www.acme.com first, I get a nullPointerException from the servlet,
and if I **then** try to load the servlet via it's full url (eg.
http://www.acme.com/login.html) that also returns an NPE.

The specific problem with the servlet is caused by calling
request.getServerName(), which appears not to be getting properly
initialised when the servlet is called via a redirect from the .jsp. And
therein, I believe, might hide a buglet.

Any thoughts?

Jeremy.



> Are you perhaps looking for the "welcome file" facility?  This is similar
to the Apache
> configuration command "DirectoryIndex", which displays a specified page
when you specify a URL
> that resolves to a directory (including the top level directory).
>
> For example, you could make a url "http://www.mydomain.com" resolve to
your page "index.jsp" by
> adding the following to the web.xml for the "ROOT" context:
>
>     <welcome-file-list>
>         <welcome-file>index.jsp</welcome-file>
>     </welcome-file-list>
>
> and then create an "index.jsp" file in the document root directory of this
context.
>
> Although you cannot map directly to a servlet using this technique, you
can do so indirectly.
> Simply make the "index.jsp" page mentioned above a one-line file that
includes a <jsp:forward>
> to the servlet you really want to execute, and you should be off to the
races.
>
> Craig McClanahan
>
>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
>
>


Re: Default servlet?

Posted by Jeremy <je...@archaeopteryx.freeserve.co.uk>.
Hi Craig,

Yep, thanks Craig, the 'Welcome file' facility was exactly what I was
looking for, it worked. That might be worth documenting. I just searched the
appdev and uguide docs in Tomcat 3.1, and the sample config files, but
couldn't see it.

This might fix Xander Heemskerk, Micheal Miranda and Sharad Baranwal's
problems also, from earlier postings with similar problems.

Jeremy.


----- Original Message -----
From: "Craig R. McClanahan" <Cr...@eng.sun.com>
To: <to...@jakarta.apache.org>
Sent: Monday, June 26, 2000 11:59 PM
Subject: Re: Default servlet?


> Jeremy Johnson wrote:
>
> > Hi Craig,
> >
> > Oops, many apologies, we were talking about two different
interpretations of
> > 'default servlet'. What I'm looking for is the ability to enable a
servlet to
> > take the place of "index.html" (or whatever), so that it is called when
only the
> > domain name is requested (eg. www.mydomain.com). DefaultServlet isn't
the
> > solution, I'll keep hunting. Many apologies for the misunderstanding.
> >
>
> Are you perhaps looking for the "welcome file" facility?  This is similar
to the Apache
> configuration command "DirectoryIndex", which displays a specified page
when you specify a URL
> that resolves to a directory (including the top level directory).
>
> For example, you could make a url "http://www.mydomain.com" resolve to
your page "index.jsp" by
> adding the following to the web.xml for the "ROOT" context:
>
>     <welcome-file-list>
>         <welcome-file>index.jsp</welcome-file>
>     </welcome-file-list>
>
> and then create an "index.jsp" file in the document root directory of this
context.
>
> Although you cannot map directly to a servlet using this technique, you
can do so indirectly.
> Simply make the "index.jsp" page mentioned above a one-line file that
includes a <jsp:forward>
> to the servlet you really want to execute, and you should be off to the
races.
>
> Craig McClanahan
>
>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
>
>


Re: Default servlet?

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

> Hi Craig,
>
> Oops, many apologies, we were talking about two different interpretations of
> 'default servlet'. What I'm looking for is the ability to enable a servlet to
> take the place of "index.html" (or whatever), so that it is called when only the
> domain name is requested (eg. www.mydomain.com). DefaultServlet isn't the
> solution, I'll keep hunting. Many apologies for the misunderstanding.
>

Are you perhaps looking for the "welcome file" facility?  This is similar to the Apache
configuration command "DirectoryIndex", which displays a specified page when you specify a URL
that resolves to a directory (including the top level directory).

For example, you could make a url "http://www.mydomain.com" resolve to your page "index.jsp" by
adding the following to the web.xml for the "ROOT" context:

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

and then create an "index.jsp" file in the document root directory of this context.

Although you cannot map directly to a servlet using this technique, you can do so indirectly.
Simply make the "index.jsp" page mentioned above a one-line file that includes a <jsp:forward>
to the servlet you really want to execute, and you should be off to the races.

Craig McClanahan



Re: Default servlet?

Posted by Jeremy Johnson <je...@yonks.org>.
Hi Craig,

Oops, many apologies, we were talking about two different interpretations of
'default servlet'. What I'm looking for is the ability to enable a servlet to
take the place of "index.html" (or whatever), so that it is called when only the
domain name is requested (eg. www.mydomain.com). DefaultServlet isn't the
solution, I'll keep hunting. Many apologies for the misunderstanding.

Maybe 'index servlet' would be a more accurate term.

Jeremy.


On Sat, 24 Jun 2000, you wrote:
> Jeremy Johnson wrote:
> 
> > Hi Craig,
> >
> > > Here's the point where I say "read the servlet specification, please!"
> >
> > Sorry, but I **was** looking at that Section 10.2 when I wrote the email..
> >
> > >     "A string containing only the '/' character indicates that the servlet
> > >     specified by the mapping becomes the "default" servlet of the
> > >     application.
> >
> > My question is, when I set a default servlet as above, why does this have to
> > prevent Tomcat serving any/all static files (eg. /furniture/button.jpg) ?
> >
> 
> What a default servlet does is handle all requests (for this web app) that are not mapped
> to some other servlet.  In Tomcat 3.1, the file-serving for static files is actually
> performed by just such a default servlet (org.apache.tomcat.servlets.DefaultServlet).
> When you replace this default servlet with your own, you are replacing this functionality
> of Tomcat -- so if you still want to serve static files, your default servlet will have to
> do it for you.
> 
> One approach would be to use the source code for this servlet as the starting point for
> your own default servlet, and customize the handling for the special things you want to
> do.
> 
> Craig
> 
> 
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org


Re: Default servlet?

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

> Hi Craig,
>
> > Here's the point where I say "read the servlet specification, please!"
>
> Sorry, but I **was** looking at that Section 10.2 when I wrote the email..
>
> >     "A string containing only the '/' character indicates that the servlet
> >     specified by the mapping becomes the "default" servlet of the
> >     application.
>
> My question is, when I set a default servlet as above, why does this have to
> prevent Tomcat serving any/all static files (eg. /furniture/button.jpg) ?
>

What a default servlet does is handle all requests (for this web app) that are not mapped
to some other servlet.  In Tomcat 3.1, the file-serving for static files is actually
performed by just such a default servlet (org.apache.tomcat.servlets.DefaultServlet).
When you replace this default servlet with your own, you are replacing this functionality
of Tomcat -- so if you still want to serve static files, your default servlet will have to
do it for you.

One approach would be to use the source code for this servlet as the starting point for
your own default servlet, and customize the handling for the special things you want to
do.

Craig



Re: Default servlet?

Posted by Jeremy Johnson <je...@archaeopteryx.freeserve.co.uk>.
Hi Craig,

> Here's the point where I say "read the servlet specification, please!"

Sorry, but I **was** looking at that Section 10.2 when I wrote the email..

>     "A string containing only the '/' character indicates that the servlet
>     specified by the mapping becomes the "default" servlet of the
>     application.

My question is, when I set a default servlet as above, why does this have to
prevent Tomcat serving any/all static files (eg. /furniture/button.jpg) ?

Jeremy.






On Sat, 24 Jun 2000, you wrote:
> Jeremy Johnson wrote:
> 
> > Hi Craig,
> >
> > Many thanks for your reply, a brief thought on the servlet mapping. You said
> > that if I map "/" to a servlet then Tomcat will no longer serve static files,
> > but is that syntactically correct? I mean, the static files (eg. *.jpg's) arn't
> > at "/", they're at (eg.) "/furniture/button.jpg", so how come that matches "/" ?
> >
> 
> Here's the point where I say "read the servlet specification, please!"
> 
> In particular, see Section 10.2, the fourth bullet, which says:
> 
>     "A string containing only the '/' character indicates that the servlet
>     specified by the mapping becomes the "default" servlet of the
>     application.
> 
> In other words, this mapping is treated specially.  It is not treated as a pattern
> match.  You will also want to look back up the page at Section 10.1, which describes
> *exactly* how Tomcat, or any other servlet container that complies with the 2.2
> specification, is supposed to map requests to servlets.
> 
> The servlet spec is available for download at:
> 
>     http://java.sun.com/products/servlet/download.html
> 
> 
> >
> > Jeremy.
> 
> Craig McClanahan
> 
> 
> 
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org

Re: Default servlet?

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

> Hi Craig,
>
> Many thanks for your reply, a brief thought on the servlet mapping. You said
> that if I map "/" to a servlet then Tomcat will no longer serve static files,
> but is that syntactically correct? I mean, the static files (eg. *.jpg's) arn't
> at "/", they're at (eg.) "/furniture/button.jpg", so how come that matches "/" ?
>

Here's the point where I say "read the servlet specification, please!"

In particular, see Section 10.2, the fourth bullet, which says:

    "A string containing only the '/' character indicates that the servlet
    specified by the mapping becomes the "default" servlet of the
    application.

In other words, this mapping is treated specially.  It is not treated as a pattern
match.  You will also want to look back up the page at Section 10.1, which describes
*exactly* how Tomcat, or any other servlet container that complies with the 2.2
specification, is supposed to map requests to servlets.

The servlet spec is available for download at:

    http://java.sun.com/products/servlet/download.html


>
> Jeremy.

Craig McClanahan




Re: Default servlet?

Posted by Jeremy Johnson <je...@archaeopteryx.freeserve.co.uk>.
Hi Craig,

Many thanks for your reply, a brief thought on the servlet mapping. You said
that if I map "/" to a servlet then Tomcat will no longer serve static files,
but is that syntactically correct? I mean, the static files (eg. *.jpg's) arn't
at "/", they're at (eg.) "/furniture/button.jpg", so how come that matches "/" ?

Also, if "/" stops that from working then surely the other servlet mappings
should get caught as well, as they also start with a "/" (eg. "/myservlet.html"
is mapped to "Myservlet.class"). The fact that this works whereas similar URI's
to static files don't work seems to be inconsistent.

Could I suggest that mapping "/" to a servlet should map only the URL
http://www.mydomain.com/ or http://www.mydomain.com, unless there is a
technical reason why this isn't possible. Writeing a new servlet to serve static
files simply because I want a servlet to serve the home page is a real pain!

Just a thought,  many thanks again for your reply.

Jeremy.


On Fri, 23 Jun 2000, you wrote:
> Jeremy wrote:
> 
> > Hello all,
> >
> > Minor problem. When I set a default servlet mapping, by mapping from "/" to
> > a (for example) a servlet called Login, all works fine except that this
> > messes up static file paths and so my static files (eg. *.jpg's) no longer
> > display.
> >
> > Is there a default servlet that handles static file serving (as there is in
> > Jigsaw) that I should map my *.jpg's and *.gif's to?
> >
> > I couldn't see anything mentioned in the archives (except
> > DefaultServlet.java which appears to be a different thing), and there's
> > nothing in the user guide.
> >
> 
> In Tomcat 3.1, DefaultServlet does in fact do the usual file serving.  It is
> called whenever no other servlet mapping matches the request URI, and it can
> tell what file to display by calling request.getServletPath().
> 
> If you want to override the "/" mapping but still have static files served, you
> are going to have to build that functionality into your own servlet instead,
> because DefaultServlet will no longer be used.
> 
> >
> > (Remarkably few problems otherwise, a really nice server, thanks all.)
> >
> > Jeremy.
> >
> 
> Craig McClanahan
> 
> 
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org

Re: Default servlet?

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

> Hello all,
>
> Minor problem. When I set a default servlet mapping, by mapping from "/" to
> a (for example) a servlet called Login, all works fine except that this
> messes up static file paths and so my static files (eg. *.jpg's) no longer
> display.
>
> Is there a default servlet that handles static file serving (as there is in
> Jigsaw) that I should map my *.jpg's and *.gif's to?
>
> I couldn't see anything mentioned in the archives (except
> DefaultServlet.java which appears to be a different thing), and there's
> nothing in the user guide.
>

In Tomcat 3.1, DefaultServlet does in fact do the usual file serving.  It is
called whenever no other servlet mapping matches the request URI, and it can
tell what file to display by calling request.getServletPath().

If you want to override the "/" mapping but still have static files served, you
are going to have to build that functionality into your own servlet instead,
because DefaultServlet will no longer be used.

>
> (Remarkably few problems otherwise, a really nice server, thanks all.)
>
> Jeremy.
>

Craig McClanahan