0

I try to add a CSS file to an JSP which is running on tomcat 8. The CSS just changes the appearance of tables. The CSS file is in the same folder as the JSP. I tried using:

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

but it didn't show any changes. So I tried:

<style type="text/css">
<%@ include file="./table.css" %>
</style>

But this gives me a weird error, when I try to reach the page in my browser on the first try I get 404 - Resource not found but when I try again it works. What can cause this and is there an easier way to import my CSS file in the JSP? I use a servlet to reach the JSP if that matters.

Edit:// I just checked the WAR File i exported and the WEB-INF Folder only contains my classes the Folders of the HTML and JSP Sites are on the root directory of the WAR file.

Aragok
  • 303
  • 7
  • 16
  • 1
    The 404 suggests to me that the path you specify for table.css is not correct. Check your WAR packaging and deployment. You should have a WAR file. – duffymo Dec 12 '15 at 22:11
  • The funny thing with the 2nd try is that the error just appears at the first try to reach the site, when i reload the page the jsp works and even the css shows up. – Aragok Dec 12 '15 at 22:13
  • Have you tried just using <%@ include file="table.css" %>? Are your JSPs inside WEB-INF directory? – Keammoort Dec 12 '15 at 22:14
  • I used the include statement within the style tag in the page header. My JSPs are in the "WebContent" folder which Eclipse automatically created. the specific JSP i'm talking about is in "WebContent/admin/". The CSS file is in the same dir. – Aragok Dec 12 '15 at 22:18
  • What is the location of your css file? – Michał Urbaniak Dec 12 '15 at 22:18
  • Its in the same directory as the JSP "WebContent/admin" – Aragok Dec 12 '15 at 22:19
  • Eclipse - two things you don't understand. I'd recommend learning about how a WAR file is created and check all the things that Eclipse is doing for you behind the scenes. Remaining ignorant and depending too much on IDEs and wizards to do the right thing is dangerous. – duffymo Dec 12 '15 at 22:32
  • Did you even look for the answer? – i am me Dec 12 '15 at 22:32
  • Possible duplicate of [ is not working](http://stackoverflow.com/questions/15356695/link-rel-stylesheet-href-css-style-css-is-not-working) – i am me Dec 12 '15 at 22:34
  • @duffymo yes i looked for an answer but everthing gives me an 404 or just simply doesn't work. – Aragok Dec 12 '15 at 22:37
  • You didn't know what to look for. Find the exploded WAR file that Eclipse created for you and compare it to what you learn when you read this: https://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html – duffymo Dec 12 '15 at 22:38
  • I compared it, in eclipse i had to move the file to the root and it loads without a problem with "". – Aragok Dec 12 '15 at 22:51

1 Answers1

1

I'd put all your .css files in a folder named /css right under the root of your WAR. The path would be css/table.css.

Same for JavaScript: create a folder named /js right under the root of your WAR. The path to JavaScript is js/foo.js

duffymo
  • 305,152
  • 44
  • 369
  • 561