0

I'm having trouble accessing some images and a css file from my JSP stored in WEB-INF. My servlet calls up the JSP and that all loads fine but the page is unable to find the images and css file. I'm very new to this sort of thing and would appreciate the help. I am aware that there have been some questions on this in the past and I've tried the suggestions posed by those but I can't seem to get my head around it. Here's the contents of the JSP (I tried a bunch of different methods):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="resources/grid.css">   
</head>
<body>  
<!-- Main container -->
<div class="container_12"></div>

<!-- Foreground container -->
<div class="grid_10 prefix_1 suffix_1">

    <img src = "/resources/images/Foreground.png">

</div>

<!-- Header container -->
<div class = "grid 8 prefix_2 suffix_2">

    <img src = "${pagecontext.request.contextPath}/resources/images/Header.png">
    <p>Test</p>

</div>

<div class = "clear"></div>

<!-- Navigation bar container -->
<div class = "grid 8 prefix_2 suffix_2">

    <img src = "${pagecontext.request.contextPath}/resources/images/Navbar.png">

</div>

 </body>
 </html>

And here's my directory structure:

http://gyazo.com/0a7aea0a44e51e1789c263767e14a40b

Pradhan
  • 15
  • 4
TOFFster
  • 53
  • 3
  • 9
  • please add that resource folder inside the WEB-INF folder,i will work – abhi Apr 21 '14 at 10:20
  • I was told that I couldn't access the files if they were in the WEB-INF folder, and that was my original attempt, which didn't work either. – TOFFster Apr 21 '14 at 10:23

2 Answers2

0

You can use the below to find the real path of an image.

<!-- Header container -->
<div class = "grid 8 prefix_2 suffix_2">

    <img src = "<%= application.getRealPath("/resources/images/Header.png") %>">
    <p>Test</p>

</div>
Rahul
  • 3,479
  • 3
  • 16
  • 28
  • So will this allow me to access the files when they're in the WEB-INF folder? The directory reference you've got there points to the current directory then down to resources, right? That means the resources need to be inside of WEB-INF? Or does the client not know that the JSP file is contained inside the WEB-INF folder once it's been served up? – TOFFster Apr 21 '14 at 10:35
  • No it will not allow to access the resource file which are inside WEB-INF File. The above solution is for your mentioned dir structure in your question. – Rahul Apr 21 '14 at 10:36
  • Oh I see, so the client should believe that the files served from WEB-INF are in the standard WebContent folder? I'll give it a try now, thanks. – TOFFster Apr 21 '14 at 10:37
0

Put all the files or css-images outside the WEB-INF folder. It is good practice that in WEB-INF you put only classes and web.xml.

Matthew Green
  • 10,161
  • 4
  • 36
  • 54
Sunny
  • 308
  • 2
  • 14