Some tips for Java development

Source: Internet
Author: User

First, Java gets the parameters passed in the URL address

1/**2* Gets the map collection of parameter names and parameters values in the URL3*@paramUrl4*@return5*/6Private Map<string, string>Geturlpramnameandvalue (String URL) {7 String regex= "(\\?| &+) (. +?) = ([^&]*) ";//Regular expressions that match parameter names and parameter values8 Pattern p =Pattern.compile (REGEX);9 Matcher m =P.matcher (URL);10//Linkedhashmap is an ordered map collection that iterates through the output in the order in which they are added11 map<string, string> parammap = new linkedhashmap<string, String> () while (M.find ()) {13 String paramname = M.group (2); //14 String Paramval=m.group (3); // get parameter value 15  Parammap.put (paramname, paramval); 16 }17 return Parammap;                 

Second, get the URL address of the request

1/**2* Get the requested URL address3*@return 4 * /5 public  String Getrequesturl () { 6 httpservletrequest request = Servletactio Ncontext.getrequest (); 7 // Request.getrequesturl () gets to the parameter part of the URL without the parameter url,request.getquerystring (), to get the full URL with the parameter, You need to put these two pieces together. 8 String url = request.getrequesturl () + "?" +request.getquerystring (); 9 return URL;  Ten one }                 

Third, obtain the requested IP address

1     /**     * Gets the requested IP address @return* /public String getrequestipaddress () {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 is converted to a date in the specified format3*@paramStr4*@returnDate5*/6PublicStaticBooleanIsvaliddate (String str, string formater) {7Boolean convertsuccess=True;8 SimpleDateFormat format =NewSimpleDateFormat (Formater);9Try{Ten Format.setlenient (False); 11  Format.parse (str);  catch (ParseException e) { 13 //14 // if the throw Java.text.ParseException or nullpointerexception, it means that the format is not 15 Convertsuccess=false;  17 return convertsuccess; 18}             

Some tips in 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.