jsp中文乱码问题解决
方法一、JSP页面显示乱码下面的显示页面(display.jsp)就出现乱码:<html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%out.print("JSP的中文处理");%></body></html>对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下:<%@ page contentType="text/html; charset=gb2312"%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%out.print("JSP的中文处理");%></body></html>
二、表单提交中文时出现乱码下面是一个提交页面(submit.jsp),代码如下:<html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><form name="form1" method="post" action="process.jsp"><div align="center"><input type="text" name="name"><input type="submit" name="Submit" value="Submit"></div></form></body></html>下面是处理页面(process.jsp)代码:<%@ page contentType="text/html; charset=gb2312"%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%=request.getParameter("name")%></body></html>如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF- 8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.setCharacterEncoding ("gb2312")对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下:<%@ page contentType="text/html; charset=gb2312"%><%request.setCharacterEncoding("gb2312");%><html><head><title>JSP的中文处理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%=request.getParameter("name")%></body></html>
三、数据库连接出现乱码只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上useUnicode=true&characterEncoding=GBK 就OK了。四、数据库的显示乱码在mysql4.1.0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决中文问题,对于text类型就要用一个编码转换类来处理,实现如下:public class Convert {/** 把ISO-8859-1码转换成GB2312*/public static String ISOtoGB(String iso){String gb;try{if(iso.equals("") || iso == null){return "";}else{iso = iso.trim();gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");return gb;}}catch(Exception e){System.err.print("编码转换错误:"+e.getMessage());return "";}}}把它编译成class,就可以调用Convert类的静态方法ISOtoGB()来转换编码。
总结:1. 在jsp中<%@ page contentType="text/html; charset=A" %>如果指定了,那么在改jsp中所有构造的String(不是引用),如果沒有指定编码,那么这些String的编码是A的。从request的得到的String如果沒有指定request的编码的话,他是iso-8859-1的从别的地方得到的String是使用原來初始的编码的,比如从数据库得到String,如果数据库的编码是B,那么该String的编码是B而不是A的,也不是系统默认的。此时,如果要输出的String的编码不是A,那么,很可能显示乱码的,所以首先要将String正確转化为编码A的String,然后输出。
2. 在jsp中<%@ page contentType="text/html; charset=A" %>沒有指定,那么相当于指定了<%@ page contentType="text/html; charset=ISO-8859-1" %>
3. Servelte中如果执行了像 response.setContentType
("text/html;charset=A");説明将response的字符输出流编码设置为A,所有要输出的String的编码要转化为A的,否則会得到乱码的。Servelet中从request得到的String的编码和jsp中一样的,但是在servlet java文件中构造的String是使用的系统默认的编码的。在servelt中从外部得到的String 是使用原来的编码的,比如从编码为B的数据库得到的数据是编码为B的,不是A,也不是系统默认的编码。 本文来源:IT传媒网原文链接:http://www.cniter.com/tech/jsp/10142_1.html
-------------------
补充:
2)表单提交乱码
表单提交时(post和Get方法),使用request.getParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。
(1)POST处理
对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式,过滤器的代码如下:
Java代码
package example.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
<STRONG><SPAN style="COLOR: #ff0000"> FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}</SPAN>
</STRONG>
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
package example.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
文中红色的代码即为处理乱码的代码。
web.xml文件加入过滤器
Xml代码
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
example.util.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
<!--gbk或者gb2312或者utf-8-->
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
example.util.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
<!--gbk或者gb2312或者utf-8-->
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>Xml代码
<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>/*</servlet-name>
</filter-mapping>
(2) Get方法的处理
tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。
打开<tomcat_home>\conf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:
Java代码
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" <SPAN style="COLOR: #ff0000">URIEncoding="GBK"</SPAN> />
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
* 注意修改后重新启动tomcat才能起作用。
分享到:
相关推荐
JSP中文问题解决办法,JSP页面传值,数据库等的中文乱码解决办法
JSP中文乱码问题解决总结,个人收集,一次放出,减少大家的查找时间。
jsp中文乱码问题解决方案
JSP中文乱码问题解决方法. 几种修改乱码的方法!
在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱码问题以及解决方法。
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着大家,现把JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考。
自从接触Java和JSP以来,就不断与Java的中文乱码问题打交道,现在终于得到了彻底的解决,现将我们的解决心得与大家共享。
本文档详细地介绍了如何解决MySql数据库中的中文乱码问题,此问题的解决将极大地帮助基于MySql的PHP编程、JSP编程、Servlet编程及C#或.Net编程!测试过的开发平台包括 Netbeans和Eclipse。测试过的服务器包括Apache、...
JSP中文乱码问题的最优解决方法
Tomcat环境下JSP中文乱码问题的解决
jsp SmartUpload 中文乱码问题解决.docx
JSP中文乱码解决集锦JSP中文乱码解决集锦JSP中文乱码解决集锦
最近实验室做了一个项目,使用jspsmartupload来实现的文件的上传下载,原来在windows平台运行的挺好,但是后来系统移植到linux平台上,结果在上传或下载的文件名中有中文时就会出现乱码。后来查了网上一些人的解决...
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考。 一、JSP页面显示乱码 二、表单提交中文时出现乱码 三、数据库连接时...
这是我收集的最全的解决JSP中文乱码的文章了 其中前面几个万能解决方法是我归纳出来的,不要怕分多,因为我太缺分了,绝对值10分
用eclipse集成开发环境, jsp 过滤器——解决中文乱码问题
jsp传值中文乱码问题解决方法示例介绍.docx
一些常见的JSP中文乱码问题解决思路,供大家参考。