JS Java escape and Unescape Functions

Source: Internet
Author: User

JavaScript code involves three functions: Escape, encodeuri, and encodeuricomponent. The corresponding three decoding functions are: Unescape, decodeuri, and decodeuricomponent.

1. encodeuricomponent is required when passing parameters so that the combined URL will not be truncated by special characters such.

Example: <script language = "JavaScript"> document. Write ('<a href = "http://passport.baidu.com /? Logout & Aid = 7 & U = '+ encodeuricomponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </SCRIPT>

2. encodeuri can be used for URL redirection.

Example: location. href = encodeuri ("http://cang.baidu.com/do/s? WORD = Baidu & Ct = 21 ");

3. You can use escape when JS uses data.

[Edit huoho. com]

For example, search for the history record in the bucket.

4. When encoding Unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeuri, and encodeuricomponent have the same encoding result.

The most commonly used format is encodeuricomponent, which converts special characters such as Chinese and Korean into UTF-8 URL encoding, therefore, if you want to use encodeuricomponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, A-Z, A-Z

Encodeuri is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, A-Z, A-Z

Encodeuricomponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, A-Z, A-Z

 

 

 

Java.net. urldecoder/java.net. urlencoder
What corresponds to the Javascript encodeuri/decodeuri and encodeuricomponent/decodeuricomponent?

 
The following Java corresponds to the Javascript escape/Unescape.

 

Java-based escape and Unescape Functions

class  EscapeUnescape
{
 public static String  escape (String src)
 {
  int i;
  char j;
  StringBuffer tmp = new StringBuffer();
  tmp.ensureCapacity(src.length()*6);

  for (i=0;i<src.length() ;i++ )
  {

   j = src.charAt(i);

   if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
    tmp.append(j);
   else
    if (j<256)
    {
    tmp.append( "%" );
    if (j<16)
     tmp.append( "0" );
    tmp.append( Integer.toString(j,16) );
    }
    else
    {
    tmp.append( "%u" );
    tmp.append( Integer.toString(j,16) );
    }
  }
  return tmp.toString();
 }

 public static String  unescape (String src)
 {
  StringBuffer tmp = new StringBuffer();
  tmp.ensureCapacity(src.length());
  int  lastPos=0,pos=0;
  char ch;
  while (lastPos<src.length())
  {
   pos = src.indexOf("%",lastPos);
   if (pos == lastPos)
    {
    if (src.charAt(pos+1)=='u')
     {
     ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16);
     tmp.append(ch);
     lastPos = pos+6;
     }
    else
     {
     ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16);
     tmp.append(ch);
     lastPos = pos+3;
     }
    }
   else
    {
    if (pos == -1)
     {
     tmp.append(src.substring(lastPos));
     lastPos=src.length();
     }
    else
     {
     tmp.append(src.substring(lastPos,pos));
     lastPos=pos;
     }
    }
  }
  return tmp.toString();
 }

 public static void main(String[] args)
 {
  String tmp="~!@#$%^&*()_+|\\=-,./?><;'][{}\"";
  System.out.println("testing escape : "+tmp);
  tmp =escape(tmp);
  System.out.println(tmp);
  System.out.println("testing unescape :"+tmp);
  System.out.println(unescape(tmp));
 }
}

 

 

Related Article

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.