*
The moose likes JSP and the fly likes style sheet and image paths Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "style sheet and image paths" Watch "style sheet and image paths" New topic
Author

style sheet and image paths

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi guy,

Just wondering, if you want to put the path of a style sheet in a included file for a JSP (eg header.jsp is included in all my pages and has all the <head></head> info in it).

The problem is, whilst the JSPs in the root can see the style sheet, anything under a different folder can not (as they would obviously be look for the style sheet under that folder. eg:
foldername/css/default.css not css/default.css)
I was just wondering, how you can change the href so that all pages always start looking in the root. I thought you just put a slash on the start of the path (like /css/default.css) but this doesn't seem to work.

Any ideas?

PS I hope this makes sense, it was a little difficult to explain.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're right, prepending the path won't work. The browser will start at the end of the domain, which in J2EE is the start of the context.

If you have JSPs nested in directories, you can use ".." in the path to signal that the browser should look up one directory.

Example, (for a page 2 directories deep):
<link href="../../myStyle.css" rel="stylesheet" type="text/css">


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Personally, I would not use relative referencing. It creates a structure that is difficult to modify.

Placing a / at the beginning of the path will make it absolute. But as you have found, that may not be sufficient.

What you really need to prepend to the href's is the context path of your web application. This could be merely the slash for the root context, but will be something like "/xyz" for other contexts (in this case named "xyz").

If you'd rather not hard-code the context path (I don't like to) it's availabe as ${pageContext.request.contextPath} in JSP 2.0, or <%= request.getContextPath() %> in JSP 1.2.
[ February 25, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi,

Thanks for that, but I'm now gettig a different problem:

com.sun.jsp.compiler.ParseException: "Cannot read file: D:\WAS\Web\secure\includes\header.jsp "
at javax.servlet.ServletException.(ServletException.java:49)
at com.sun.jsp.JspException.(JspException.java:29)
...

But the path "D:\WAS\Web\secure\includes\init.jsp" is correct and the file does exist.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you trying to link to a file with an absolute path?

You need to make the url relative to either the calling file or relative to the root of your apps context.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
The physical path on the machine is
"D:\WAS\Web\secure\includes\header.jsp"

The JSP calling the include is index.jsp under "/secure/mainmenu.jsp"
The include file is under "/secure/includes/header.jsp"

<%@ include file="<%=request.getContextPath()%>includes/header.jsp" %>

The file is there and can be opened but WAS doesn't seem to be able to read it. Is there something else that needs to be added to tell WAS how to read request.getContextPath() methods?

PS: using JDK1.3.1 under WAS 3.5 (old i know).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

When using the include directive, you shouldn't need to use getContextPath (in fact it shouldn't work).

Try, just:
<%@ include file="<%=/includes/header.jsp" %>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

When using the include directive, you shouldn't need to use getContextPath


Correct. Since includes happen on the server side, the container knows which context the file belongs to, so such references are relative to the context root.

This is not the same as images and style sheets which are referenced by the browser on the client side where the context path must be specified.
[ February 28, 2005: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:


Correct. Since includes happen on the server side, the container knows which context the file belongs to, so such references are relative to the container root.

This is not the same as images and style sheets which are referenced by the browser on the client side where the context path must be specified.


Right, but it looks like he's just including these files, inline, with the include directive:

<%@ include file="<%=request.getContextPath()%>includes/header.jsp" %>


Which can make the conversation a little confusing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Oops! Pre-caffeine typo -- that should have been "relative to the context root". I have corrected it in my posting.

So to be completely concrete about it. In a web app with a context name of xyz if there is an include file in a folder named foo, the path would be


For an image file in a folder named images, the path would be



Note that for server-side references (includes), the context path is not specified (which also means that you can only include within your own context). For client-side references (images, style sheets, script files), the context path is mandatory (otherwise, in the stateless HTTP protocol, the server has no idea which web app to fetch the file from).
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
OK think i got an idea of what's going on now and when to use getContextPath and when not.

But hang on...

Another problem!

Method getContextPath() not found in interface javax.servlet.http.HttpServletRequest

Sorry :-)
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Bugger it, it's OK I sorted it (sort of)

Just replaced the request.getContextPath() with the physical path of file: "http://devserv/web/"

devserv = machine name web/ is root path

The / worked for all the include file though, so cheers for your help. Maybe now I can move onto bigger and better things! :-D
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: style sheet and image paths
 
Similar Threads
Servlets and CSS
change CSS rules dynamic with JavaScript
html restriction
Css External Style Sheets Location
Style Sheet Problem in Websphere