tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From William Wragg <>
Subject RE: Forward to JSP from Servlet
Date Fri, 16 May 2003 09:35:05 GMT
Something you might try that is somewhere in between the two you suggested 
is to create a webapp called resources (or something similar) which 
contains all your images etc. Then hard code this webapp's path into the 
IMG tag. This way you could still rename your webapps without having to go 
through all the JSPs making changes. But the resources webapp will always 
have to be called the same thing.



At 11:21 16/05/2003, you wrote:

>Right you are. Thanks.
>What is the suggested alternative ? I would like to make everything relative
>(to something). I have come up with 2 solutions.
>1. Hard code the name of my web app into the jsp pages.
>    ie <IMG SRC="/MyApp/jsp/images/myImage.gif">
>    not really a good idea in my opinion since as soon as you change the
>    name of the web app you have to go through all the JSPs and make changes.
>2. Get the servlet context within the JSP and prepend
>    that to the URL with the relative path based on the root for the web app.
>    ie <IMG src="<%= getServletContext().getRealPath(
>                 "/jsp/images/SepRight.gif") %>">
>    Guaranteed to work all the time, but makes your JSPs harder to read.
>Any other reccomendations ?
>BTW, I checked back on some of the code I wrote for Websphere just to make
>sure I wasn't going crazy. I wasn't, it was written how I originally stated
>(the resources were relative to the location of the JSP file). Am I right to
>assume that this was a "feature" in the Websphere server, that does not
>conform to the Java specs ?
>-----Original Message-----
>From: news []On Behalf Of Bill Barker
>Sent: Thursday, May 15, 2003 8:36 PM
>Subject: Re: Forward to JSP from Servlet
>Nope, the problem is that you are confusing the h*ll out of the browser.
>The browser has no knowledge at all that you've done a forward, so it will
>request relative links for things like stylesheets and images against the
>URL that it sent.  It doesn't know or care about the URL that you forwarded
>You can stay with this design, but you'd have to make all of the references
>to stylesheets and images absolute in your JSP page (e.g. <img
>src="/myapp/images/cool.jpg"> instead of <img src="images/cool.jpg">).
>"Daryl Handley" <> wrote in message
> > Hi all,
> >
> > When I forward to a JSP from a servlet, my JSP page is unable to find the
> > resources (stylesheets, images, ....)it needs to display the page
> > The page comes up fine if I just type in the address of the JSP ...
> >
> > localhost/OnlineStore/jsp/index.jsp for example
> >
> > ... but if I forward to the page ....
> >
> > getsServletContext().getRequestDispatcher("/jsp/index.jsp").forward(req,
> > res);
> >
> > ... it comes up with no stylesheets applied and none of my images are
> > visible.
> >
> > I tried doing a res.sendRedirect and it worked fine (as I expected it
> > I don't really want to use sendRedirect unless I have to since it has it's
> > own problems.
> >
> > I'm assuming this has something to do with the servlet that is compiled
> > the jsp assuming the resources are going to be somewhere different, but
> > not really sure. I'm fairly new to Tomcat, but I used to do these kinds of
> > forwards all the time using Websphere with no problem. Maybe there is some
> > setting somewhere that I need to adjust ?
> >
> > Tomcat is v4.1.24
> >
> >
> > TIA
> > Daryl

View raw message