急,有关jsp表单中的乱码问题
来源:百度知道 编辑:UC知道 时间:2024/05/13 01:36:23
1) 在每个JSP页面开头写<%@ page contentType="text/html;charset=GBK"%>
2) 用以下方法进行转码。
static public String iso2gb(String str) {
if (str != null) {
byte[] tmpbyte=null;
try {
tmpbyte=str.getBytes("ISO8859_1");}
catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());}
try {
str=new String(tmpbyte,"GBK"); }
catch(UnsupportedEncodingException e) {
System.out.println(e.getMessage());}
}
return str;
}
一个好的方法是在web.xml部署一个中文乱码自动转换过滤器
web.xml
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>com.common.jsp.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value