<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Resource Url Problems</title>
	<atom:link href="http://iderror.com/errors/java/java-jsp/resource-url-problems/feed/" rel="self" type="application/rss+xml" />
	<link>http://iderror.com/errors/java/java-jsp/resource-url-problems/</link>
	<description>You have errors, we have solutions!</description>
	<lastBuildDate>Thu, 07 May 2009 00:45:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: admin</title>
		<link>http://iderror.com/errors/java/java-jsp/resource-url-problems/comment-page-1/#comment-7958</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Fri, 20 Mar 2009 10:28:59 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/idError/?p=8030#comment-7958</guid>
		<description>The solution is to use server-relative URLs to all external resources in your JSPs. This includes images, style sheets and script files.

Server-relative URLs begin with the context path to the web application. Let&#039;s say that you have your web application mapped to context path /xyz. If the images are located in a folder named images at the root of the web app (the same folder in which WEB-INF resides), the proper URL to reference an image file named whatever.jpg would be:
/xyz/images/whatever.jpg
This allows us to directly reference the image, and is completely independent of whatever URL the browser thinks is currently loaded. Morever, this URL can be used on any page in the web application and it will correctly address the resource. 
But we never want to hard-code the context path into our URLs cause this is a very bad idea.
We want to dynamically grab the context path so that it doesn&#039;t matter to the pages what the context path happens to be. To do so in JSP 2.0, we can use an EL expression as follows:
${pageContext.request.contextPath}
If you are still stuck in the dark ages using JSP 1.x, the following scriptlet expression will do the trick:
&lt;%= request.getContextPath() %&gt;
An example using this in an image tag:
 &lt;img src=&quot;${pageContext.request.contextPath}/images/whatever.jpg&quot;/&gt;
or in a script tag:
&lt;script src=&quot;${pageContext.request.contextPath}/scripts/something.js&quot;&gt;&lt;/script&gt;
Using server-relative addressing ensures that our external resources can always be successfully referenced from any page in the web application. </description>
		<content:encoded><![CDATA[<p>The solution is to use server-relative URLs to all external resources in your JSPs. This includes images, style sheets and script files.</p>
<p>Server-relative URLs begin with the context path to the web application. Let&#8217;s say that you have your web application mapped to context path /xyz. If the images are located in a folder named images at the root of the web app (the same folder in which WEB-INF resides), the proper URL to reference an image file named whatever.jpg would be:<br />
/xyz/images/whatever.jpg<br />
This allows us to directly reference the image, and is completely independent of whatever URL the browser thinks is currently loaded. Morever, this URL can be used on any page in the web application and it will correctly address the resource.<br />
But we never want to hard-code the context path into our URLs cause this is a very bad idea.<br />
We want to dynamically grab the context path so that it doesn&#8217;t matter to the pages what the context path happens to be. To do so in JSP 2.0, we can use an EL expression as follows:<br />
${pageContext.request.contextPath}<br />
If you are still stuck in the dark ages using JSP 1.x, the following scriptlet expression will do the trick:<br />
< %= request.getContextPath() %><br />
An example using this in an image tag:<br />
 <img src="${pageContext.request.contextPath}/images/whatever.jpg"/><br />
or in a script tag:<br />
<script src="${pageContext.request.contextPath}/scripts/something.js"></script><br />
Using server-relative addressing ensures that our external resources can always be successfully referenced from any page in the web application.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
