Catfish
Catfish

Reputation: 19284

getServletContext().getInitParameter("contextName") does not return port number in the url

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?

Upvotes: 2

Views: 10228

Answers (2)

sethupathi.t
sethupathi.t

Reputation: 502

If you need port alone:

request.getLocalPort() or request.getServerPort()

If you need port along with IP and context name:

request.getContextPath() or request.getServletPath() or request.getLocalAddr()

for more information refer the below links:

http://docs.oracle.com/javaee/1.4/api/javax/servlet/ServletRequest.html
http://www.kodejava.org/examples/211.html

Upvotes: 0

BalusC
BalusC

Reputation: 1108642

The getServletContext().getInitParameter() returns the value of a <context-param> of the given name which is hard specified in web.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() or HttpServletRequest#getLocalPort() instead, depending on which port number exactly you'd like to obtain: the one as specified in Host header, or the one which the server is actually using.

Please note that you'd normally use HttpServletRequest#getContextPath() to obtain the context name.

Upvotes: 6

Related Questions