Java獲取URL地址中傳遞的參數


轉載:https://www.cnblogs.com/xdp-gacl/p/3490276.html

一、 Java獲取URL地址中傳遞的參數

   /**
 2      * 獲取URL中的參數名和參數值的Map集合
 3      * @param url
 4      * @return
 5      */
 6     private Map<String, String> getUrlPramNameAndValue(String url){
 7     String regEx="(\\?|&+)(.+?)=([^&]*)";//匹配參數名和參數值的正則表達式
 8         Pattern p = Pattern.compile(regEx);  
 9         Matcher m = p.matcher(url);
10      // LinkedHashMap是有序的Map集合,遍歷時會按照加入的順序遍歷輸出
11     Map<String, String> paramMap = new LinkedHashMap<String, String>();
12         while(m.find()){
13         String paramName = m.group(2);//獲取參數名
14         String paramVal=m.group(3);//獲取參數值
15             paramMap.put(paramName, paramVal);
16         }
17 return paramMap; 18 }

 

二、獲取請求的URL地址

 

復制代碼
1     /**
2      * 獲取請求的IP地址
3      * @return
4      */
5     public String getRequestIpAddress(){
6         return ServletActionContext.getRequest().getRemoteAddr();
7     }

 

三、獲取請求的IP地址

1 /**
2      * 獲取請求的IP地址
3      * @return
4      */
5     public String getRequestIpAddress(){
6         return ServletActionContext.getRequest().getRemoteAddr();
7     }

四:判斷字符串是否能夠轉換成指定格式的日期

 1 /**
 2     * 驗證字符串是否能夠轉換成指定格式的日期
 3     * @param str
 4     * @return date
 5     */
 6     public static boolean isValidDate(String str ,String formater) {
 7       boolean convertSuccess=true;
 8        SimpleDateFormat format = new SimpleDateFormat(formater);
 9        try {
10           format.setLenient(false);
11           format.parse(str);
12        } catch (ParseException e) {
13           // e.printStackTrace();
14           //如果throw java.text.ParseException或者NullPointerException,就說明格式不對
15            convertSuccess=false;
16        } 
17        return convertSuccess;
18     }

 

 

 


免責聲明!

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



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