Referencing CSS in jsp via$pagecontext.request.contextpath in tomcat8 does not work

Asked

Viewed 818 times

1

I am using Tomcat 8. My css and js files do not load into jsp via $pagecontext.request.contextpath in tomcat8. I can only load css if I set the absolute path from the webapp directory. Ex(pages/css/style.css).

The structure of the project is as follows:

src
   webapp
      pages
         css
         js

Obs: My jsp files are in the same hierarchy as js and css folders.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Yellow Pages</display-name>
    <welcome-file-list>
        <welcome-file>paginas/index.jsp</welcome-file>
    </welcome-file-list> ...

The Head of the jsp:

<link  type="text/css" href="${request.contextPath}css/bootstrap.min.css" rel="stylesheet">
<link  type="text/css" href="${request.contextPath}css/bootstrap-theme.min.css" rel="stylesheet">

Any help is welcome, thank you!

2 answers

3

Tried to use it this way?

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

If this doesn’t work, try placing the value of ${pageContext.request.contextPath} inside an input to know what value it stores:

<input value="${pageContext.request.contextPath}">
  • After trying, I found that it was being rendered as text. Thank you!

2


The following jsp configuration in web.xml was wrong:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

After much research, I found someone with the same problem in this link, found that the specified version of JSP did not support Expression Language.

I inserted the following most updated specification and succeeded:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"
    >

Browser other questions tagged

You are not signed in. Login or sign up in order to post.