You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bill Barker <wb...@wilshire.com> on 2003/05/16 05:35:48 UTC

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

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" <da...@yahoo.com> wrote in message
news:FNEJIOJHLIMDDBPLEEHBKEKDCAAA.darylhandley72@yahoo.com...
> 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
properly.
> 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
too).
> 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
from
> the jsp assuming the resources are going to be somewhere different, but
I'm
> 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




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Forward to JSP from Servlet

Posted by William Wragg <wi...@datapro.co.uk>.
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.

Regards,

Wm.

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 [mailto:news@main.gmane.org]On Behalf Of Bill Barker
>Sent: Thursday, May 15, 2003 8:36 PM
>To: tomcat-user@jakarta.apache.org
>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
>to.
>
>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" <da...@yahoo.com> wrote in message
>news:FNEJIOJHLIMDDBPLEEHBKEKDCAAA.darylhandley72@yahoo.com...
> > 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
>properly.
> > 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
>too).
> > 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
>from
> > the jsp assuming the resources are going to be somewhere different, but
>I'm
> > 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

Apache configured to run SSL creates two differente tomcat applications??

Posted by Csongor Gyuricza <cs...@mandic.com.br>.
We are running Tomcat 4.1.12 under Apache 1.3.27. When we enable SSL on
Apache, it seems that Tomcat instances a new application, so we have one
application for the secure connection and another for the non-secure
connection. Is this correct?? Should it be this way?

We are using Java Exchange's DBConnectionBroker to manage our connections
(http://www.javaexchange.com/) and since we started using SSL, there are
always two instances of the connections pool (one for each application), and
this is causing some problems.

Any hint?

Csongor Gyuricza


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Forward to JSP from Servlet

Posted by Daryl Handley <da...@yahoo.com>.
What I ended up doing (and it saatisfies my requirements of being simple yet
without hard coding) is setting up my directorys for resources at the root
of the web app rather than within the jsp directory.

So at the root of my web app, I have the following sub directorys.

jsp
html
images
styles

Within my JSPs I have set up the URL's like this.

<IMG src="../images/image_name.html">

This works with the servlets as well since the browsers reference is

http://myDomain/myApp/servlet/myServlet

Thus when I forward to the jsp it will look for the resources at the root of
the web app (right where I left them).

Not sure if that's what you meant by "relative to the path of the servlet",
but cheers to all that helped.

Daryl




-----Original Message-----
From: mike jackson [mailto:mjackson@cdi-hq.com]
Sent: Friday, May 16, 2003 8:43 AM
To: 'Tomcat Users List'
Subject: RE: Forward to JSP from Servlet


Usually I end up setting up all of the relative paths for images and
whatnot relative to the path of the servlet.  However if you don't want
to do this, another good alternative is to map the servlet to the same
relative path as the jsp.  So if the jsp is going to be at
/OnlineStore/jsp/file.jsp, then in your web.xml you'd have a:

	<servlet-mapping>
		<servlet-name>foo</servlet-name>
		<url-pattern>/OnlineStore/jsp/foo</url-pattern>
	</servlet-mapping>

When you reference "foo" within the /OnlineStore/jsp it'll have the same
relative path as the jsp file.

--mikej
-=------
mike jackson
mjackson@cdi-hq.com



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Forward to JSP from Servlet

Posted by mike jackson <mj...@cdi-hq.com>.
Usually I end up setting up all of the relative paths for images and
whatnot relative to the path of the servlet.  However if you don't want
to do this, another good alternative is to map the servlet to the same
relative path as the jsp.  So if the jsp is going to be at
/OnlineStore/jsp/file.jsp, then in your web.xml you'd have a:

	<servlet-mapping>
		<servlet-name>foo</servlet-name>
		<url-pattern>/OnlineStore/jsp/foo</url-pattern>
	</servlet-mapping>

When you reference "foo" within the /OnlineStore/jsp it'll have the same
relative path as the jsp file.  

--mikej
-=------
mike jackson
mjackson@cdi-hq.com

> -----Original Message-----
> From: Anthony Cunningham [mailto:anthony.cb@om.asahi-kasei.co.jp]
> Sent: Friday, May 16, 2003 1:47 AM
> To: Tomcat Users List
> Subject: Re: Forward to JSP from Servlet
> 
> whether or not this is a good or correct solution what I did was
create a
> file
> called Global.jsp and include that at the top of all the other jsp
files.
> In there I set a variable to the webappname and use that variable
> everywhere
> then if the webapp name changes i just change it there
> you could also put a lot of your headers and stuff thats common to all
> jsps in
> there too.
> 
> if you change this though the changes are not picked up by tomcat, you
> have to
> resave any jsps that use it or delete tomcats work folder
> 
> Daryl Handley 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 [mailto:news@main.gmane.org]On Behalf Of Bill Barker
> > Sent: Thursday, May 15, 2003 8:36 PM
> > To: tomcat-user@jakarta.apache.org
> > 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
> > to.
> >
> > 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" <da...@yahoo.com> wrote in message
> > news:FNEJIOJHLIMDDBPLEEHBKEKDCAAA.darylhandley72@yahoo.com...
> > > 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
> > properly.
> > > 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
> > too).
> > > 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
> > from
> > > the jsp assuming the resources are going to be somewhere
different,
> but
> > I'm
> > > 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
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Forward to JSP from Servlet

Posted by Anthony Cunningham <an...@om.asahi-kasei.co.jp>.
whether or not this is a good or correct solution what I did was create a file
called Global.jsp and include that at the top of all the other jsp files.
In there I set a variable to the webappname and use that variable everywhere
then if the webapp name changes i just change it there
you could also put a lot of your headers and stuff thats common to all jsps in
there too.

if you change this though the changes are not picked up by tomcat, you have to
resave any jsps that use it or delete tomcats work folder

Daryl Handley 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 [mailto:news@main.gmane.org]On Behalf Of Bill Barker
> Sent: Thursday, May 15, 2003 8:36 PM
> To: tomcat-user@jakarta.apache.org
> 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
> to.
>
> 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" <da...@yahoo.com> wrote in message
> news:FNEJIOJHLIMDDBPLEEHBKEKDCAAA.darylhandley72@yahoo.com...
> > 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
> properly.
> > 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
> too).
> > 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
> from
> > the jsp assuming the resources are going to be somewhere different, but
> I'm
> > 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: Forward to JSP from Servlet

Posted by Daryl Handley <da...@yahoo.com>.
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 [mailto:news@main.gmane.org]On Behalf Of Bill Barker
Sent: Thursday, May 15, 2003 8:36 PM
To: tomcat-user@jakarta.apache.org
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
to.

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" <da...@yahoo.com> wrote in message
news:FNEJIOJHLIMDDBPLEEHBKEKDCAAA.darylhandley72@yahoo.com...
> 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
properly.
> 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
too).
> 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
from
> the jsp assuming the resources are going to be somewhere different, but
I'm
> 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




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org