How to get only part of URL from HttpServletRequest?

前端 未结 5 1952
花落未央
花落未央 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:32

    In my understanding, you need the domain part and Context path only. Based on this understanding, You can use this method to get the required string.

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

提交回复
热议问题