Java urlencoder. encode and htmlencoder

Source: Internet
Author: User

// JSP functions similar to htmlencode functions in ASP
Public static string htmlencode (string text ){
If (text = NULL)
Return "";
Stringbuffer Results = NULL;
Char [] orig = NULL;
Int beg = 0, Len = text. Length ();
For (INT I = 0; I <Len; ++ I ){
Char c = text. charat (I );
Switch (c ){
Case 0:
Case '&':
Case '<':
Case '> ':
Case '"':
If (Results = NULL ){
Orig = text. tochararray ();
Results = new stringbuffer (LEN + 10 );
}
If (I> beg)
Results. append (orig, beg, I-Beg );
Beg = I + 1;
Switch (c ){
Default: // case 0:
Continue;
Case '&':
Results. append ("&");
Break;
Case '<':
Results. append ("<");
Break;
Case '> ':
Results. append ("> ");
Break;
Case '"':
Results. append ("/"");
Break;
}
Break;
}
}
If (Results = NULL)
Return text;
Results. append (orig, beg, len-Beg );
Return results. tostring ();
}

// JSP functions similar to urlencode functions in ASP
Public static string urlencode (string text ){
Stringbuffer strurl = new stringbuffer ();
For (INT I = 0; I <text. Length (); ++ I ){
Switch (text. charat (I )){
Case '':
Strurl. append ("% 20 ");
Break;
Case '+ ':
Strurl. append ("% 2B ");
Break;
Case '/'':
Strurl. append ("% 27 ");
Break;
Case '/':
Strurl. append ("% 2f ");
Break;
Case '.':
Strurl. append ("% 2e ");
Break;
Case '<':
Strurl. append ("% 3C ");
Break;
Case '> ':
Strurl. append ("% 3e ");
Break;
Case '#':
Strurl. append ("% 23 ");
Break;
Case '% ':
Strurl. append ("% 25 ");
Break;
Case '&':
Strurl. append ("% 26 ");
Break;
Case '{':
Strurl. append ("% 7b ");
Break;
Case '}':
Strurl. append ("% 7D ");
Break;
Case '//':
Strurl. append ("% 5c ");
Break;
Case '^ ':
Strurl. append ("% 5E ");
Break;
Case '~ ':
Strurl. append ("% 73 ");
Break;
Case '[':
Strurl. append ("% 5B ");
Break;
Case ']':
Strurl. append ("% 5d ");
Break;
Default:
Strurl. append (text. charat (I ));
Break;
}
}
Return strurl. tostring ();
}

 

 

 

 

Use existing functions:
Java.net. urlencoder. encode ()
Java.net. urldecoder. Decode ()

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.