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

Discussion in 'Java' started by Aaron Isotton, Jun 3, 2005.

  1. 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
     
    Aaron Isotton, Jun 3, 2005
    #1
    1. Advertising

  2. Aaron Isotton

    Juha Laiho Guest

    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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Jun 4, 2005
    #2
    1. Advertising

  3. Re: Serve static content with tomcat using a servlet on the frontpage

    Juha Laiho wrote:
    [snip]
    >>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.


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

    Seriously, that seems a good idea.

    Thanks,
    Aaron
     
    Aaron Isotton, Jun 4, 2005
    #3
  4. Re: Serve static content with tomcat using a servlet on the frontpage

    Aaron Isotton wrote:

    > 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.

    --
    John Bollinger
     
    John C. Bollinger, Jun 6, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Jessel
    Replies:
    0
    Views:
    419
    Ben Jessel
    Jul 25, 2003
  2. Alex Martinoff
    Replies:
    2
    Views:
    4,209
    Alex Martinoff
    Sep 7, 2003
  3. Sean Clarke
    Replies:
    1
    Views:
    1,950
    Sudsy
    Jan 7, 2004
  4. circuit_breaker
    Replies:
    2
    Views:
    2,091
    Jack Jia
    Apr 4, 2004
  5. Josh McCulloch
    Replies:
    1
    Views:
    1,035
    Sudsy
    Aug 19, 2004
Loading...

Share This Page