Some useful common JAVA methods (1)

Source: Internet
Author: User
<%!
Stc_rsyb_name stcrsyname1 [];
Stc_syb_inf stcsybinf1 [];
%>

<% /*
String P = (string) Session. getvalue ("access ");
If (P = NULL |! P. Equals ("1 ")){
Response. sendredirect ("error.htm ");
Return;
}*/
%>
<%!
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, "& nbsp;", "& nbsp ;");
Txt = replace (txt, "", "& nbsp ;");
Return txt;
}

String unHtmlEncode (String txt ){
Txt = replace (txt ,"&","&");
Txt = replace (txt ,""",""");
Txt = replace (txt, "<", "<");
Txt = replace (txt, ">", "> ");
Txt = replace (txt, "& nbsp ;","");
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 (replace (MSG,", "),", ") + "");");
Out. println (Focus );
Out. println ("</SCRIPT> ");
} 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 ("</script> ");
} 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 ("& nbsp ;");
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 ("& nbsp; | ");
}
} Else {
For (INT I = 0; I <B-leng; I ++)
T = T + "& nbsp ;";
For (int I = 0; I <s. length (); I ++ ){
If (s. substring (I, I + 1). equals (""))
Out. print ("& nbsp ;");
If (! S. substring (I, I + 1). equals (""))
Out. print (htmlEncode (s. substring (I, I + 1 )));
}
Out. print (htmlEncode (t + "| "));
}
} Catch (Exception e) {System. out. println ("----" + e );}
}

File: // determine whether it is a number
Boolean isNumeric (String number)
{
Try
{
Integer. parseInt (number );
Return true;
}
Catch (NumberFormatException sqo)
{
Return false;
}
}
File: // determine whether it is a date
String mk_date (String c_date, int c_mode)
{
String wk_buf = "";
String ToDay = "";
Try
{
ToDay = Integer. toString (java. util. Calendar. getInstance (). get (java. util. Calendar. YEAR ));
Switch (c_mode)
{
Case 0:
{
Wk_buf = c_date.substring (2, 4) + "/" + c_date.substring (4, 6 );
If (c_date.length () = 8)
{
Wk_buf = wk_buf + "/" + c_date.substring (6, 8 );
}
Break;
}
Case 1:
{
For (int I = 0; I <c_date.length (); I ++)
{
If (c_date.substring (I, I + 1) = "")
{
Wk_buf = wk_buf + "0 ";
}
Else
{
Wk_buf = wk_buf + c_date.substring (I, I + 1 );
}
}
C_date = wk_buf;
Wk_buf = "";
If (c_date.substring (0, 1 )! = "0 ")
{
Wk_buf = "19 ";
}
Else
{
Wk_buf = "20 ";
}

Wk_buf = wk_buf + c_date.substring (0, 2) + c_date.substring (3, 5 );

If (c_date.length () = 8)
{
Wk_buf = wk_buf + c_date.substring (6, 8 );
}
Break;
}
Case 2:
{
Wk_buf = c_date.substring (0, 4) + c_date.substring (5, 7) + c_date.substring (8, 10 );
Break;
}
Case 3:
{
For (INT I = 0; I <c_date.length (); I ++)
{
If (c_date.substring (I, I + 1) = "")
{
Wk_buf = wk_buf + "0 ";
}
Else
{
Wk_buf = wk_buf + c_date.substring (I, I + 1 );
}
}
C_date = wk_buf;
Wk_buf = "";
If (integer. parseint (c_date.substring (0, 2)> integer. parseint (today. substring (2, 4 )))
{
Wk_buf = integer. tostring (integer. parseint (today. substring (0, 2)-1 );
}
Else
{
Wk_buf = today. substring (0, 2 );
}
Wk_buf = wk_buf + c_date.substring (0, 2) + c_date.substring (3, 5 );
If (c_date.length () = 8)
{
Wk_buf = wk_buf + c_date.substring (6, 8 );
}
Break;
}
Case 4:
{
Wk_buf = c_date.substring (0, 4) + "/" + c_date.substring (4, 6) + "/" + c_date.substring (6, 8 );
Break;
}
Case 5:
{
Wk_buf = c_date.substring (0, 4) + c_date.substring (5, 7 );
Break;
}
}
}
Catch (exception E)
{
System. Out. println ("wrong:" + E );
}
Return wk_buf;
}

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.