Java servlet 的根 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1629102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:00:22  来源:igfitidea点击:

Root URl of the servlet

javajakarta-ee

提问by DonX

I want to get the root url of my web application from one of the servlet.

我想从 servlet 之一获取我的 Web 应用程序的根 url。

If I deploy my application in "www.mydomain.com" I want to get the root url like "http://www.mydomain.com".

如果我在“www.mydomain.com”中部署我的应用程序,我想获得像“ http://www.mydomain.com”这样的根网址。

Same thing if I deploy it in local tomcat server with 8080 port it should give http://localhost:8080/myapp

同样的事情,如果我将它部署在具有 8080 端口的本地 tomcat 服务器中,它应该给出 http://localhost:8080/myapp

Can anyone tell me how to get the root URL of my web application from servlet?

谁能告诉我如何从 servlet 获取我的 Web 应用程序的根 URL?

public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String rootURL="";
        //Code to get the URL where this servlet is deployed

    }
}

采纳答案by ChssPly76

You do realize that the URL client sees (and/or types into his browser) and the URL served by the container your servlet is deployed on can be very different?

您确实意识到 URL 客户端看到(和/或在他的浏览器中键入)和部署 servlet 的容器所提供的 URL 可能有很大不同?

In order to get the latter, though, you have a few methods available on HttpServletRequest:

但是,为了获得后者,您可以在HttpServletRequest上使用一些方法:

  • You can either call getScheme(), getServerName(), getServerPort()and getContextPath()and combine them using appropriate separators
  • OR you can call getRequestURL()and remove getServletPath()and getPathInfo()from it.
  • 您可以打电话getScheme()getServerName()getServerPort()getContextPath()使用适当的分隔符将它们组合起来
  • 或者您可以调用并从中getRequestURL()删除getServletPath()和删除getPathInfo()

回答by janko

Generally, you can't obtain the URL; but, there are workarounds for specific cases. See Finding your application's URL with only a ServletContext

一般是获取不到网址的;但是,有针对特定情况的解决方法。请参阅仅使用 ServletContext 查找应用程序的 URL

回答by jagan

  1. Write a scriptlet in the welcome file to capture the root path. I assume index.jsp is the default file. So put the following code in that

    <% RootContextUtil rootCtx = RootContextUtil.getInstance(); if( rootCtx.getRootURL()==null ){ String url = request.getRequestURL().toString(); String uri = request.getRequestURI(); String root = url.substring( 0, url.indexOf(uri) ); rootCtx.setRootURL( root ); } %>

  2. Use this variable wherever needed within the application directly by calling the value as

  1. 在欢迎文件中编写一个脚本来捕获根路径。我假设 index.jsp 是默认文件。所以把下面的代码放在里面

    <% RootContextUtil rootCtx = RootContextUtil.getInstance(); if( rootCtx.getRootURL()==null ){ String url = request.getRequestURL().toString(); String uri = request.getRequestURI(); String root = url.substring( 0, url.indexOf(uri) ); rootCtx.setRootURL( root ); } %>

  2. 通过将值调用为

String rootUrl = RootContextUtil.getInstance().getRootURL();

String rootUrl = RootContextUtil.getInstance().getRootURL();

NOTE: No need to worry about protocols/ports/etc.. Hope this helps every one

注意:无需担心协议/端口/等。希望这对每个人都有帮助

回答by Benny Neugebauer

This function helps you to get your base URL from a HttpServletRequest:

此函数可帮助您从以下位置获取基本 URL HttpServletRequest

  public static String getBaseUrl(HttpServletRequest request) {
    String scheme = request.getScheme() + "://";
    String serverName = request.getServerName();
    String serverPort = (request.getServerPort() == 80) ? "" : ":" + request.getServerPort();
    String contextPath = request.getContextPath();
    return scheme + serverName + serverPort + contextPath;
  }

回答by Mohsen Zamani

public static String getBaseUrl(HttpServletRequest request) {
    String scheme = request.getScheme();
    String host = request.getServerName();
    int port = request.getServerPort();
    String contextPath = request.getContextPath();

    String baseUrl = scheme + "://" + host + ((("http".equals(scheme) && port == 80) || ("https".equals(scheme) && port == 443)) ? "" : ":" + port) + contextPath;
    return baseUrl;
}