You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mike Molina <sy...@hotmail.com> on 2006/07/24 19:31:52 UTC

Links to relative URL's from a JSP

This thread is related to relative URL within JSP's and how they refer to 
the local directory structure in a web application.

My directory structure in Tomcat goes something like this:

webapps/inprogress/WebContent
(Accessed through Tomcat as such: http://127.0.0.1:8080/inprogres/WebContent

where WebContent contains my JSP and HTML files, WEB-INF, etc.

When creating img links to image files in the WebContent folder, I noticed

1) HTML files can use the correct local pathname, e.g., <img 
src="imagname.jpg">

however,

2) JSP files, even though accessing image files in the same directory must 
use the previous directory, e.g., <img src="../imagename.jpg"> even though 
the images are contained in the same directory.

My assumption is that JSP files are "rooted" in the WEB-INF directory and so 
must use the previous directory notation back up a directory to access the 
images contained the WebContent folder where everything else is.

Thus, I have two questions:

1) Why does this occur, and how can I modify my xml configuration files so 
that JSP's reference links exactly as an html file in the same directory 
would?

2) How can I change the root folder of tomcat to the 
webapps/inprogress/WebContent directory, which xml files, and how exactly 
should I modify them?

Thanks,
Mike M.



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


Re: Links to relative URL's from a JSP

Posted by Michael Jouravlev <jm...@gmail.com>.
On 7/24/06, David Smith <dn...@cornell.edu> wrote:
>  > My assumption is that JSP files are "rooted" in the WEB-INF directory
> and so must use the previous directory notation back up a directory to
> access the images contained the WebContent folder where everything else is.
>
> Nope.  They are "rooted" right where they are although internal
> forwarding can make the request URL different from the actual location
> of the JSP.

Right. Mike, check this link:
http://wiki.apache.org/struts/StrutsTaglibHtmlBase Its wording could
be better, but hopefully you will get the idea. Basically, just use
<html:base/> tag. Without parameter it will "root" to the directory
where JSP is, or you can use a parameter and to "root" anywhere you
like.

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


Re: Links to relative URL's from a JSP

Posted by David Smith <dn...@cornell.edu>.
> SEVERE: Error loading WebappClassLoader
>  delegate: false
>  repositories:
>    /WEB-INF/classes/
> ----------> Parent Classloader:
> org.apache.catalina.loader.StandardClassLoader@17d5d2a
> mm_lodging1.css
> java.lang.ClassNotFoundException: catalogue.css
>     at 
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352) 
>
> [...] 


There must be something very strange with your code if a css file is 
being interpretted as a class or resource on the classpath.  How are you 
calling it?  It should be in a standard link tag in the head of your jsp 
page as in <link type="text/css" src="<%= request.getContextPath() 
%>/stylesheets/catalogue.css"> or <link type="text/css" 
src="${pageContext.request.contextPath}/stylesheets/catalogue.css"/>

--David


Mike Molina wrote:

> I'm beginning to see my problem is a bit more complex than I had 
> mentioned.
>
> Here is the situtation: I have two JSP's, login.jsp and 
> catalogue.jsp.  Login JSP calls a servlet to do some processing. If it 
> is successful, the servlet forwards the request and response objects 
> to "/catalogue.jsp".
>
> Whenever I access "catalogue.jsp" by entering in its absolute URL, 
> everything works fine.  I can still have my relative img links to the 
> images contained in the same folder, e.g., <img src="image.jpg">
>
> However, if "catalogue.jsp" is accessed by the servlet forwarding the 
> request and response objects to "catalogue.jsp" then none of the usual 
> relative URL's work.  In fact, the servlet will not even compile 
> correctly:
>
>
>
> SEVERE: Error loading WebappClassLoader
>  delegate: false
>  repositories:
>    /WEB-INF/classes/
> ----------> Parent Classloader:
> org.apache.catalina.loader.StandardClassLoader@17d5d2a
> mm_lodging1.css
> java.lang.ClassNotFoundException: catalogue.css
>     at 
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352) 
>
> [...]
>
>
>
> This is only remedied if I use relative links to the images beginning 
> with ".." For example, if the image links are constructed like <img 
> src="../image.jpg">.  Then the forwarding, runtime compilation of the 
> servlet, and the image URL's in the document work fine.
>
> To illustrate how this works, say I have 3 images, one of them with a 
> "correct" relative URL location, and the other two using "../" before 
> the image filename.  If I access the JSP using an absolute URL, one of 
> the images will work, if I use dispatcher forwarding, 2 of them will 
> work.
>
> I am using Eclipse for development.
>
> Sincerely,
> Mike M.
>
>
>> From: David Smith <dn...@cornell.edu>
>> Reply-To: "Tomcat Users List" <us...@tomcat.apache.org>
>> To: Tomcat Users List <us...@tomcat.apache.org>
>> Subject: Re: Links to relative URL's from a JSP
>> Date: Mon, 24 Jul 2006 13:50:22 -0400
>>
>> > 2) JSP files, even though accessing image files in the same 
>> directory must use the previous directory, e.g., <img 
>> src="../imagename.jpg"> even though the images are contained in the 
>> same directory.
>>
>> What does the URL in your browser look like when you are access the 
>> JSP pages?  The browser constructs the full path to the image from 
>> the URL used to request the page (JSP or HTML -- the browser doesn't 
>> know the difference).
>>
>> > My assumption is that JSP files are "rooted" in the WEB-INF 
>> directory and so must use the previous directory notation back up a 
>> directory to access the images contained the WebContent folder where 
>> everything else is.
>>
>> Nope.  They are "rooted" right where they are although internal 
>> forwarding can make the request URL different from the actual 
>> location of the JSP.  In the past, I've done stuff like <img 
>> src="${pageContext.request.contextPath}/WebContent/image.jpg"> to get 
>> around these issues with an absolute path to the image.  My example 
>> assumes a servlet spec 2.4 webapp and container.
>>
>> --David
>>
>> Mike Molina wrote:
>>
>>> This thread is related to relative URL within JSP's and how they 
>>> refer to the local directory structure in a web application.
>>>
>>> My directory structure in Tomcat goes something like this:
>>>
>>> webapps/inprogress/WebContent
>>> (Accessed through Tomcat as such: 
>>> http://127.0.0.1:8080/inprogres/WebContent
>>>
>>> where WebContent contains my JSP and HTML files, WEB-INF, etc.
>>>
>>> When creating img links to image files in the WebContent folder, I 
>>> noticed
>>>
>>> 1) HTML files can use the correct local pathname, e.g., <img 
>>> src="imagname.jpg">
>>>
>>> however,
>>>
>>> 2) JSP files, even though accessing image files in the same 
>>> directory must use the previous directory, e.g., <img 
>>> src="../imagename.jpg"> even though the images are contained in the 
>>> same directory.
>>>
>>> My assumption is that JSP files are "rooted" in the WEB-INF 
>>> directory and so must use the previous directory notation back up a 
>>> directory to access the images contained the WebContent folder where 
>>> everything else is.
>>>
>>> Thus, I have two questions:
>>>
>>> 1) Why does this occur, and how can I modify my xml configuration 
>>> files so that JSP's reference links exactly as an html file in the 
>>> same directory would?
>>>
>>> 2) How can I change the root folder of tomcat to the 
>>> webapps/inprogress/WebContent directory, which xml files, and how 
>>> exactly should I modify them?
>>>
>>> Thanks,
>>> Mike M.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>>
>
>
>
> ---------------------------------------------------------------------
> 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
>


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


Re: Links to relative URL's from a JSP

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mike,

> Whenever I access "catalogue.jsp" by entering in its absolute URL,
> everything works fine.  I can still have my relative img links to the
> images contained in the same folder, e.g., <img src="image.jpg">
> 
> However, if "catalogue.jsp" is accessed by the servlet forwarding the
> request and response objects to "catalogue.jsp" then none of the usual
> relative URL's work.  In fact, the servlet will not even compile correctly:

I think these are unrelated issues; you are getting a
ClassNotFoundException, not a JSP compilation error. Why is something
looking for a class called catalogue.css?

The standard practice for including "relative" URLs is, in fact, /not/
to use them. Instead, use absolute URLs -- but since the "context path"
can change given the deployment configuration, you can use the following
(relatively standard) trick:

<img src="<%= request.getContextPath() %>/your/full/path/image.png" />
instead of
<img src="../relative/path/image.png" />

Note that if your webapp's context name is "myApp", then the URL will
look like this:

/myApp/your/full/path/image.png

but you should omit the "/myApp" in the path that you include in your
JSP. Otherwise, the URL generated will be "/myApp/myApp/your/full/...".

-chris



Re: Links to relative URL's from a JSP

Posted by Gustavo Carvalho <gu...@yahoo.com.br>.
Maybe using 'getServletContext()' u can solve the problem.

Mike Molina <sy...@hotmail.com> escreveu: I'm beginning to see my problem is a bit more complex than I had mentioned.

Here is the situtation: I have two JSP's, login.jsp and catalogue.jsp.  
Login JSP calls a servlet to do some processing. If it is successful, the 
servlet forwards the request and response objects to "/catalogue.jsp".

Whenever I access "catalogue.jsp" by entering in its absolute URL, 
everything works fine.  I can still have my relative img links to the images 
contained in the same folder, e.g., 

However, if "catalogue.jsp" is accessed by the servlet forwarding the 
request and response objects to "catalogue.jsp" then none of the usual 
relative URL's work.  In fact, the servlet will not even compile correctly:



SEVERE: Error loading WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@17d5d2a
mm_lodging1.css
java.lang.ClassNotFoundException: catalogue.css
 at 
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352)
[...]



This is only remedied if I use relative links to the images beginning with 
".." For example, if the image links are constructed like 
src="../image.jpg">.  Then the forwarding, runtime compilation of the 
servlet, and the image URL's in the document work fine.

To illustrate how this works, say I have 3 images, one of them with a 
"correct" relative URL location, and the other two using "../" before the 
image filename.  If I access the JSP using an absolute URL, one of the 
images will work, if I use dispatcher forwarding, 2 of them will work.

I am using Eclipse for development.

Sincerely,
Mike M.


>From: David Smith 
>Reply-To: "Tomcat Users List" 
>To: Tomcat Users List 
>Subject: Re: Links to relative URL's from a JSP
>Date: Mon, 24 Jul 2006 13:50:22 -0400
>
> > 2) JSP files, even though accessing image files in the same directory 
>must use the previous directory, e.g.,  even 
>though the images are contained in the same directory.
>
>What does the URL in your browser look like when you are access the JSP 
>pages?  The browser constructs the full path to the image from the URL used 
>to request the page (JSP or HTML -- the browser doesn't know the 
>difference).
>
> > My assumption is that JSP files are "rooted" in the WEB-INF directory 
>and so must use the previous directory notation back up a directory to 
>access the images contained the WebContent folder where everything else is.
>
>Nope.  They are "rooted" right where they are although internal forwarding 
>can make the request URL different from the actual location of the JSP.  In 
>the past, I've done stuff like 
>src="${pageContext.request.contextPath}/WebContent/image.jpg"> to get 
>around these issues with an absolute path to the image.  My example assumes 
>a servlet spec 2.4 webapp and container.
>
>--David
>
>Mike Molina wrote:
>
>>This thread is related to relative URL within JSP's and how they refer to 
>>the local directory structure in a web application.
>>
>>My directory structure in Tomcat goes something like this:
>>
>>webapps/inprogress/WebContent
>>(Accessed through Tomcat as such: 
>>http://127.0.0.1:8080/inprogres/WebContent
>>
>>where WebContent contains my JSP and HTML files, WEB-INF, etc.
>>
>>When creating img links to image files in the WebContent folder, I noticed
>>
>>1) HTML files can use the correct local pathname, e.g., 
>>src="imagname.jpg">
>>
>>however,
>>
>>2) JSP files, even though accessing image files in the same directory must 
>>use the previous directory, e.g.,  even though 
>>the images are contained in the same directory.
>>
>>My assumption is that JSP files are "rooted" in the WEB-INF directory and 
>>so must use the previous directory notation back up a directory to access 
>>the images contained the WebContent folder where everything else is.
>>
>>Thus, I have two questions:
>>
>>1) Why does this occur, and how can I modify my xml configuration files so 
>>that JSP's reference links exactly as an html file in the same directory 
>>would?
>>
>>2) How can I change the root folder of tomcat to the 
>>webapps/inprogress/WebContent directory, which xml files, and how exactly 
>>should I modify them?
>>
>>Thanks,
>>Mike M.
>>
>>
>>
>>---------------------------------------------------------------------
>>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
>>
>
>
>---------------------------------------------------------------------
>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
>



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



 		
---------------------------------
 Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora!
 		
---------------------------------
 Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora!

Re: Links to relative URL's from a JSP

Posted by Mike Molina <sy...@hotmail.com>.
I'm beginning to see my problem is a bit more complex than I had mentioned.

Here is the situtation: I have two JSP's, login.jsp and catalogue.jsp.  
Login JSP calls a servlet to do some processing. If it is successful, the 
servlet forwards the request and response objects to "/catalogue.jsp".

Whenever I access "catalogue.jsp" by entering in its absolute URL, 
everything works fine.  I can still have my relative img links to the images 
contained in the same folder, e.g., <img src="image.jpg">

However, if "catalogue.jsp" is accessed by the servlet forwarding the 
request and response objects to "catalogue.jsp" then none of the usual 
relative URL's work.  In fact, the servlet will not even compile correctly:



SEVERE: Error loading WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@17d5d2a
mm_lodging1.css
java.lang.ClassNotFoundException: catalogue.css
	at 
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1352)
[...]



This is only remedied if I use relative links to the images beginning with 
".." For example, if the image links are constructed like <img 
src="../image.jpg">.  Then the forwarding, runtime compilation of the 
servlet, and the image URL's in the document work fine.

To illustrate how this works, say I have 3 images, one of them with a 
"correct" relative URL location, and the other two using "../" before the 
image filename.  If I access the JSP using an absolute URL, one of the 
images will work, if I use dispatcher forwarding, 2 of them will work.

I am using Eclipse for development.

Sincerely,
Mike M.


>From: David Smith <dn...@cornell.edu>
>Reply-To: "Tomcat Users List" <us...@tomcat.apache.org>
>To: Tomcat Users List <us...@tomcat.apache.org>
>Subject: Re: Links to relative URL's from a JSP
>Date: Mon, 24 Jul 2006 13:50:22 -0400
>
> > 2) JSP files, even though accessing image files in the same directory 
>must use the previous directory, e.g., <img src="../imagename.jpg"> even 
>though the images are contained in the same directory.
>
>What does the URL in your browser look like when you are access the JSP 
>pages?  The browser constructs the full path to the image from the URL used 
>to request the page (JSP or HTML -- the browser doesn't know the 
>difference).
>
> > My assumption is that JSP files are "rooted" in the WEB-INF directory 
>and so must use the previous directory notation back up a directory to 
>access the images contained the WebContent folder where everything else is.
>
>Nope.  They are "rooted" right where they are although internal forwarding 
>can make the request URL different from the actual location of the JSP.  In 
>the past, I've done stuff like <img 
>src="${pageContext.request.contextPath}/WebContent/image.jpg"> to get 
>around these issues with an absolute path to the image.  My example assumes 
>a servlet spec 2.4 webapp and container.
>
>--David
>
>Mike Molina wrote:
>
>>This thread is related to relative URL within JSP's and how they refer to 
>>the local directory structure in a web application.
>>
>>My directory structure in Tomcat goes something like this:
>>
>>webapps/inprogress/WebContent
>>(Accessed through Tomcat as such: 
>>http://127.0.0.1:8080/inprogres/WebContent
>>
>>where WebContent contains my JSP and HTML files, WEB-INF, etc.
>>
>>When creating img links to image files in the WebContent folder, I noticed
>>
>>1) HTML files can use the correct local pathname, e.g., <img 
>>src="imagname.jpg">
>>
>>however,
>>
>>2) JSP files, even though accessing image files in the same directory must 
>>use the previous directory, e.g., <img src="../imagename.jpg"> even though 
>>the images are contained in the same directory.
>>
>>My assumption is that JSP files are "rooted" in the WEB-INF directory and 
>>so must use the previous directory notation back up a directory to access 
>>the images contained the WebContent folder where everything else is.
>>
>>Thus, I have two questions:
>>
>>1) Why does this occur, and how can I modify my xml configuration files so 
>>that JSP's reference links exactly as an html file in the same directory 
>>would?
>>
>>2) How can I change the root folder of tomcat to the 
>>webapps/inprogress/WebContent directory, which xml files, and how exactly 
>>should I modify them?
>>
>>Thanks,
>>Mike M.
>>
>>
>>
>>---------------------------------------------------------------------
>>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
>>
>
>
>---------------------------------------------------------------------
>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
>



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


Re: Links to relative URL's from a JSP

Posted by David Smith <dn...@cornell.edu>.
 > 2) JSP files, even though accessing image files in the same directory 
must use the previous directory, e.g., <img src="../imagename.jpg"> even 
though the images are contained in the same directory.

What does the URL in your browser look like when you are access the JSP 
pages?  The browser constructs the full path to the image from the URL 
used to request the page (JSP or HTML -- the browser doesn't know the 
difference).

 > My assumption is that JSP files are "rooted" in the WEB-INF directory 
and so must use the previous directory notation back up a directory to 
access the images contained the WebContent folder where everything else is.

Nope.  They are "rooted" right where they are although internal 
forwarding can make the request URL different from the actual location 
of the JSP.  In the past, I've done stuff like <img 
src="${pageContext.request.contextPath}/WebContent/image.jpg"> to get 
around these issues with an absolute path to the image.  My example 
assumes a servlet spec 2.4 webapp and container. 

--David

Mike Molina wrote:

> This thread is related to relative URL within JSP's and how they refer 
> to the local directory structure in a web application.
>
> My directory structure in Tomcat goes something like this:
>
> webapps/inprogress/WebContent
> (Accessed through Tomcat as such: 
> http://127.0.0.1:8080/inprogres/WebContent
>
> where WebContent contains my JSP and HTML files, WEB-INF, etc.
>
> When creating img links to image files in the WebContent folder, I 
> noticed
>
> 1) HTML files can use the correct local pathname, e.g., <img 
> src="imagname.jpg">
>
> however,
>
> 2) JSP files, even though accessing image files in the same directory 
> must use the previous directory, e.g., <img src="../imagename.jpg"> 
> even though the images are contained in the same directory.
>
> My assumption is that JSP files are "rooted" in the WEB-INF directory 
> and so must use the previous directory notation back up a directory to 
> access the images contained the WebContent folder where everything 
> else is.
>
> Thus, I have two questions:
>
> 1) Why does this occur, and how can I modify my xml configuration 
> files so that JSP's reference links exactly as an html file in the 
> same directory would?
>
> 2) How can I change the root folder of tomcat to the 
> webapps/inprogress/WebContent directory, which xml files, and how 
> exactly should I modify them?
>
> Thanks,
> Mike M.
>
>
>
> ---------------------------------------------------------------------
> 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
>


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