0

In reference to my older unanswered question, I have a further error / problem that is partially related to it.

generic linking, variables and paths in jsp

enter image description here

When I include my header.jsp like this:

<%@include file="/WEB-INF/view/jsp/common/header.jsp" %>

It works fine.

But does not if I do it like this:

<%@include file="${pageContext.request.contextPath}/view/jsp/common/header.jsp" %>

Error:

HTTP Status 500 - /WEB-INF/view/jsp/common/login/login.jsp (line: 8, column: 1) File "${pageContext.request.contextPath}/view/jsp/common/header.jsp" not found

The above with ${} is a proper way and thats what I had been doing in the past until I started using spring and spring security.

But I dont think its a problem of spring or spring security.

I really cant understand why WEB-INF has a weightage and How can I make my links generic (ref my old stated question)

Community
  • 1
  • 1
Masood Ahmad
  • 731
  • 4
  • 15
  • 38

1 Answers1

3

Actually, it's not the proper way in this case. The context path is not used for creating paths within the internals of your app, as in this case. Instead, it is used to generate URLs within the view layer, such as links, form URLs, and etc.

In this case, the following is correct:

<%@include file="/WEB-INF/view/jsp/common/header.jsp" %>

The include line above is referring to a resource that is packaged inside your war file. How would the context path, which is configured at the servlet container, have anything to do with the location of something packaged inside your war?

To recap, you only need to use ${pageContext.request.contextPath} to prefix URLs that are going to be placed in output to the client.

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • how can i be generic in it for links, vars, etc then? – Masood Ahmad Aug 04 '13 at 04:15
  • 1
    @MasoodAhmad I added a bit more detail. I'm not sure how else to explain it to you. They are two different scenarios and you don't use it for includes. The code that you indicated that works, which I included in my answer, is the correct way. – Steven Benitez Aug 04 '13 at 05:26
  • Going to accept your answer with Thanks !! and do tell if you have any more strategies good for refactoring / changing dir locations in future. – Masood Ahmad Aug 04 '13 at 06:16