gpt4 book ai didi

jsp - 如何获取域名URL和应用程序名称?

转载 作者:行者123 更新时间:2023-12-03 04:54:08 24 4
gpt4 key购买 nike

这是场景。

我的 Java Web 应用程序具有以下路径

https://www.mywebsite.com:9443/MyWebApp

假设有一个 JSP 文件

https://www.mywebsite.com:9443/MyWebApp/protected/index.jsp

我需要检索

https://www.mywebsite.com:9443/MyWebApp 

在此 JSP 文件中。

当然,有一种相当懒惰和愚蠢的方法,只需获取 URL,然后重新跟踪路径即可。

但是有没有一种编程方法可以做到这一点?具体来说,我认为我可以获取域+端口,但是如何实际检索应用程序名称“MyWebApp”?

最佳答案

查看 HttpServletRequest 的文档.
为了在您的示例中构建 URL,您需要使用:

  • getScheme()
  • getServerName()
  • getServerPort()
  • getContextPath()

这是一个将返回您的示例的方法:

public static String getURLWithContextPath(HttpServletRequest request) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
}

关于jsp - 如何获取域名URL和应用程序名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2204870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com