JSP页面乱码问题及解决方法:
1、页面中文显示为无规则乱码。在JSP页面中指定编码的方式。
<%@ page contentType="text/html"; charset=gb2312"%>
这样对于不同的应用服务器和JDK,都不会出现乱码问题。
2、中文显示为多个问号。这是因为浏览器默认使用UTF-8编码方式来发送页面请求。可以通过一个字符编码的函数来解决,把请求通过iso-8859-1来编码。如例:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%!String trans(String chi)
{
String result=null;
byte temp[];
try{
temp=chi.getBytes("iso-8859-1");
result=new String(temp);
}catch(UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
%>
<%
out.println(trans(request.getParameter("name")));
%>
3、 比2更简单的方法,在页面中加入:
request.setCharacterEncoding("gb2312");
向数据库存取数据出现乱码,解决方法如下:
1 首先要弄一个过滤器来对页面中的提交的数据进行过滤,也就是转换为UTF-8的编码
修改web.xml配置文件,加入如下代码
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
当然,filter.SetCharacterEncodingFilter类应当拷贝到相应目录下。(此类内容见附录)
2 修改其中的连接数据库部份。(以mysql为例)
原:
jdbc:mysql://localhost:3306/addressbook
现:
jdbc:mysql://localhost:3306/addressbook?useUnicode=true&characterEncoding=GBK
使用编码为GBK的方式连接数据库,也可以使用GB2312或是UTF-8,但UTF-8也会出现乱码,不知是何原因。这3种方式大家可以逐个测试。
现在只能使用GBK或是GB2312而且jdbc也只能使用3.0.10,我试过最新的3.0.18也是乱码。
3 现在数据库中和页面中应该都显示正常了,如果页面还是乱码,可试一下如下方法:
out.println("<td>"+ new String(abook.getName().getBytes("ISO-8859-1"), "GB2312") + "</td>");
out.println("<td>" + new String(abook.getPhone().getBytes("ISO-8859-1"), "GB2312") + "</td>");
out.println("<td>"+ new String(abook.getAddress().getBytes("ISO-8859-1"), "GB2312") +"</td>");
附:
================================================================================================================
package filter;
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 {
// Conditionally select and set the character encoding to be used
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);
}
}