URLDecoder.decode(String str,String charSet)的大致实现原理

URL编码 百分号编码 URLDecoder.decode的大致实现原理

Java代码
  1. package com.dt.test;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.net.URLDecoder;  
  5. import java.net.URLEncoder;  
  6.   
  7. /*** 
  8.  * URL编码又叫百分号编码 URLDecoder.decode的大致实现原理 
  9.  */  
  10. class testURLEncode {  
  11.     public void testURLEncode() {  
  12.         String testString;  
  13.         try {  
  14.             testString = URLEncoder.encode("中文""utf-8");  
  15.             System.out.println("testString : " + testString);  
  16.             testString = testString.replace("%""");  
  17.             int length = testString.length() / 2;  
  18.             byte[] data = new byte[length];  
  19.             for (int i = 0; i < length; i++) {  
  20.                 data[i] = (byte) Integer.parseInt(testString.substring(2 * i,  
  21.                         2 * i + 2), 16);  
  22.             }  
  23.             String result = new String(data, "utf-8");  
  24.             System.out.println("result : " + result);  
  25.         } catch (UnsupportedEncodingException e1) {  
  26.             // TODO Auto-generated catch block  
  27.             e1.printStackTrace();  
  28.         }  
  29.     }  
  30.     public void testURLEncodeGBK() {  
  31.         String testString;  
  32.         String testString0;  
  33.           
  34.         try {  
  35.             testString = URLEncoder.encode("中文""utf-8");  
  36.             testString0 = testString;  
  37.             System.out.println("testString : " + testString);  
  38.               
  39.             testString =  URLDecoder.decode(testString0,"GBK");  
  40.             System.out.println("decode : " + testString);  
  41.               
  42.             testString =  URLDecoder.decode(testString0,"utf-8");  
  43.             System.out.println("decode : " + testString);  
  44.             testString =  URLEncoder.encode("中文""GBK");  
  45.             System.out.println("decode : " + testString);  
  46.             testString =  URLDecoder.decode(testString,"GBK");  
  47.             System.out.println("decode : " + testString);  
  48.         } catch (UnsupportedEncodingException e1) {  
  49.             e1.printStackTrace();  
  50.         }  
  51.     }  
  52.   
  53.     public static void main(String[] args) {  
  54.   
  55.         new testURLEncode().testURLEncode();  
  56.         new testURLEncode().testURLEncodeGBK();  
  57.     }  
  58. url后参数的转码与解码

    import java.net.URLDecoder;
    import java.net.URLEncoder;

      String strTest = "?=abc?中%1&2<3,4>";
      strTest = URLEncoder.encode(strTest, "UTF-8");
      System.out.println(strTest);
      strTest = URLDecoder.decode(strTest,"UTF-8");
      System.out.println(strTest);





 

 

执行结果:

%3F%3Dabc%3F%E4%B8%AD%251%262%3C3%2C4%3E
?=abc?中%1&2<3,4>


package com.cn.servlet04; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet("/Cookie03") public class ServleCookie03 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置响应消息体的数据格式 resp.setContentType("text/html;char=utf-8"); //获取cookie Cookie[] cookies = req.getCookies(); //遍历数据 if(cookies != null && cookies.length>0){ for(Cookie c:cookies){ String name = c.getName(); //判断name是否是登录时间的name System.out.println("name"+name); if("time".equals(name)){ String value = c.getValue(); //解码 value = URLDecoder.decode(value,"utf-8"); //有我要的cookie 不是第一次访问 resp.getWriter().write("<h1>欢迎回来</h1>"); } } } if(cookies == null || cookies.length==0){ //第一次访问 Date date = new Date();//获取系统时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String str_date = sdf.format(date); Cookie c = new Cookie("time",str_date); // c.setMaxAge(0); resp.addCookie(c); resp.getWriter().write("欢迎你首次访问"); } } } 为什么网页无法正常显示,给出修改过后的代码
07-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值