16

In Eclipse, I created a Dynamic Web Project and a JSP file under WebContent folder. I also created a CSS file under the WebContent folder. Then I use <link rel="stylesheet" type="text/css" href="XXX.css"> in the JSP to link to the CSS file but when I run on web server (Tomcat) the CSS didn't apply. Can someone tell me why?

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
user1056648
  • 193
  • 1
  • 1
  • 7
  • possible duplicate of [Browser can't access CSS and images when calling a Servlet which forwards to a JSP](http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-a-j/3658735#3658735) – BalusC Dec 06 '11 at 12:45

4 Answers4

24

You must put your web project name before the address path of your css file

Example:

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

or in more dynamic way:

<link rel="stylesheet" href="${pageContext.request.contextPath}/XXX.css" />

Have fun :)

kia kaviani
  • 241
  • 1
  • 2
7

You can use: With style.css file in WEB-INF/jsp folder

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

NOTE

This however copies the entire source of the CSS file into the HTML output of the JSP page. In other words, this is a server-side include, not a client-side resource reference. So you effectively miss the advantage that the browser can cache static resources and this way you end up with a bandwidth waste because the very same CSS file is embedded in every single page. In other words, a bad idea in terms of performance and efficiency.

as @BalusC described in comment! you want to test your style.css file anyway, this is a solution.

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
  • 6
    This however copies the entire source of the CSS file into the HTML output of the JSP page. In other words, this is a server-side include, not a client-side resource reference. So you effectively miss the advantage that the browser can cache static resources and this way you end up with a bandwidth waste because the very same CSS file is embedded in every single page. In other words, a bad idea in terms of performance and efficiency. – BalusC Jun 08 '12 at 03:07
0

you can use

  <link rel="stylesheet" type="text/css" href="path/css">
Deepak Singh
  • 460
  • 2
  • 7
  • 19
0

You should restart eclipse so that it maps all css and javascript files again. I worked for me.

Vaibhav Shah
  • 346
  • 2
  • 5