Some useful common JAVA methods (1)

Source: Internet
Author: User

<%!


File: // get the machine name or IP address


String get_Myhostname (javax. servlet. http. HttpServletRequest request ){


String myhostname = null;


Try {


Myhostname = request. getRemoteHost ();


Myhostname = myhostname. toUpperCase ();


} Catch (Exception e ){}


Return myhostname;


}


File: // process an empty string


String dealNull (String str ){


String returnstr = null;


If (str = null) returnstr = "";


Else returnstr = str;


Return returnstr;


}


File: // process empty objects


Object dealNull (Object obj ){


Object returnstr = null;


If (obj = null) returnstr = (Object )("");


Else returnstr = obj;


Return returnstr;


}



Int dealEmpty (String s ){


S = dealNull (s );


If (s. equals ("") return 0;


Return Integer. parseInt (s );


}



String replace (String str, String substr, String restr ){


String [] tmp = split (str, substr );


String returnstr = null;


If (tmp. length! = 0 ){


Returnstr = tmp [0];


For (int I = 0; I <tmp. length-1; I ++)


Returnstr = dealNull (returnstr) + restr + tmp [I + 1];


}


Return dealNull (returnstr );


}



String htmlEncode (String txt ){


Txt = replace (txt ,"&","&");


Txt = replace (txt ,"&","&");


Txt = replace (txt ,""",""");


Txt = replace (txt ,""",""");


Txt = replace (txt, "<", "<");


Txt = replace (txt, "<", "<");


Txt = replace (txt, ">", "> ");


Txt = replace (txt, ">", "> ");


Txt = replace (txt ,"","");


Txt = replace (txt ,"","");


Return txt;


}


String unHtmlEncode (String txt ){


Txt = replace (txt ,"&","&");


Txt = replace (txt ,""",""");


Txt = replace (txt, "<", "<");


Txt = replace (txt, ">", "> ");


Txt = replace (txt ,"","");


Return txt;


}



File: // ALERT Function


Void sAlert (String title, String msg, String focus, JspWriter out ){


Try {


Out. println ("<" script language = javascript "> ");


Out. println ("alert (" "+ title +" "+"


"+" "+ Replace (msg,", "),", ") + "");");


Out. println (focus );


Out. println ("");


} Catch (Exception e) {System. out. println (e );}


}


File: // CONFIRM Function


Void sConfirm (String title, String msg, String location, JspWriter out ){


Try {


Out. println ("<" script language = javascript "> ");


Out. println ("if (confirm (" "+ title +" "+"


"+" "+ Replace (msg," ",") + "")){");


Out. println ("var d = (new Date (). toString ();");


Out. println (location + "+ d ;");


Out. println ("}");


Out. println ("parent.doc ument. form1.text1. select ();");


Out. println ("");


} Catch (Exception e ){}


}



File: // get the string length


Int getStringLength (String s ){


Int p = 0;


Try {


Int iLength = 0;


Int irealLength = 0;


Int j = 0;


P = s. getBytes ("Shift_JIS"). length;


} Catch (java. io. UnsupportedEncodingException e ){}


Return p;


}



Void formatString (String s, int B, JspWriter out ){


B --;


String t = "";


Try {


Int leng = getStringLength (s );


Int j = 0;


If (leng> B ){


For (int I = 1; I <= B; I ++ ){


T = s. substring (0, I );


If (t. getBytes ("Shift_JIS"). length <= B ){


J ++;


} Else break;


}


File: // j = j + 1;


File: // System. out. println ("B =" + B + "----" + "j =" + j );


For (int I = 0; I <j; I ++ ){


If (s. substring (I, I + 1). equals (""))


Out. print ("");


If (! S. substring (I, I + 1). equals (""))


Out. print (htmlEncode (s. substring (I, I + 1 )));


}


If (s. substring (0, j). getBytes ("Shift_JIS"). length = B ){


Out. print ("| ");


} Else {


Out. print ("| ");


}


} Else {


For (int I = 0; I <B-leng; I ++)


T = t + "";


For (int I = 0; I <s. length (); I ++ ){


If (s. substring (I, I + 1). equals (""))


Out. print ("");

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.