request.getParameter()获取乱码问题

一、假如用户是form表单方式提交的话

在使用rquest.getParameter("") 假如获取到的是乱码的话,就需要设置jsp页面和获取是使用同种编码方式

URLDecoder.decode(request.getParameter("参数名"),"UTF-8");

假如是上面获取形式的话  那么需要将页面的编码方式也设置为utf-8格式

二、假如用户是直接url访问时(location.href=‘’),request.getParameter("")获取的是乱码则:

String  title = new String(request.getParameter("title").getBytes("ISO-8859-1"),"utf-8");

因为直接http请求方式的话,使用的编码是iso-8859-1格式的,所以应该先以这种方式获取流然后转为utf-8格式,这样的话就直接可以显示正确的汉字了

猜你喜欢

转载自liuzhiqiang19890403.iteye.com/blog/2173676