I’m working with some old jsp apps and we are moving servers and so the urls have changed. The new url’s we were given have port numbers in them – http://example.com:8686/theapp
Now this line getServletContext().getInitParameter("contextName") returns example/ instead of example:8686/.
Is there a similar function or parameter that I can use so that the port number will be displayed in the url?
The
getServletContext().getInitParameter()returns the value of a<context-param>of the given name which is hard specified inweb.xml. This is not a dynamic value. You’d basically need to edit the<context-param>in question in order to provide the “right” value.To dynamically get the port number of the current HTTP servlet request, you need to use
HttpServletRequest#getServerPort()orHttpServletRequest#getLocalPort()instead, depending on which port number exactly you’d like to obtain: the one as specified inHostheader, or the one which the server is actually using.Please note that you’d normally use
HttpServletRequest#getContextPath()to obtain the context name.