How to get only part of URL from HttpServletRequest?

前端 未结 5 1943
花落未央
花落未央 2020-12-31 02:05

From the following URL I need to get (http://localhost:9090/dts) alone.
That is I need to remove (documents/savedoc) (OR)
need to get on

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 02:30

    Usually I have a method like this:

    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();
    }
    

    This method will return what you want, with the port number only if it exists in the current request. In your case it will return: http://localhost:9090/dts

提交回复
热议问题