JBoss应用程序完整路径
有什么方法可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 getRequestUrl()< /a>.
我希望它能帮助你
Try with getRequestUrl().
I hope it helps you
在 servlet 或 JSP 中,您可以调用 javax.servlet.http.HttpUtils.getRequestURL(request) ,
到 servlet 的整个 URL
它返回一个 StringBuffer,其中包含从 javadoc
如果您只想要上下文路径,则必须删除 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
If you only want up to the context path, you'll have to remove your servlet path
没有方法可以返回包含查询参数的整个 URL。您需要使用类似以下内容的内容:
或者,如果您没有查询参数,则可以使用
getRequestURL
这很令人困惑,但这里有一个图形可以帮助您解决问题:
该图形已被删除,因为它在其他几个答案中使用了,管理员认为同一个图形不能回答多个问题。对不起。您只需猜测图形是什么。我希望我能分享它。我的博客上有一个副本,但我也无法在此处链接它,因为管理员认为这是“自我推销”并且不希望这样。所以你自己去找吧。
There is no method that returns the entire URL including the query parameters. You need to use something like:
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.