How to get the server name in a Java Web Application -


i have web application users deploy on own java web servers (e.g. tomcat). java side of web application needs report url of web application (e.g. http://aserver.com:8080/myapp or https://blah.blahsever/myapp). since number of users use port-forwarding , other network techniques, web application reports wrong name.

i have tried following techniques don't produce user requires.

note (request httpservletrequest)

request.getlocaladdr();    // returns: 127.0.0.1 request.getlocalname();    // returns: localhost request.getservername();   // returns: localhost request.getserverport();   // returns: 8080 request.getcontextpath();  // returns: /myapp  request.getscheme();       // returns: http  inetaddress.getlocalhost().gethostname();           // returns: serverxxx-xxx-xxx-xx.xxxx-xxxxxxx.net inetaddress.getlocalhost().gethostaddress();        // returns: xxx.xxx.xxx.xxx (ip address) inetaddress.getlocalhost().getcanonicalhostname();  // returns: serverxxx-xxx-xxx-xx.xxxx-xxxxxxx.net 

the use of inetaddress gets close want since using server aliases , proxypass in our apache2 server, value inetaddress actual values of server rather alias.

the technique can think of round this, user provides property in properties file, web application reads on startup. if property set, value used return full web application path (e.g. serverurl = https://blah.blahserver/myapp) . technique work, involves more deployment work customers.

does have ideas how can achieve more elegant solution?

thanks, phil

check 'host' request header. client add header indicate host they're trying contact.

this same header that's used virtualhosts, it's pretty standard. it's obligatory in http1.1 requests.

in fact, cannot value purely server side, since it's possible user has defined own hostname server.

request.getheader("host") 

if server behind apache reserve proxy, use proxypreservehost directive make sure host header kept intact. i'm sure other reverse proxies have similar options.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -