0

I'm using servlet 2.3 for my project development due to some legacy code we have. Is there a way to get the context path in the init method or any other way of the servlet?

I know it's possible on higher versions of Servlet and can get it in hacky way using the getRealPath() method on servlet 2.3. However I'm still looking for a better and cleaner code.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nikhil L
  • 175
  • 1
  • 3
  • 16
  • `ServletContext#getServletContextName()` was already added in 2.3? What newer method exactly was you talking about which you thought would solve your problem? – BalusC Mar 14 '16 at 09:00
  • yeah, I saw this method. But I have set a display name in my web.xml and using this method returns the display name. I tried removing the display name tag and it does return me the context name alright. Just wondering if I can get the context name with keeping display name tag. – Nikhil L Mar 14 '16 at 09:21
  • Don't you actually want the context path instead of the context name? I.e. the one which is available by `ServletContext#getContextPath()` since 2.5? – BalusC Mar 14 '16 at 09:28
  • I'll put it down like this :- example -> `http://localhost:8888/mycontext/somepage` - I want "/mycontext" only .. – Nikhil L Mar 14 '16 at 09:42
  • That's the context path, not context name. I fixed the terminology in your question. – BalusC Mar 14 '16 at 09:58
  • okay, sorry for my mistake. – Nikhil L Mar 14 '16 at 10:00
  • Method names already describe exactly what they return :) – BalusC Mar 14 '16 at 10:06

1 Answers1

0

I wasn't able to do this. The best way I found was to move to 2.5 spec.

Posting answer just in case anyone is trying the same.

Nikhil L
  • 175
  • 1
  • 3
  • 16