jsp乱码问题

 

jsp乱码发生的原因:

因为tomcat 对post 方式处理参数采用的是iso-8895-1。

图:

 

 

 jsp乱码发生的情况:

1,表单提交的时候,request.getParameter("");返回的为乱码

2,http://localhost/a.jsp?pram=中文,用request.getParameter("");返回的为乱码

前一种方式为post 方式,request.setCharacterEncoding("UTF-8")对此种方式生效

后一种方式为get方式,request.setCharacterEncoding("UTF-8")对此种方式无效

处理:

1,通过强转String   str=new   String(request.getParameter().getBytes(“iso-8859-1”),”GB2312”);  
2,修改tomcat 的server.xml来修改tomcat 处理字符的方式

3,js 中
var url = ".../servlet/Check_Unit_Name?unName=" + str.value; 
url = encodeURI(url); 
页面2:
<% String unName = new String(request.getParameter("unName").getBytes("ISO-8859-1"),"UTF-8"); %>

 or <a href="csdn2.jsp?name=<%=URLEncoder.encode("中国", "utf-8")%>">查看</a>

 

实例:

关于页面<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 <frame src='onlineAnaRightAction.do?method=list&parm=在线分析'...

页面的的编码也是utf-8,

修改tomcat 下的server.xml

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

加上红色的,在后台可以用request.getParameter("parm")得到中文

分析:

UTF-8 采用三个字节表示一个普通的汉字而 GBK 采用两个,两个 UTF-8 汉字是 6 个字节,
被强制转成 GBK 的话就会变成三个字符。

出现这个问题的原因在于,URI 参数是使用 UTF-8 编码的,而在 server.xml 中的 Connector
里的 URIEncoding 属性被改成“GBK”了。你把它改回来,改成 UTF-8 就行了,那样使用

request.getParameter("name"); 可以直接得到正确的值。

如果你不想改动 server.xml 那就得转码,因为 Tomcat 默认的 URI 传输采用 ISO-8859-1 的编码。
new String(request.getParameter("name").getBytes("iso8859-1"), "utf-8")

 

参考:1,http://wenku.baidu.com/view/b052061d650e52ea55189813.html

2,http://topic.csdn.net/u/20080917/15/7a6a59cb-9950-499e-ad5d-421cd377f81b.html

3,http://topic.csdn.net/u/20080910/16/269beb9e-9fce-44a6-bc1c-f0fcf56f708f.html?seed=1632860754

4,http://blog.sina.com.cn/s/blog_491df4cd01000afp.html

5,http://hi.baidu.com/atell/blog/item/79bc641bb0a2d64b42a9ad05.html

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值