tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robbert <robbert.ouweh...@gmail.com>
Subject Re: CSS not being used when a Servlet is involved
Date Sun, 29 Oct 2006 12:53:23 GMT


Christopher Schultz-2 wrote:
> 
> 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.
> 
> No, especially since it's not needed anymore.


Christopher Schultz-2 wrote:
> > 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.

It's alright. I'm here to learn.


Christopher Schultz-2 wrote:
> 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.

Why is the process turned upside down, exactly? Or rather, is there any
benefit in mapping /index to a Servlet and then forwarding to a JSP instead
of going to JSP pages which invoke my Servlet?
This probably boils down to the following question: When do you use a JSP
and when do you use a Servlet?


Christopher Schultz-2 wrote:
> 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.

Number 1 is correct and 3 is partially correct. The fourth, invisible,
option you didn't mention (and which you didn't know) is as follows: The
assignment requires the use of a StatistiekServlet which was a Servlet
acting behind the scenes. The way I did it seemed the easiest way since
JSP's can contain snippets of Java code: Kinda like how you can embed
snippets of PHP in a PHP file.


Christopher Schultz-2 wrote:
> 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
> 

Struts sounds like a good framework. I'm not too experienced with those but
I take it that won't be hard to use. It's just how we were learned to code.
So it never seemed to me as if the process was reversed.

-- 
View this message in context: http://www.nabble.com/CSS-not-being-used-when-a-Servlet-is-involved-tf2521410.html#a7060632
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message