简体   繁体   English

如何在不使用JSF的情况下使用Java CDI获取服务器的URL

[英]How to get the url of the server with java cdi without JSF

My app is an Java REST app, and i need to send e-mails. 我的应用程序是Java REST应用程序,我需要发送电子邮件。

The e-mail has the URL for the related action... 电子邮件中包含相关操作的网址 ...

How can i get the real server address???? 我如何获得真实的服务器地址?

I can get the Session instance, but it has only the contextPath... 我可以获取Session实例,但是它只有contextPath ...

http://xxxxxx/<contextPath>

I need the fully context... like this 我需要完整的上下文...像这样

<protocol>://<domain>/<contextPath>

If you use Java EE 7 / CDI 1.1+ you can inject ServletContext for the application and HttpServletRequest for the current request : 如果使用Java EE 7 / CDI 1.1+,则可以为应用程序注入ServletContext ,为当前请求注入HttpServletRequest

@Inject
ServletContext context;

@Inject
HttpServletRequest request;

You'll have what you need in these to build your URL. 您将在其中拥有构建URL所需的功能。

If you are on Java EE 6 / CDI 1.0 the easiest solution is to add Apache Deltaspike servlet module to your project, then you'll have the same feature with : 如果您使用的是Java EE 6 / CDI 1.0,最简单的解决方案是将Apache Deltaspike Servlet模块添加到您的项目中,那么您将拥有与以下功能相同的功能:

@Inject
@Deltaspike
ServletContext context;

@Inject
@Deltaspike
HttpServletRequest request;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM