Serve static content with tomcat using a servlet on the front page

A

Aaron Isotton

Hello Group

I am working on a web application involving (mainly) one servlet serving
most of the content. I am using Tomcat 5.5. Specifically, I want to do
the following:

- / (the front page) should be served by MyServlet
- /some-directory/* should be served by MyServlet
- /css/* should be served as static content

(In reality, there are more directories like /css (and more servlets),
but this should be enough to make the point.)

Moreover, it is crucial that there may be only ONE instance of
MyServlet. Thus, using multiple 'servlet' declarations with different
paramenters doesn't work.

If I map / to MyServlet I can't access /css/* any more. So I tried
mapping / to MyServlet and /css/* to 'default' (the Tomcat default
servlet, which serves static content just as I want it to). The problem
is that /css/* is then mapped to the document root; thus I have to use
/css/css/some.file.name to access /css/some.file.name. That's just ugly.

Another way might be mapping only /some-directory/* to MyServlet and
using a forwarding index.jsp in the document root, but that's just plain
ugly.

So my questions are:

- is there some way to specify that /css/* should be served by 'default'
WITHOUT the /css/css uglyness?

- or is there some totally different solution for this problem? I'd like
to keep it as elegant as possible.

Thanks,
Aaron
 
J

Juha Laiho

Aaron Isotton said:
I am working on a web application involving (mainly) one servlet serving
most of the content. I am using Tomcat 5.5. Specifically, I want to do
the following:

- / (the front page) should be served by MyServlet
- /some-directory/* should be served by MyServlet
- /css/* should be served as static content

(In reality, there are more directories like /css (and more servlets),
but this should be enough to make the point.)

Moreover, it is crucial that there may be only ONE instance of
MyServlet. Thus, using multiple 'servlet' declarations with different
paramenters doesn't work.

I have the feeling that a redesign of your application would help.
This sounds like you're placing too much of your application logic
into the servlet class. Delegating the core logic to a separate class
(or a set of classes) should help. With that you can better control
which pieces of your logic really need to be unique instances, and
you can stop worrying over multiple instances of the servlet class.
At that point also your web.xml problem should be over.

By the way, you shouldn't need multiple <servlet> elements for the
above; just multiple <servlet-mapping>s. I can't tell whether this
would cause multiple instances to be created, though.
 
A

Aaron Isotton

Juha Laiho wrote:
[snip]
I have the feeling that a redesign of your application would help.
This sounds like you're placing too much of your application logic
into the servlet class. Delegating the core logic to a separate class
(or a set of classes) should help. With that you can better control
which pieces of your logic really need to be unique instances, and
you can stop worrying over multiple instances of the servlet class.
At that point also your web.xml problem should be over.

*g* "Any problem can be solved by adding a layer of abstraction".

Seriously, that seems a good idea.

Thanks,
Aaron
 
J

John C. Bollinger

Aaron said:
Moreover, it is crucial that there may be only ONE instance of
MyServlet.

There is no way to ensure this in general. As far as the servlet spec
goes, a servlet container is free to use a pool of instances, and it is
also free to discard any particular instance at any time (and, if
needed, replace it with a new instance). It is possible that particular
containers would allow you to configure these things in some
container-specific way, and it is also possible that you could come up
with a webapp configuration that caused your container of choice to use
only one instance, but these are not good solutions.

Just why is it that you want there to be only one instance, anyway?
There are surely better ways to achieve your underlying goals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top