aspose file tools*
The moose likes Servlets and the fly likes get website URL in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "get website URL in a servlet" Watch "get website URL in a servlet" New topic
Author

get website URL in a servlet

Ben Hagadorn
Greenhorn

Joined: Mar 28, 2006
Posts: 25
I have a servlet who's main purpose is to run a timed job that sends email to a list of recipients when certain error conditions occur (based upon checking a table in the DB). Included in this email is a link to a web page that the user can click on to view more details about the errors.

The issue that I'm having is that I'd like to be able to dynamically determine what the URL of the website is for the email message I send. The doGet() and doPost() are never used so I have no handle to a request object -- all I have is the ServletContext object from the init() event.

I realize that I could add a configuration parameter to the web.xml file and get it by calling servletContext.getInitParameter() but I'd rather not do that: this app could be installed on several servers and I don't want to have to change the web.xml file for each one.

Is there a way to get the URL info from the servletContext?

Thanks for the help.

- Ben
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You could put the URL in a properties file, or in a database.
I know with Tomcat, you can set context-init-params from either server.xml or from your context fragment files (depending on how you're deploying your app).
Both of those reside outside of the actual war file or webapps directory structure. There is also JNDI.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Just an idee !
Can you add a filter. this filter will extract the needed information (server name , context ) only once !this information can then be stored somwhere static variable , application context...... Then when your deamon servlet is running it can get back the information!

Ok this is not perfect you need that someone came on your site before having the information. But i hope it is the case
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ben,
I think Ben's question had more to do with making the variable configurable from outside the webapp.
-Ben.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get website URL in a servlet
 
Similar Threads
Doubt on chapter-6 Q-7 HFSJ
requestdispatcher.forward() Doubt?
ServletConfig & Context ?
servlet throws IllegalAccessError when trying to access another class
accessing web.xml from java bean