Filter转译编码问题之谜
//这个是过滤器的处理类
package filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 filterconfig implements Filter{
protected String encoding=null;
protected FilterConfig config;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String encode=getEncoding();
if(encode!=null){
try {
request.setCharacterEncoding(encode);
} catch (UnsupportedEncodingException e) {
System.out.println("request.setCharacterEncoding(encode);出错了!");
e.printStackTrace();
}
System.out.println("------encode!=null-------"+encode);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterconfig) throws ServletException {
// TODO Auto-generated method stub
this.config=filterconfig;
this.encoding=filterconfig.getInitParameter("Encoding");
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
//登陆表单
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Insert title here</title>
</head>
<body>
<form action="success.jsp">
用户名<input type="text" name="name" ><br>
年 龄<input type="text" name="age" ><br>
<input type="submit" name="OK" VALUE="提交">
</form>
</body>
</html>
//处理成功跳转页
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Insert title here</title>
</head>
<body>
你提交的内容是:
姓名:<%=request.getParameter("name") %><br>
年龄:<%=request.getParameter("age") %>
</body>
</html>
//web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>test</display-name>
<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>
<filter>
<filter-name>filter</filter-name>
<filter-class>filter.filterconfig</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
以上一切做完之后运行...得到的中文字符却没有正常显示.....