tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: CSS not being used when a Servlet is involved
Date Sat, 28 Oct 2006 12:48:15 GMT
Robbert,

> Hm, alright. Should all else fail, is it possible to let a Servlet handle
> the CSS?

You don't really want to do this.

> I have four JSP pages (index, profile, statistics and gallery) that must
> invoke the StatistiekServlet. The page is simply a normal, static HTML page
> that calls the Servlet. Then the Servlet catches certain HTTP Headers and
> query strings and does stuff depending on the values. When the Servlet's
> finished, the JSP continues spitting out HTML code.
> And if I do that, it says "Resource not available: /StatistiekServlet" for
> whatever reason.

Since you mentioned that you aren't very familiar with servlets, I'll go
ahead and say the following. Sorry if it sounds patronizing.

You have your process turned upside down, here. You are using a JSP as
the target of the URL, then invoking a servlet from there to do your
dirty work, and then going back. This is the wrong way to do things.

What you really want to do is map "/index" to your StatistiekServlet, do
whatever you need to do, and then do a "forward" (using the
RequestDispatcher) to your JSP in order to generate the outgoing content.

I'm guessing that you have this separate servlet for several reasons:

1. You have shared code to execute.
2. Someone (correctly) told you that JSPs with tons of logic and Java
   code are ... icky?
3. You weren't sure how to re-use your servlet code and not have to
   inspect the URL to figure out to which JSP you should forward
   afterward.

If you want to use shared code in a JSP (#1), you can simply put it in a
utility class/method that takes the appropriate methods. You don't have
to use the servlet mechanism and actually use a servlet to do this stuff.

The easiest way I can think of to "invert" your process (i.e. start with
the servlet, /then/ forward to the JSP based upon the URL being used) is
to use an application framework that helps you by mapping URLs to code
and then lets you define forwards for that URL mapping. Struts is such a
framework. You can set up mappings like this:

<action path="/index" type="your.shared.code.class">
    <forward name="success" path="/index.jsp" />
</action>

<action path="/profile" type="your.shared.code.class">
    <forward name="success" path="/profile.jsp" />
</action>
<action path="/statistics" type="your.shared.code.class">
    <forward name="success" path="/statistics.jsp" />
</action>

<action path="/gallery" type="your.shared.code.class">
    <forward name="success" path="/gallery.jsp" />
</action>

Note that the code invoked is the same every time; only the "success"
page changes. Your "servlet" code will have to turn into an "Action" (no
big deal) and have a bit of code at the end to tell Struts to use the
"success" forward (also not a big deal).

But, if you really want to have a nicely separated MVC application,
Struts can help tremendously. It looks like you have tried to take some
of these steps yourself, but have gotten confused somewhere along the
way -- ending up with your JSPs invoking your servlet, which feels
/very/ weird to me.

-chris


Mime
View raw message