request.獲取路徑的方法


假定你的web application 名稱為news,你在瀏覽器中輸入請求路徑:
 
http://localhost:8080/news/main/list.jsp
 
則執行下面向行代碼后打印出如下結果:
 
1、 System.out.println(request.getContextPath());
 
打印結果:/news
 
 
2、System.out.println(request.getServletPath());
 
打印結果:/main/list.jsp
 
 
3、 System.out.println(request.getRequestURI());
 
打印結果:/news/main/list.jsp
 
4、System.out.println(request.getRequestURL());
 
打印結果:http://localhost:8080/news/main/list.jsp
 
 
5、 System.out.println(request.getRealPath("/"));
 
打印結果:F:\Tomcat 6.0\webapps\news\test
 
 
6   像移動網關一樣,iisforward這個ISAPI過濾器也會對request對象進行再包裝,附加一些WLS要用的頭信息。這種情況下,直接用request.getRemoteAddr()是無法取到真正的客戶IP的。
   1. private String getIpAddr() {   
   2.      String ipAddress = null;   
   3.      //ipAddress = this.getRequest().getRemoteAddr();   
   4.      ipAddress = this.getRequest().getHeader("x-forwarded-for");   
   5.      if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 
   6.       ipAddress = this.getRequest().getHeader("Proxy-Client-IP");   
   7.      }   
   8.      if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 
   9.          ipAddress = this.getRequest().getHeader("WL-Proxy-Client-IP");   
  10.      }   
  11.      if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 
  12.       ipAddress = this.getRequest().getRemoteAddr();   
  13.       if(ipAddress.equals("127.0.0.1")){   
  14.        //根據網卡取本機配置的IP   
  15.        InetAddress inet=null;   
  16.     try {   
  17.      inet = InetAddress.getLocalHost();   
  18.     } catch (UnknownHostException e) {   
  19.      e.printStackTrace();   
  20.     }   
  21.     ipAddress= inet.getHostAddress();   
  22.       }   
  23.             
  24.      }   
  25.   
  26.      //對於通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照','分割   
  27.      if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15   
  28.          if(ipAddress.indexOf(",")>0){   
  29.              ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));   
  30.          }   
  31.      }   
  32.      return ipAddress;    
  33.   }   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM