Css – In eclipse dynamic web project, how to link css to jsp file in webcontent folder

csseclipsejsptomcat

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?

Best Answer

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 :)

Related Topic