Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource
Solution 1:
Files in /WEB-INF
folder are not public accessible. Put the CSS files one level up, in the WebContent
folder, and ensure that they are accessible by entering their URL straight in the browser address bar. Also, the URL which you specify in the <link href>
must be relative to the request URL (which you see in the browser address bar when opening the JSP), not to its location on the server disk file system. The best approach is to make it domain-relative by starting with a forward slash /
.
<link rel="stylesheet" href="/BookShopWeb/css/styles.css" />
or a bit more dynamically so that you don't need to change your JSPs everytime whenever you change the context path
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />
JSP files can be kept in /WEB-INF
, but this way they are only accessible through a dispatching servlet, either homegrown by extending HttpServlet
or implicitly by the servletcontainer such as the <welcome-file>
.
See also:
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
- JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
Solution 2:
Your directory structure should be
/BookShopWeb/|
|--src
|---WebContent
|
|---META-INF
|----WEB-INF---web.xml
|
|--css--styles.css
|--jsp---index.jsp
Also You named your css as styles.jsp which is not proper way to declare a css file.
In your web.xml:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
In your jsp file:
<head>
<link rel="stylesheet" type="text/css" href="./css/styles.css" />
</head>