Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

including a css file to a jsp from another folder

 
Jhodee Rigodon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jhodee Rigodon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, so what should be the suggested way of including css files in a jsp?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my first reply. It's also covered in the JSP FAQ.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an absolute path, including the app context.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the JSP FAQ.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I prefer the term "server-relative" because most people think of "absolute" as including the http:// and domain parts.

Good point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic