encodeURIComponent与encodeURI区别

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
encodeURIComponent(URIstring)
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

 

encodeURI() 函数可把字符串作为 URI 进行编码。
encodeURI(URIstring)
该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#

 

encodeURIComponent() 函数 与 encodeURI() 函数的区别之处:

前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串).

 

要连续两次调用 encodeURI(String) 方法
是因为 Java 中的 request.getParameter(String) 方法会进行一次 URI 的解码过程,调用时内置的解码过程会导致乱码出现。
而 URI 编码两次后, request.getParameter(String) 函数得到的是原信息 URI 编码一次的内容。
接着用 java.net.URLDecoder.decode(String str,String codename) 方法,将已经编码的 URI 转换成原文。

 

comment=encodeURIComponent("http://cang.baidu.com/bruce42");
var pdata="method=updateComment&date="+dateStr+"&comment="+comment;	
pdata=encodeURI(encodeURI(pdata));

 

//BookingDaysCommentForm.java
public void setComment(String comment) {
	try {
		this.comment = java.net.URLDecoder.decode(comment,"utf-8");
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		this.comment = comment;
		LOGGER.error("Decode comment error:"+e.getMessage());
	}catch (RuntimeException re){
		this.comment = comment;
		LOGGER.error("Decode comment RuntimeException:"+re.getMessage());
	} 
}	

 

 

//BookingDaysCommentOpenAction.java
String formatComment = formBean.getComment();
formatComment = java.net.URLDecoder.decode(formatComment,"utf-8");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值