Finding your application URL only with ServletContext

I am writing a Java web application using Spring MVC. I have a background process that goes through a database and finds notifications that should be emailed to my users. These email messages must include hyperlinks in the app. This seems like a pretty common web application pattern, but I'm having problems.

How to get the full URL of my application with server name and context? I do not have access to any of the methods in the HttpServletRequest, because I run it as a background process, and not in response to a web request. The best I can do is access the ServletContext.

Currently, I put the base URL in the configuration file and read it at startup, but this application will be licensed and deployed on the client application servers, and if possible, I would like them to not have to configure it manually.

+45
java java-ee spring-mvc
Mar 24 '09 at 0:29
source share
5 answers

Dynamically compiling a URL at runtime is not recommended, especially based on ServletRequest. This is primarily due to the fact that you do not know the URL that users will use to access the application - the application server can be located behind the web server, firewall or load balancer. To prevent it, network topologies cannot be predicted.

Your current methodology for retrieving a URL from a properties file is good enough to resolve the issue. Perhaps you should take a look at providing an administrative console for managing the URL that appears in mail messages, especially if there is an administrator console in place or there are related parameters that should be included in one.

Edit: My last point repeats what Tony was talking about.

+29
Mar 24 '09 at 1:51
source share

Why is there simply no installation web page that appears when you first start the application if the configuration file does not exist (for example, in the WEB-INF folder. Using ServletContext you can call getRealPath and get the real path File and see if it exists (). If so, redirect to the start page of the application, if it is not, open the administration page).

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

+7
Mar 24 '09 at 1:04
source share

Servlet 2.5 added getContextPath () to ServletContext . Updating EE dependency versions should definitely fix your problem.

More details here:

  • Retrieving Servlet Context Path from Spring Web Application
  • Is there a built-in Spring environment variable for the web context root?
  • How to get ContextPath in init () method of Servlet 2.4
+1
Oct 27 '15 at 8:38
source share

I think you can use something like:

 String host = InetAddress.getLocalHost().getCanonicalHostName(); String appUrl = String.format("http://%s:%s%s", host, port, contextPath); 

with port and contextPath from ServletContext.

+1
Jul 05 '16 at 12:57
source share

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

0
Oct 27 '09 at 8:24
source share



All Articles