`

java URLEncoder URLDecoder 和js encodeURI

阅读更多

 

 

个人理解中文传送的时后需要转码:

js代码:
要进行两次转码才不会出现乱码(默认为UTF-8)
encodeURI(encodeURI(http://localhost/qq/index.jsp?title=专业));


java接收参数代码:
	String title = request.getParameter("title");
	title = URLDecoder.decode(title, "UTF-8");


java 编码:
  http://localhost/qq/index.jsp?title=URLEncoder.encode(URLEncoder.encode(title,"UTF-8"));


本地开发时候没有乱码,可是到linux中出现乱码, 下面是解决的方法, 但没有找到乱码的原因,以下作为使用记录。 

	1传递中文
		parent.location.href=queryURL+"?title="+encodeURI(encodeURI(title ) );
	
	2 有存在iframe,则decode参数,后传递数据。
		<%
			String title = request.getParameter("title");
			if(title !=null ){
				title = java.net.URLDecoder.decode(title, "UTF-8");
   //如果这里还是乱码则用下面的方法转码
// title=new String(title.getBytes("GBK"),"ISO8859-1");
			}
		%>
		<iframe height="530" width="100%" src="newsList${ suffix}?c2str=${c2str}&tabid=1&title=<%=(title!=null?title:"")%>&area=${ !empty param.area? param.area:'' }" id="newsList" scrolling="no" frameborder="0"></iframe>

	3第三个页面接收数据再次转码
	title = URLDecoder.decode(URLDecoder.decode(title, "UTF-8") ,"UTF-8") ;
 



作为个人记录用。

 

 

 在编码与解码的时候要注意页面的编码也会影响到encode的编码结果。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics