Some tips for Java development

Source: Internet
Author: User

First, Java gets the parameters passed in the URL address

1     /** 2      * Gets a Map collection of parameter names and parameter values in the URL 3      * @param URL 4      * @return 5 */      6     private map<string, String> ; Geturlpramnameandvalue (string url) {7     string regex= "(\\?| &+) (. +?) = ([^&]*) ";//Regular expression matching parameter name and parameter value 8         Pattern p = pattern.compile (regEx);   9         Matcher m = p.matcher (URL), Linkedhashmap is an ordered Map collection that iterates through the output in the order in which it was added     map<string, string> param Map = new linkedhashmap<string, string> (), while         (M.find ()) {         String paramname = M.group (2);// Gets the parameter name of         String Paramval=m.group (3);//Gets the argument value             of Parammap.put (ParamName, paramval);         }17         return Parammap;18     }        

Second, get the URL address of the request

1     /** 2      * Gets the requested URL address 3      * @return 4 *      /5 public     String Getrequesturl () {6     HttpServletRequest Request = Servletactioncontext.getrequest (); 7//Request.getrequesturl () gets to the parameter part of the URL without a parameter url,request.getquerystring () to get the full URL with the parameter, you need to piece together the two parts 8     String URL = request.getrequesturl () + "?" +request.getquerystring (); 9     return url;10     }

Third, obtain the requested IP address

1     /**2      * Get the requested IP address 3      * @return4      */5 public     String getrequestipaddress () {6         return Servletactioncontext.getrequest (). GETREMOTEADDR (); 7     }

Four: Determines whether a string can be converted to a date in the specified format

1     /** 2     * Verify that the string can be converted to the specified format date 3     * @param str 4     * @return Date 5 */     6 public     Static Boolean isValid Date (String str, string formater) {7       Boolean convertsuccess=true; 8        simpledateformat format = new Simpledateform at (Formater); 9        try {           format.setlenient (false);           Format.parse (str);        catch (ParseException e) {           // E.printstacktrace ()           //If throw java.text.ParseException or NullPointerException, the format is not            Convertsuccess=false;16        } +        return convertsuccess;18     }

Some tips for Java development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.