在做java开发时,我们会经常碰到中文显示出错的问题,解决方法一般是设置encoding,过滤器,(unicode码)资源文件等等,好用的还是过滤器。
过滤器的实现代码在网络上可以找到不少,但自己备份几个还是不错的。
- 过滤器1
/*
*
* TALENTBASE 3.0 Copyright 2005 Neusoft, Co.ltd .
* All rights reserved.
*
* Package: com.neusoft.talentbase.framework.filter
* FileName: CharsetEncodingFilter.java
* by zhangzhh
*
* For
*
* version: 1.0
*
* Created on 2005-3-22
* Last Modified:
*
* History:
*
*
*/
package com.cvicse.inforstudio.weblogs.util;
/**
*
* @author zhangzhh
* @version 1.0
* </pre>
* Created on :2005-3-22 17:02:47
* LastModified:
* History:
* </pre>
*/
import javax.servlet.*;
import java.io.IOException;
public class CharsetEncodingFilter implements Filter {
private String defaultEncode = "UTF-8";
public void init(FilterConfig config) throws ServletException {
if(config.getInitParameter("charset")!=null){
defaultEncode=config.getInitParameter("charset");
}
}
public void destroy() {
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
ServletRequest srequest=request;
srequest.setCharacterEncoding(defaultEncode);
chain.doFilter(srequest,response);
}
}
- 过滤器2
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class SetCharacterEncodingFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
- 过滤器3
package test;
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;
import javax.servlet.UnavailableException;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
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);
}
}
等一下,这还没完,还要在web.xml里面配置一下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>myweb</display-name> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>test.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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
。。。。。。。。。。。。。。。。。。。。。。。。。。。
类似的东西不少,前两个用过,不错;后面的从视觉上看挺漂亮,推荐!
=================================================================================
应用中总是发现各种各样的乱码问题,简单分类一下:
1.Java文件和JSP文件编码时产生乱码
即:在Java和jsp文件中包含中文字符,而Java或jsp在编译的时候是采用字节流的(其他编码),产生编码不一致,出现乱码。解决方法:
java文件:文件属性中的Encoding编码设置为GBK、GB2312或UTF-8
JSP文件:在文件头设置<%@ page contentType="text/html;charset=GBK"%>
2.和其他应用交互时产生乱码,通常会在Request中的URI产生乱码。
原因是很多程式是基于字节流的,而Java程序是基于字符流。解决办法:
Tomcat:修改conf/server.xml,找到
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" enableLookups="false" maxHttpHeaderSize="8192" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="8080" redirectPort="8443" URIEncoding="GBK" />
在最后面加上URIEncoding="GBK".
另一种就是在HttpServelet中doGet方法中设置contentype=GBK,这样在处理请求前编码格式就被处理了。例:
public class MyHttpServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=GBK");
// Do Other Things
}
}