This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the current host address

 
Picus Ameradeus
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way to get the current host? For example, I am testing a app that sends an email to confirm an order, when the order is inserted into the DB, the application will send an email asking the user to click a link to ocnfirm it.

I sthere a way for the app to automatically send the correct url in the email without having to hard code it into the java and recompile everytime I move it from the test server to the live server.

example:

private String url = "http://testHost/app/Confirm.do";

will need to be changed to

private String url = "http://liveHost/app/Confirm.do";

when I move it.

I know that Struts HTML tags can get the application host automatically, is there servlet code to do the same thing?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get it from the Request object.

A better solution would be to create a set of properties files (one for each environment your app runs in) and put them into a configuration directory in your WAR. Each file will have the configuration properties needed for a single environment.

You can add a system property to the app server JVM startup command to define which environment you are running in:

-Dappenv=prod

Then create a class with a static block to load the properties into a map and store the map in the Application scope. Add getter methods to the class to encapsulate access to the map.

If you don't want to use the static block, you could also do this with a servlet that is defined in the deployment descriptor to be loaded at startup.
[ September 25, 2006: Message edited by: Scott Johnson ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I agree with Scott that a properties file approach is vastly superior to trying to branch based on server name...

Originally posted by Scott Johnson:
If you don't want to use the static block, you could also do this with a servlet that is defined in the deployment descriptor to be loaded at startup.


"startup servlets" are an old-fashioned way of doing what should now be done with context listeners.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"startup servlets" are an old-fashioned way of doing what should now be done with context listeners.


Doh! You are right!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!