It's not a secret anymore!
The moose likes JSP and the fly likes Linking External CSS in a JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Linking External CSS in a JSP page" Watch "Linking External CSS in a JSP page" New topic

Linking External CSS in a JSP page

Paul Hoffman

Joined: Nov 04, 2005
Posts: 19
Hi, I'm having a problem linking to an external CSS file in JSP. I'm simply using the following line of code, but it isn't working:

<link href="style.css" type="text/css" rel="stylesheet" />

Is there something else I need to be doing?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

The href in your link element specifes a URL-relative path that will only work if the css file is in the same folder as the JSP and if the JSP is directly addressed. Since in most web apps neither of these are the case, you need to use server-relative addressing for the files.

Let's say that the css files is in a folder named styles at the context root. Adressing it would be:

<link href="${pageContext.request.contextPath}/styles/mycss.css" ...

(assuming JSP 2.0)

[Asking smart questions] [About Bear] [Books by Bear]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Linking External CSS in a JSP page
It's not a secret anymore!