0

I'm developing a webapp on Eclipse and this is the file system directory structure:

myapp
   - src
   - build
   - WebContent
      - pages
      - css
      - images
      - modules
      - META-INF
      - WEB-INF

When I run this webapp with Tomcat (Run from Eclipse) I can load in the browser the jsp pages contained in (myapp -> WebContent -> pages) using for example this URL:

http://hostname:8080/myapp/pages/somepage.jsp

However the problem is that the loading of the css and images in such somepage.jsp fail. In the code of the somepage.jsp I pointed the css in this way:

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

And the image in this way:

<img src="../images/someimage.png"/>

Problem is I have no clue why such images and css are not loaded.

2 Answers2

0

The problem is in the relative path, always a safer approach to do something like

<link href="<%=request.getContextPath()%>/css/new_style.css" rel="stylesheet" type="text/css"/>

or a JSTL equivalent

<link href="${pageContext.request.contextPath}/css/new_style.css" rel="stylesheet" type="text/css"/>

than you're starting from the root, accounting for context path as well

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • What you suggested gives exactly the same result and that's the proof that I referenced the css correctly in the jsp page. But the problem remains in fact when I contact via browser the css I just get a blank page (and NOT even a 404 error). –  Nov 05 '14 at 09:29
  • could be a number of different reasons, most likely something in your eclipse setup. You most likely use wtp plugin, check inside your workspace for the workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps and see what is actually deployed under your webapp – Master Slave Nov 05 '14 at 09:33
  • I looked in this path: workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps and I found a folder "myapp" containing all the folders contained in myapp's WebContent folder (images and css included). –  Nov 05 '14 at 09:55
  • its the exact location from which the tomcat is serving your content. To debug further enable the directory listing on tomcat as suggested here http://stackoverflow.com/questions/7068046/how-can-i-list-all-the-files-in-folder-on-tomcat, and try navigating to css or image file. No further ideas – Master Slave Nov 05 '14 at 09:59
  • This is the reason of the problem: "Resource interpreted as Image but transferred with MIME type text/plain" in fact that's what I see in the browser when I inspect the page. –  Nov 05 '14 at 16:57
0

Better way is to use ${pageContext.request.contextPath} as it returns the name of the application's context.

For ex,

myapp/css/new_style.css can be dyanmically formed as {pageContext.request.contextPath}myapp/css/new_style.css

Have a look here to know What does "./" (dot slash) refer to in terms of an HTML file path location?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56