Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I detect the server info?  RSS feed

 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain my current approach for this:

I'm storing some values in web.xml:


In my JSP, I need to pull a key using application.getInitParameter("testKey") or application.getInitParameter("localhostKey"), depending on the server environment.

My question is, how do I detect the server's IP address? Right now, I'm using testing for (request.getRemoteHost().equals("127.0.0.1")), which lets me know I'm on my local environment. But how would I know information about the remote environment?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have two values? Detecting upon IP address is fragile and not a robust way to deal with the issue.

Rather, have a single value that is set appropriately for each server.
 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Why have two values? Detecting upon IP address is fragile and not a robust way to deal with the issue.

Rather, have a single value that is set appropriately for each server.


Well, for further clarification, these are Google Maps keys, and Google validates the key against the address in the Address bar. For example, if I am browsing at http://localhost:8080/app/, then the JSP needs to pull the localhostKey; if I push the app to another server, then I'll be browsing to http://10.10.10.10:8080/app, and I'll need to use the testKey.

Ultimately, what I'm trying to do is have the key value set dynamically based on the server upon which the application is residing, without having to modify the code each time I push a WAR file to the server. I'm just not sure of the best approach.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to have distinct values in the web.xml for each server (automated building with Ant makes this easy), you could use properties files.

Detecting the server by IP or other means and branching in the code is most certainly not the best approach.
 
Trevor Price
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If you don't want to have distinct values in the web.xml for each server (automated building with Ant makes this easy), you could use properties files.


I was thinking that I might need to learn Ant to pull this off, but since I've never used it I was trying to find an easier way.

Thanks Bear.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning Ant may slow you down a little now, but it will pay for itself a zillion-fold going forward...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!