Java 仅使用 ServletContext 查找应用程序的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/675730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding your application's URL with only a ServletContext
提问by Josh Hinman
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.
我正在使用 Spring MVC 编写 Java Web 应用程序。我有一个后台进程,它遍历数据库并查找必须通过电子邮件发送给我的用户的通知。这些电子邮件消息需要包含指向应用程序的超链接。对于网络应用程序来说,这似乎是一种相当常见的模式,但我遇到了麻烦。
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.
如何使用服务器名称和上下文路径导出应用程序的完全限定 URL?我无权访问 HttpServletRequest 中的任何方法,因为我将此作为后台进程运行,而不是响应 Web 请求。我能做的最好的事情就是访问 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.
目前,我将基本 URL 放入配置文件并在启动时读取它,但此应用程序将获得许可并部署到客户的应用程序服务器,如果可能,我希望他们不必手动配置它。
采纳答案by Vineet Reynolds
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.
不建议在运行时动态准备 URL,尤其是基于 ServletRequest。这主要是因为您不知道用户将用于访问应用程序的 URL - 应用程序服务器可能位于 Web 服务器、防火墙或负载平衡器之后。简而言之,人们无法预测网络拓扑。
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.
您当前从属性文件中获取 URL 的技术足以解决上述问题。也许您应该考虑提供一个管理控制台来管理出现在邮件中的 URL,特别是如果有一个管理控制台,或者如果有相关的选项应该进入一个。
Edit: My last point echoes what Tony has spoken of.
编辑:我的最后一点与托尼所说的相呼应。
回答by Tony BenBrahim
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).
如果配置文件不存在(例如在 WEB-INF 文件夹中。使用 ServletContext,您可以调用 getRealPath 并获取文件的真实路径和查看它是否存在()。如果存在,则重定向到应用程序起始页面,如果不存在,则打开管理页面)。
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.
您最好使用 ServletContext 从 web.xml 读取一些设置,并获取上下文路径,只有 HttpRequest 可以为您提供 FQ URL。
回答by gavenkoa
Servlet 2.5 added getContextPath()to ServletContext
. Updating EE dependency versions definitely should resolve your issue.
Servlet 2.5 将getContextPath()添加到ServletContext
. 更新 EE 依赖版本绝对可以解决您的问题。
More info here:
更多信息在这里:
回答by O.Wozniak
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.
来自 ServletContext 的端口和 contextPath。