仅使用 ServletContext 查找应用程序的 URL [英] Finding your application's URL with only a ServletContext

查看:25
本文介绍了仅使用 ServletContext 查找应用程序的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring MVC 编写 Java Web 应用程序.我有一个后台进程,它遍历数据库并查找必须通过电子邮件发送给我的用户的通知.这些电子邮件消息需要包含指向应用程序的超链接.对于网络应用来说,这似乎是一种相当常见的模式,但我遇到了麻烦.

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.

如何使用服务器名称和上下文路径获取应用程序的完全限定 URL?我无权访问 HttpServletRequest 中的任何方法,因为我将此作为后台进程运行,而不是响应 Web 请求.我能做的最好的事情就是访问 ServletContext.

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 放入配置文件并在启动时读取它,但此应用程序将获得许可并部署到客户的应用程序服务器,如果可能,我希望他们不必配置这是手动的.

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,尤其是基于 ServletRequest.这主要是因为您不知道用户将用于访问应用程序的 URL - 应用程序服务器可能位于 Web 服务器、防火墙或负载平衡器之后.简而言之,人们无法预测网络拓扑.

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 的技术足以解决上述问题.也许您应该考虑提供一个管理控制台来管理出现在邮件中的 URL,尤其是在有管理控制台的情况下,或者如果有相关的选项应该纳入其中.

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.

我的最后一点与托尼所说的相呼应.

My last point echoes what Tony has spoken of.

这篇关于仅使用 ServletContext 查找应用程序的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆