0

I'm using tomcat 7.0. Now I am facing an issue that could not load the css and js file. trying to add ${pageContext.request.contextPath} but does not work, also tried c:url tag, but getting syntax error in eclipse.

The structure of these files is:

WebContent
-content/css/lab3.css
html and js folder are under content folder as well

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>lab3</display-name>
  <welcome-file-list>
    <welcome-file>/content/html/lab3.html</welcome-file>
  </welcome-file-list>
</web-app>

Here is the html head:

<link rel="stylesheet" type= "text/css" href= "${pageContext.request.contextPath}/css/lab3.css"  media="screen, projection">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
J.L
  • 592
  • 5
  • 17
  • 35
  • Expressions like `${pageContext.request.contextPath}` cannot be used in a plain HTML file, they must be used inside a JSP file. I suggest you start by reading a few tutorials about Java servlets and JSP. – David Levesque Aug 24 '13 at 01:24
  • @DavidLevesque okay, so in the plain HTML file, how should i access the location? – J.L Aug 24 '13 at 01:30
  • You can use a relative path, e.g.: `href="../css/lab3.css"` – David Levesque Aug 24 '13 at 02:09

2 Answers2

6

EL expressions ${} doesn't run in a plain HTML file. It runs in JSP (and Facelets) files only. In your particular case, it'd be merely a matter of renaming lab3.html to lab3.jsp, or adding the following JSP servlet mapping to web.xml:

<servlet-mapping>
    <servlet-name>jsp</servlet-name> <!-- This is the default servlet name of Tomcat's own JSP servlet. To be sure, look in Tomcat's own web.xml for the exact name. -->
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

which will tell Tomcat to treat all .html files as if they are JSP files, with the consequence that EL will instantly work as well in .html files.

If none of the above is an acceptable solution, then you'd need to fall back to logical thinking and properly understanding and using relative paths. Like as in local disk file systems, the ../ brings you one folder up in URLs. Provided that the HTML file is in /content/html/lab3.html and the CSS file is in /content/css/lab3.css, then the following should do:

<link ... href="../css/lab3.css" />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

use ${request.contextPath} instead of ${pageContext.request.contextPath}

Shashi Ranjan
  • 1,491
  • 6
  • 27
  • 52