JBoss应用程序完整路径

发布于 2024-08-22 17:11:47 字数 170 浏览 3 评论 0原文

有什么方法可以从 java 代码获取应用程序的 url 地址,我的意思是完整的地址,而不仅仅是 getContextPath() 的值。类似于 http://localhost:8080/etc

Is there any way I can get url address of an application from java code, I mean complete address not only value from getContextPath(). Something like http://localhost:8080/etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘴硬脾气大 2024-08-29 17:11:47

尝试使用 getRequestUrl()< /a>.

我希望它能帮助你

Try with getRequestUrl().

I hope it helps you

So要识趣 2024-08-29 17:11:47

在 servlet 或 JSP 中,您可以调用 javax.servlet.http.HttpUtils.getRequestURL(request) ,

到 servlet 的整个 URL

它返回一个 StringBuffer,其中包含从 javadoc

使用 HttpServletRequest 对象中的信息重建客户端用于发出请求的 URL。返回的 URL 包含协议、服务器名称、端口号和服务器路径,但不包含查询字符串参数。

如果您只想要上下文路径,则必须删除 servlet 路径

In a servlet or JSP, you can call javax.servlet.http.HttpUtils.getRequestURL(request)

It returns a StringBuffer containing the entire URL up to the servlet

From the javadoc

Reconstructs the URL the client used to make the request, using information in the HttpServletRequest object. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

If you only want up to the context path, you'll have to remove your servlet path

吝吻 2024-08-29 17:11:47

没有方法可以返回包含查询参数的整个 URL。您需要使用类似以下内容的内容:

req.getRequestURL()+"?"+req.getQueryString();

或者,如果您没有查询参数,则可以使用 getRequestURL

这很令人困惑,但这里有一个图形可以帮助您解决问题:

该图形已被删除,因为它在其他几个答案中使用了,管理员认为同一个图形不能回答多个问题。对不起。您只需猜测图形是什么。我希望我能分享它。我的博客上有一个副本,但我也无法在此处链接它,因为管理员认为这是“自我推销”并且不希望这样。所以你自己去找吧。

There is no method that returns the entire URL including the query parameters. You need to use something like:

req.getRequestURL()+"?"+req.getQueryString();

Or if you don't have query parameters you can use getRequestURL

It is pretty confusing, but here is a graphic that helps sort it out:

The graphic has been removed because it was used in several other answers, and the administrators think that the same graphic can not answer multiple questions. Sorry. You will just have to guess what the graphic is. I wish I could share it. I have a copy on my blog, but I can't link that here either because the administrators think it is "self-promotion" and don't want that. So look for it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文