2

This is the structure: (they're in the same directory!)

Directory
|-view.jsp
|-stylesheet.css

When I do <link href="stylesheet.css" rel="stylesheet" media="screen"> The .css file does not get referenced correctly, i.e. I have no idea what path to set to get to it (if put as a URL in the browser, I get a 404).

I guess it gets translated as http://localhost:8080/myApp/stylesheet.css and then there is no mapping defined for it. Logging says:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myApp/stylesheet.css] in DispatcherServlet with name 'appServlet'

It should work like this, shouldn't it? For example, this works:

<%@ include file="include.jsp"%>

include.jsp is in the same folder as well.

1 Answer 1

3

JSP views and other resources stored under WEB-INF/ are not directly accessible to the end-user, they are web application's private resources and the server doesn't expose them. You'll have to place any public resources one level above WEB-INF/, for example:

webapp/
|-- style/
|     stylesheet.css
|-- images/
|     image1.png
|     image2.png
|-- html/
|     index.html
+- WEB-INF/
  +-- jsp/
        view.jsp
        include.jsp
Sign up to request clarification or add additional context in comments.

4 Comments

So how does a JSP (not an end-user) access a resource (a CSS), specifically, what path should webapp/WEB-INF/jsp/view.jsp set as href to reference this webapp/style/stylesheet.css?
@Bloke JSP does not access the CSS, the user's browser does it. And browser can't find resource under WEB-INF
The JSP template gets rendered to plain HTML and exposed under some publicly accessible URL; you can either use an absolute path to the CSS (i.e. "/myApp/style/stylesheet.css") or a relative path (depends on what URL the page is mapped to; if it's in the webapp's root, like /myApp/index.html, it would be "style/stylesheet.css", if it's on a different level, for example /myApp/subdir/page.html, it would be "../style/stylesheet.css")
how you put <a href="view.jsp"></a> in index.html.if want to navigate to view.jsp in web-inf folder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.