wood burning stoves 2.0*
The moose likes JSP and the fly likes including a css file to a jsp from another folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "including a css file to a jsp from another folder" Watch "including a css file to a jsp from another folder" New topic
Author

including a css file to a jsp from another folder

Jhodee Rigodon
Greenhorn

Joined: Jun 07, 2010
Posts: 5
hi all.. how can i include the link of the css file to my jsp? here's the directory hierarchy and the code i tried (but didn't work).. please help..


my jsp file is in the web folder


<head>
<title>refreshed</title>
<meta http-equiv="Content-Type" content="text/HTML; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../src/java/Style/style.css" />
</head>
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Shouldn't you have your styles (and images) in your web folder. In a typical java web-app you would have them in the same folder that contains your WEB-INF folder and then reference them absolutely rather than relatively as you have done in your example.

Sean


I love this place!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

As mentioned, all CSS and JavaScript files should be in the web content folder*. Since they're not Java source, they shouldn't be in anything called "java", because nobody would ever think to look there for non-Java resources.

* At least after deployment. Before deployment they can be anywhere you want.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

And once you've moved the file to an appropriate location, be sure that you fix the URL in your page. It should be server-relative, starting with the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jhodee Rigodon
Greenhorn

Joined: Jun 07, 2010
Posts: 5
alright..thanks for the response... it's all working now..after i transferred the images and stylesheets to the web folder
and did something like <link rel="stylesheet" type="text/css" href="Style/style.css" />

i still have a question though... well, if given the chance to still keep the original locations of the files (basing on the image), what's the reason why my initial code (<link rel="stylesheet" type="text/css" href="../src/java/Style/style.css" />) didn't work? just wanna clear things out..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Firstly, you are still using page-relative addressing, and while that may be working for you, it's fragile and not a good practice.

Secondly, it didn't work because the file wasn't part of the web app and couldn't be served.
Jhodee Rigodon
Greenhorn

Joined: Jun 07, 2010
Posts: 5
okay, so what should be the suggested way of including css files in a jsp?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

See my first reply. It's also covered in the JSP FAQ.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use an absolute path, including the app context.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

David Newton wrote:Use an absolute path, including the app context.

I prefer the term "server-relative" because most people think of "absolute" as including the http:// and domain parts.
Jhodee Rigodon
Greenhorn

Joined: Jun 07, 2010
Posts: 5
i'm kinda new in this field, so could you show me some example of that "server-relative" way of including the css??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

It's all in the JSP FAQ.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:I prefer the term "server-relative" because most people think of "absolute" as including the http:// and domain parts.

Good point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: including a css file to a jsp from another folder