JSP乱码总结和自定义Filter设置编码

1.自己写个Filter

CharsetEncodingFilter.java

package filter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 CharsetEncodingFilter implements Filter {
	
	private String encoding;
	
	public void destroy() {
		System.out.println("--------destroy---------");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		System.out.println("--------doFilter---------");
		arg0.setCharacterEncoding(encoding);
		arg2.doFilter(arg0,arg1);
	}

	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("--------init---------");
		this.encoding = arg0.getInitParameter("encoding");
		System.out.println(encoding);
	}
}


web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    	<filter>
		<filter-name>CharsetEncodingFilter</filter-name>
		<filter-class>filter.CharsetEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>CharsetEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  	<filter-mapping>
		<filter-name>CharsetEncodingFilter</filter-name>
		<url-pattern>/servlet/*</url-pattern>		
	</filter-mapping>
</web-app>


2.在页面

<%@ page contentType="text/html;charset=UTF-8" %>
<%request.setCharacterEncoding("UTF-8");%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%response.setCharacterEncoding("UTF-8");%>

 

3.如果出现类似类似这样的乱码:%E4%B8%8E%E6%A8%A1%E5%BC%8F%

<%URLDecoder.decode(str,"UTF-8");%>


4.自己手动编码转换

	String str = (String) request.getParameter("username");
	byte[] temp = str.getBytes("ISO-8859-1");
	str = new String(temp,"UTF-8");
	out.print(str);


5.修改tomcat默认编码

默认情况下,tomcat使用的是iso8859-1的编码编码方式
修改tomcat下的conf/server.xml文件
找到如下代码:   
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
这段代码规定了Tomcat监听HTTP请求的端口号等信息。
可以在这里添加一个属性:URIEncoding,将该属性值设置为UTF-8,即可让Tomcat(默认ISO-8859-1编码)以UTF-8的编码处理get请求。
更改后的代码如下所示:
<Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />此外
还可以增加属性useBodyEncodingForURI="true" 设置POST和GET使用相同编码
更改后的代码如下所示:
<Connector port="8080"  useBodyEncodingForURI="true"  URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />


6.注意response.setContentType("text/html;charset=utf-8");和PrintWriter out = response.getWriter();的位置关系,

切记要将PrintWriter out = response.getWriter();放在response.setContentType("text/html;charset=utf-8");的后面,否则设置的编码将无效


7.其他

http://fiona777.iteye.com/blog/385934

http://handonghandong.iteye.com/blog/758866

http://gaobaolu.blog.edu.cn/2012/722247.html

你可能感兴趣的:(tomcat,jsp,String,filter,byte,encoding)