Java和JS转码
怕忘记。
转为ASCII形式:
String str="你是";try {System.out.println(URLEncoder.encode(str, "UTF-8")); //结果为%E4%BD%A0%E6%98%AFSystem.out.println(URLDecoder.decode("%E4%BD%A0%E6%98%AF","UTF-8")); //结果为你是 这个里面的字符为URLEncoder.encode(str, "UTF-8")产生System.out.println(URLDecoder.decode("%E4%BD%A0%E6%98%AF","UTF-8")); //结果为你是 这个里面的字符为javascript中encodeURI('你是')产生} catch (UnsupportedEncodingException e) {e.printStackTrace();}?HTML中的Js代码:
<script type="text/javascript"> function test_encodeURI(){alert(encodeURI('你是'));// 到jsp页面中解码的话用URLDecoder.decode即可,具体参看类URLEncodeTest.javadocument.write(encodeURI('你是')+"<br>");alert(decodeURI('%E4%BD%A0%E6%98%AF'));// encodeURIComponentdocument.write(encodeURI('www.cup.edu.cn')+"<br>");document.write(encodeURIComponent('http://www.cup.edu.cn')+"<br>"); //http%3A%2F%2Fwww.cup.edu.cndocument.write(decodeURI('www.cup.edu.cn')+"<br>");document.write(decodeURIComponent('http%3A%2F%2Fwww.cup.edu.cn')+"<br>"); //http://www.cup.edu.cn }</script>?