views:

2347

answers:

4

I'm writing a Java web app using Spring MVC. I have a background process that goes through the database and finds notifications that must be e-mailed to my users. These e-mail messages need to include hyperlinks to the application. This seems like a fairly common pattern for a web app, but I'm having trouble.

How do I derive my application's fully qualified URL, with server name and context path? I don't have access to any of the methods in HttpServletRequest because I'm running this as a background process, not in response to a web request. The best I can do is get access to ServletContext.

Currently I'm putting the base URL into a configuration file and reading it at startup, but this application is going to be licensed and deployed to customers' application servers, and if possible I'd like them not to have to configure this manually.

+1  A: 

Why just not have a setup web page that comes up the first time the application is run if the configuration file does not exist (in WEB-INF folder for example. Using ServletContext, you can call getRealPath and get the real path of a File and see if it exists(). If it does, redirect to the app start page, if it does not, open the admin page).

The best you cam do with ServletContext is read some settings from web.xml, and get the context path, only the HttpRequest can give you the FQ URL.

Tony BenBrahim
+2  A: 

It is not recommended to dynamically prepare the URL at run time, especially based on ServletRequest. This is primarily because you have no idea of the URL that users would be using to access the application - the application server could be behind a web server, a firewall or a load balancer. To keep it short, one cannot predict network topologies.

Your current technique of fetching the URL from the property file is good enough to resolve the said issue. Maybe you should look at providing an administrative console to manage the URL appearing in mails, especially if there is an admin console in place, or if there are related options that should go into one.

Edit: My last point echoes what Tony has spoken of.

Vineet Reynolds
A: 

First, you have to understand that servlet container itself does not necessarily have a single unique URL. For example, it may run in a tomcat, be attached to apache over AJP13 and serve 1000 host names such as http://.example.com/

Once you understand this and are quite sure that your application will be accessible through a single host+port, you can use following code to find out your application's URL from REQUEST:

public static void setApplicationBase(HttpServletRequest req) {
    if (applicationBase == null) {
        applicationBase = req.getScheme() + "://" + req.getServerName() +
                getPort(req) + req.getContextPath();
    }
}

private static String getPort(HttpServletRequest req) {
    if ("http".equalsIgnoreCase(req.getScheme()) && req.getServerPort() != 80 ||
            "https".equalsIgnoreCase(req.getScheme()) && req.getServerPort() != 443 ) {
        return (":" + req.getServerPort());
    } else {
        return "";
    }
}

Add servlet filter to invoke this code on every request and you are done. The performance penalty will be minimal.

At least you can use this value to prefill a field in your config page.

Vilmantas Baranauskas
The question specifically requested an approach that did not use HttpServletRequest, just a ServletContext.
Brian
A: 

Have you checked with a debugger if your ServletContext is not an instance of HttpServletContext? If so, cast it, and get the information from there.

Thorbjørn Ravn Andersen