• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

style sheet and image paths

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic