Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource
Asked Answered
C

2

14

I have a dynamic web project called BookShopWeb which I created in eclipse, with the following directory structure

/BookShopWeb/|
  |--src
  |---WebContent
                | 
                |---META-INF
                |----WEB-INF---web.xml
                            |
                            |--css--styles.css
                            |--jsp---index.jsp 

In web.xml I set the start page as

<welcome-file-list>

<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>

In the index.jsp I am including the css as

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

The index page when loaded however does not show the css info.I checked the element with firebug and it shows an error report

Apache Tomcat/6.0.29 - Error report..
The requested resource (/css/styles.css) is not available.

Any idea why this occurs?How can I correct this? thanks mark

Crusty answered 22/4, 2011 at 4:19 Comment(0)
S
22

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:

Strenta answered 22/4, 2011 at 4:21 Comment(3)
if JSP files are kept in /WEB-INF, how should I write field in href?Complemental
@shirley: just specify URL of dispatching servlet. If you're completely new to servlets, just read stackoverflow.com/tags/servlets/infoStrenta
I have the same problem. I put css folder in webapp and my jsp is in WEB-INF...I still can't get the reference to work. Is there some confifugration to make in web.xml servelet dispatcher for that also ?Issie
C
6

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>
Circumvent answered 22/4, 2011 at 4:21 Comment(2)
The css file was styles.css .I made mistake while typing the question.Crusty
okay. But your directory structure should be as in my answer.Circumvent

© 2022 - 2024 — McMap. All rights reserved.