【问题标题】:How to get only part of URL from HttpServletRequest?如何从 HttpServletRequest 仅获取部分 URL?
【发布时间】:2012-12-28 05:59:04
【问题描述】:

我需要从以下 URL 单独获取 (http://localhost:9090/dts)
那就是我需要删除 (documents/savedoc) (OR)
只需要得到-(http://localhost:9090/dts)

http://localhost:9090/dts/documents/savedoc  

是否有任何方法可以请求获得上述内容?

我尝试了以下并得到了结果。但仍在努力。

System.out.println("URL****************"+request.getRequestURL().toString());  
System.out.println("URI****************"+request.getRequestURI().toString());
System.out.println("ContextPath****************"+request.getContextPath().toString());

URL****************http://localhost:9090/dts/documents/savedoc  
URI****************/dts/documents/savedoc  
ContextPath****************/dts

谁能帮我解决这个问题?

【问题讨论】:

    标签: java string url servlets httprequest


    【解决方案1】:

    你说你想得到准确的:

    http://localhost:9090/dts
    

    在您的情况下,上述字符串包括:

    1. scheme: http
    2. 服务器host name本地主机
    3. 服务器port9090
    4. context path: dts

    (有关请求路径元素的更多信息可以在官方 Oracle Java EE 教程中找到:Getting Information from Requests
    ##第一个变种:###

    String scheme = request.getScheme();
    String serverName = request.getServerName();
    int serverPort = request.getServerPort();
    String contextPath = request.getContextPath();  // includes leading forward slash
    
    String resultPath = scheme + "://" + serverName + ":" + serverPort + contextPath;
    System.out.println("Result path: " + resultPath);
    

    ##第二个变种:##
    String scheme = request.getScheme();
    String host = request.getHeader("Host");        // includes server name and server port
    String contextPath = request.getContextPath();  // includes leading forward slash
    
    String resultPath = scheme + "://" + host + contextPath;
    System.out.println("Result path: " + resultPath);
    

    两种变体都能满足您的需求:http://localhost:9090/dts

    当然还有其他变体,就像其他人已经写过的那样......

    这只是在您询问如何获得http://localhost:9090/dts 的原始问题中,即您想要include 方案的路径。

    如果您仍然不需要方案,快速的方法是:

    String resultPath = request.getHeader("Host") + request.getContextPath();
    

    你会得到(在你的情况下):localhost:9090/dts

    【讨论】:

      【解决方案2】:

      AFAIK 对此没有提供 API 方法,需要自定义。

      String serverName = request.getServerName();
      int portNumber = request.getServerPort();
      String contextPath = request.getContextPath();
      

      //试试这个

      System.out.println(serverName + ":" +portNumber + contextPath );
      

      【讨论】:

        【解决方案3】:

        只需从 URL 中删除 URI,然后将上下文路径附加到它。无需摆弄松散的方案和端口,当您处理根本不需要出现在 URL 中的默认端口 80 时,这只会更加乏味。

        StringBuffer url = request.getRequestURL();
        String uri = request.getRequestURI();
        String ctx = request.getContextPath();
        String base = url.substring(0, url.length() - uri.length() + ctx.length());
        // ...
        

        另见:

        【讨论】:

        • 5 个类似的问题。终于有一个简洁的答案了。
        【解决方案4】:

        据我了解,您只需要域部分和上下文路径。基于这样的理解,你可以使用这个方法来获取需要的字符串。

        String domain = request.getRequestURL().toString();
        String cpath = request.getContextPath().toString();
        
        String tString = domain.subString(0, domain.indexOf(cpath));
        
        tString = tString + cpath;
        

        【讨论】:

          【解决方案5】:

          对于那些想要在他们的端点中获取以端点为目标的首页的URL。你可以使用这个:

          request.getHeader("referer")
          

          【讨论】:

            【解决方案6】:

            通常我有这样的方法:

            public String getAbsoluteContextPath() throws MalformedURLException {
                ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
                HttpServletRequest request = (HttpServletRequest) context.getRequest();
                URL url = new URL(request.getRequestURL().toString());
                return url.getProtocol() + "://" + url.getAuthority() + context.getRequestContextPath();
            }
            

            此方法将返回您想要的内容,仅当端口号存在于当前请求中时才会返回该端口号。在您的情况下,它将返回: http://localhost:9090/dts

            【讨论】:

              猜你喜欢
              • 2020-02-11
              • 2011-02-21
              • 2011-02-08
              • 1970-01-01
              • 2012-11-10
              • 2014-07-04
              • 1970-01-01
              • 1970-01-01
              • 2012-08-21
              相关资源
              最近更新 更多