1. 程式人生 > >HttpServletRequest獲取請求路徑

HttpServletRequest獲取請求路徑

eth ref col ren views align method tostring com

Java代碼 技術分享圖片
  1. HttpServletRequest獲取請求路徑
  2. 1//Returns the part of this request‘s URL from the protocol name up to the query string in the first line of the HTTP request
  3. // eg. /ser.do?method=add_pre&type=mad
  4. String url = request.getRequestURI();
  5. return
    /ssm/ser.do
  6. 2//The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters
  7. //eg. http://localhost:8080/ssm/ser.do?method=add_pre&type=mad
  8. StringBuffer url_buffer = request.getRequestURL();
  9. return
    http://localhost:8080/ssm/ser.do


HttpServletRequest 的這兩種方法都只能得到不包含參數的請求url,區別如下:

1 前者返回相對路徑,後者返回完整路徑

2 前者返回string ,後者返回stringbuffer

要想得到完整請求url可以通過如下方法,getQueryString()得到的是url後面的參數串,和前者相加就是帶參數的請求路徑了
Java代碼 技術分享圖片
  1. String queryString = request.getQueryString();
  2. String fullPath = url + queryString;
  3. // 或者是url_buffer.toString()+queryString;
  4. 即 /ssm/ser.do + method=add_pre&type=mad

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

HttpServletRequest獲取請求路徑