0

I am learning jsp and just made an index file in web-inf folder in eclipse project. But the css file is not getting linked with my index file. I have tried everything available on web to solve this issue but it didnt work. Index file is:

<!DOCTYPE html>

<html>
<head>
<title>Core Software Solutions</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">

The css file is in web-inf/layout/styles

2
  • are you getting a 404 ? Commented Jul 27, 2015 at 6:35
  • the index file is opening but the css file is not linked with this index.html file(the snippet above). That means the required properties specified in the css for my webpage is not being shown. Commented Jul 27, 2015 at 6:41

3 Answers 3

1

put the css file folder parrell of web-inf folder or outside of web-inf. you can able to access the css files.

directory structure for simple web application :

index.html
default.jsp
anotherJsp.jsp
images/logo.jpeg
css/abc.css
WEB-INF/classes/*.class
WEB-INF/lib/*.jar
WEB-INF/web.xml

additional web resource always should be parrell of web-inf directory and relative to your jsp or html files.

Sign up to request clarification or add additional context in comments.

10 Comments

<link href="./css/layout.css" rel="stylesheet" type="text/css" media="all">
This is the link that i gave to my css
if your jsp is parrell of css folder the you use like this. <link href="css/layout.css" rel="stylesheet" type="text/css" media="all">
The funny thing is if i open it with a web browser then the css file is linked but when i open it through tomcat server its not reading the css
can you share location of jsp or html file.
|
0

I think there are two things you need to check.

  1. The path of the css file. I guess you have to add './' in front of your href path. './' means current directory. If your html file and css file is in the same directory, this will work. Or is your css file in any different directory? Have you tried to put the absolute path of your css file? If not, try this first.

  2. If the path is okay, then check the permission of your css file.

Comments

0

I had the same problem,

I resolved it with using JSTL EL tag <c:url />. But if you want to use it in this way, you need to use .jsp file, not .html and import JSTL library.

Use tag <c:url /> this way: <link href="<c:url value="/css/style.css"/>" rel="stylesheet">

So, first thing you need to do is import JSTL library, if you have maven project, just add these lines to your pom.xml file between <dependencies></dependencies> section:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Then you need to specify prefix for using JSTL EL language in your .jsp file. So, put this line <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> above your <!Doctype> tag in your .jsp file. Now, you can use EL tags using prefix c, this is why I use <c:url />

Now about directory structure. If you use link in href attribute like in this way: <link href="<c:url value="/css/style.css"/>" rel="stylesheet"> you need to have your .jsp file on the same directory level as is your css directory. Example:

index.jsp
images/
css/style.css
WEB-INF/
META-INF/

Try it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.