7

My jsp lies at below location

http://myApp.com/myWebApp/customer/images/customer.jsp

My image(accessed thru customer.jsp) lies at

http://myApp.com/myWebApp/images/customer.gif

In image tag i am making the absoulte path as below

src="${param.contextPath}images/customer.gif"

just for info i am using param here becoz this is how we access request params in EL.

I was expecting that ${param.contextPath} will return /myWebApp/ but it is returning /myWebApp/customer/. Is there a way i can get just context path(not with customer namespace) i.e /myWebApp/ from request?

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • 1
    Just change it to `src="customer.gif"`. Or was there a reason for the context path? – Russell Gutierrez Oct 03 '12 at 09:20
  • 1
    Using relative paths from within JSP files can lead to trouble under some circumstances (for example, when the JSP file is included by another JSP file somewhere else in the directory hierarchy). – Isaac Oct 03 '12 at 09:25

2 Answers2

25

If you read the context path from a request parameter, then someone must have populated it beforehand with some value. That value was incorrect, and as there is no information as to who populated this value and how, not much can be said about it.

Try using ${pageContext.request.contextPath} instead. That is the standard method to obtain the context path under which your application is deployed.

Isaac
  • 16,458
  • 5
  • 57
  • 81
6

Use ${pageContext.request.contextPath} instead

Anshu
  • 7,783
  • 5
  • 31
  • 41