Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 409
markjason72 Avatar asked Apr 22 '11 04:04

markjason72


People also ask

Which of the following ways will be used to access the JSP page if we put the JSP file Login JSP inside the Web-INF folder?

so how can i access those jsps. You can access them by forwarding to them from another resource that is available to the web (an JSP that is not under WEB-INF, or a controller servlet). You can also grant access to them by creating a servlet entry for each JSP with a corresponding servlet-mapping entry.

What is WEB-INF folder?

WEB-INF. This directory, which is contained within the Document Root, is invisible from the web container. It contains all resources needed to run the application, from Java classes, to JAR files and libraries, to other supporting files that the developer does not want a web user to access.

Where can I find WEB-INF?

WEB-INF folder is under public_html folder. You can configure and add your content to your site yourself with. class files.

What are JSP files?

A JSP page is a text document that contains two types of text: static data, which can be expressed in any text-based format (such as HTML, SVG, WML, and XML), and JSP elements, which construct dynamic content. The recommended file extension for the source file of a JSP page is . jsp.


1 Answers

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"
like image 98
BalusC Avatar answered Sep 19 '22 15:09

BalusC