1、从页面到java:传递中文参数,有一下三种方法
不进行任何编码的url中的空格转换成 %20,
1)escape();参数为null
2)encodeURI();如果参数中有&,参数会被截断;encodeURI()主要用于整个URI(例如,http://www.jxbh.cn/illegal value.htm);不会对本身属于URI的特殊字符进行编码,例如冒号、正斜杠、问号和井字号。对应的解码方法decodeURI()。
3)encodeURIComponent();出现&时,不会被截断;而encode-URIComponent()主要用于对URI中的某一段(例如前面URI中的illegal value.htm)进行编码;而encodeURIComponent()则会对它发现的任何非标准字符进行编码。对应的解码方法decodeURIComponent()。
java中 request给参数赋值时,自动解码一次,但是往往解码不正确,需要
js中:categoryText=encodeURIComponent(encodeURIComponent(categoryText));
java中:cateName=java.net.URLDecoder.decode(cateName,"UTF-8");
如下:
var uri="http://www.jxbh.cn/illegal value.htm#start";
alert(encodeURI (uri)): //”http: //www.jxbh.cn/illegal%20value .htm#s tart”
alert( encodaURIComponent (uri));//”http% 3A%2F%2Fwww.jxbh.cn%2 Fillegal%2 0value. htm%23 start”
保证页面和java的编码一致。
设置request.setCharacterEncoding("UTF-8");
2、从java到页面:传递中文参数,
保证java和页面的编码方式一致。
设置 response.setContentType("text/html;charset=utf-8");
页面<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>