Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get CSS to work in JSP code

Status
Not open for further replies.

tommyc7

Programmer
Feb 3, 2007
33
US
I don't understand what I'm doing wrong, hopefully one of you will... My code uses templates extensively, but I think the CSS part should be pretty straightforward, so I'll provide only tiny code snippets.

Basically, the style sheet is not being applied.


Snippet 1: This code is in <WEB_ROOT>/maint/maint.jsp
Code:
<template:insert template='/myTemplate.jsp'>
...
</template:insert>


Snippet 2: This code is in <WEB_ROOT>/myTemplate.jsp'
Code:
<head>	
...
<link rel="stylesheet" href="/style.css" type="text/css" media="screen" />
</head>

style.css is in <WEB_ROOT>.

Why isn't the absolute path working for me?


Thanks,
Tom



 
Change it to this:

Code:
<head>    
...
<link rel="stylesheet" href="[!]..[/!]/style.css" type="text/css" media="screen" />
</head>

The absolute path won't work unless you defined the entire path for the href. With you just putting a '/' at first in the href, the code will default to the current directory and start the path from there.

[monkey][snake] <.
 
Will it?

The / should denote the web root.


Code:
<link rel="stylesheet" href="/style.css" type="text/css" media="screen" />

Will link to a document called /style.css under the root.

Code:
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

Will link to a document called style.css in the same directory as the current document.


It could be that the root of the site is not actually the web root. I'm not familiar with JSP nor what <WEB_ROOT> is.
Is this site hosted on a virtual server? If so then perhaps <WEB_ROOT> is referring to the server root rather than the virtual host root, in which case you may need to include the virtual server path in your path from <WEB_ROOT>

Hope that makes some sense!


<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
By <WEB_ROOT>, I just meant to highest level directory. I am using Tomcat. So, say the directory structure on my localhost is:

C:\Tools\Apache Software Foundation\Tomcat 5.5\webapps\myWebSite\WEB-INF\classes...

Then, in my example above, <WEB_ROOT> = C:\Tools\Apache Software Foundation\Tomcat 5.5\webapps\myWebSite

This directory structure will obviously be different when the project is deployed to an ISP. So, I was looking for a way to reference the relative path to this directory. Everything I read, said that the "/" is that directory when the web server is running. It works fine other types of files, it's only the CSS that is giving me a problem.

I don't think the .. will work because I'll be calling this from different directory depths.

Thanks,
Tom
 
I'm guessing the while <WEB_ROOT> is referring to the directory you said, '/' is referring to C:\

Just a guess though.

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
I apologize for being dim on the / denoting the web root directory.

However, if this is your directory setup:

Snippet 1: This code is in <WEB_ROOT>/maint/maint.jsp
Snippet 2: This code is in <WEB_ROOT>/myTemplate.jsp

This code should work:
Code:
<link rel="stylesheet" href="../style.css" type="text/css" media="screen" />

I don't think the .. will work because I'll be calling this from different directory depths.

That's exactly what the .. will accomplish. It will basically say go to the parent directory of the one I'm currently in and use the file "style.css".

The parent directory of maint is the <WEB_ROOT>.




[monkey][snake] <.
 
That's exactly what the .. will accomplish.
No it won't. You missed the "different directory depths" bit. Presumably, this bit of JSP is a template that's going to appear on every page of the site, and he wants it to link to the stylesheet whether the current page is

somepage.htm
somedir/somepage.htm
somedir/someotherdir/somepage.htm

Using a relative URL like ../ only works at one level of the tree.

What does it say in the address line of your browser when you're looking at this page, tommy?

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
You missed the "different directory depths" bit.

I was taking "different directory depths" as one call, where one file was in one directory and the other was in a different directory in a different depth.

Ok, I'll be quiet now, I've botched this one up enough already. [cry]

[monkey][snake] <.
 
Thanks for helping guys, sorry it took so long to reply, I wasn't able to get back to this....

Anyway, to answer you question, Chris, the URL is:


And I'vr tried (at least) both of these variations of the heading:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en">
<head>	
	<title>
	Category Maintenance
	</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	
	<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

</head>

and

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en">

<head>	
	<title>
	Category Maintenance
	</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	
	<link rel="stylesheet" href="/style.css" type="text/css" media="screen" />

</head>

If I move cat.jsp to the root directory, though, the style sheet is only recongized if I DON'T have the /.

So for
This works:


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en">
<head>	
	<title>
	Category Maintenance
	</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	
	<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

</head>


and this doesn't:


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en">

<head>	
	<title>
	Category Maintenance
	</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	
	<link rel="stylesheet" href="/style.css" type="text/css" media="screen" />

</head>


I'm not html/css guru, but this seems odd to me!


Thanks again!
 
So it seems your root is not what you believe it to be. An easy test to do is put a link on the page that points to the root, load up the website and check where the link takes you. That will be your root and that is the folder your stylesheet is being searched in.
Code:
<a href="/">I point to the root folder.</a>

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
Thanks!! You're absolutely right, it thinks the root is:


So, does that mean there's a mistake in my server.xml file? Shouldn't the root directory be specific to the web site and not the outer container?

Code:
<Context path="/myWebSite" docBase="/myWebSite" debug="0" reloadable="true" />

Does this look right (using Tomcat 5.5)?
 
You will need to ask that question in the forum for the server you're using. I cannot help you with server configuration.

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top