tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier ...@ice-sa.com>
Subject Re: Links in CSS vs JSPs
Date Tue, 20 Sep 2011 20:45:53 GMT
Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> André,
> 
> On 9/19/2011 6:06 PM, André Warnier wrote:
>> What I do not understand here (maybe due to my lack of knowledge of
>> JSP pages logic) is why one would need to "fix" anything, if the
>> links are correctly spelled-out in the first place, taking into
>> account how a browser handles absolute/relative links.
>>
>> e.g. imagine this structure :
>>
>> (catalina_base)/webapps/ myapp/ (contains *.JSP pages) /images/
>> (contains images) /css/ (contains stylesheets)
>>
>> If the JSP pages contain links to stylesheets located in
>> "myapp/css", their reference should say : href(or
>> src)="css/name-of-stylesheet.css"
> 
> You can't do this unless all your JSPs are at the same directory
> level, say, in "myapp".

Why not ?
Suppose all stylesheets are in (tomcat_dir)/webapps/myapp/css/, and all images in 
(tomcat_dir)/webapps/myapp/images/, then
if you have this JSP in e.g. (tomcat_dir)/webapps/myapp/level2/mypage.jsp, and it refers 
to a stylesheet, the link should be "../css/mystyle.css", that's all.
And if you have this JSP in e.g. (tomcat_dir)/webapps/myapp/level2/level3/mypage.jsp, and

it refers to a stylesheet, the link should be "../../css/mystyle.css", that's all.
(You always know where, *relative to your JSP*, the stylesheet is.)

And if any of these stylesheets refers to an image, it should do it as 
"../images/image.jpg", because by the time the broser fetches the stylesheet, the "base 
href" is the URL whence the stylesheet came from, thus always
"http://myhost.cpy.com/myapp/css/mystyle.css".
To fetch the image, the browser will
- strip the last element of the stylesheet path (mystyle.css), leaving
"http://myhost.cpy.com/myapp/css/"
- add the image URL ("../images/image.jpg"), thus ending up
"http://myhost.cpy.com/myapp/css/../images/image.jpg", which is a valid and correct URL.

You can redo this with the /myapp/level2/level3 JSP and stylesheet if you want, it does 
not make a difference.



> 
>> If the css files in turn contain links to images, they can be
>> expressed as : src="../images/some-image.jpg" And so on.
> 
> This doesn't work if there are JSPs included in others from various
> levels in the directory tree. It works great when the world is flat...
> after that, everything goes to hell.

But it does work.  If you have JSP's that include others, that gets resolved at the server

level, no ?  By the time the browser gets this one *html* page, it is coming from one 
definite URL (the URL from which the browser requested it), so the browser has no doubt.

The doubt comes only in a (contrived) case like the following :
Imagine you have a JSP containing a link to a stylesheet as follows :
<style src="utilities/servlets/css_servlet?css=somedir/mystyle.css" ..>
(with "css_servlet" being some code which cleverly retrieves the named css file from 
somewhere else altogether), then by the time the browser gets this stylesheet, it thinks 
its base path is "http://myhost.cpy.com/myapp/utilities/servlets/css_servlet", and /then/,

if this css file contains a reference to an image (like "../images/image.jpg"), the 
browser will be confused, because it will "build" the URL to this image as
"http://myhost.cpy.com/myapp/utilities/images/image.jpg"
and thus get it wrong (probably).

So don't do that.

The point is, if you use relative paths properly, then you can zip your entire app, give 
the zip your designer who will unzip it in his directory /chris/project1/, and work on it

with a local html design tool. When he/she is done, "it" will zip it again and send it 
back to you to unzip wherever you feel like (at webapps/my_newapp if you like), and all 
links should still work.

It's only if you start playing ganes like above that everything falls apart.  And as soon

as you start using absolute links ... forever rule you it will, the dark side of the force.


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


Mime
View raw message