java认证:用servlet过滤器解决JSP中文乱码问题Java认证考试
文章作者 100test 发表时间 2009:05:02 16:38:22
来源 100Test.Com百考试题网
---servlet 文件
package com.encoding.
import java.io.IOException.
import javax.servlet.FilterChain.
import javax.servlet.FilterConfig.
import javax.servlet.ServletException.
import javax.servlet.ServletRequest.
import javax.servlet.ServletResponse.
public class Encoding implements javax.servlet.Filter {
public void destroy() {
/ / TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request .setCharacterEncoding("GBK").
chain.doFilter(request, response).
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
---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">.
<.!-- 中文乱码过滤处理 -->.
<.filter>.
<.filter-name>.encoding<./filter-name>.
<.filter-class>.
com.encoding..Encoding
<./filter-class>.
<./filter>.
<.filter-mapping>.
<.filter-name>.encoding<./filter-name>.
<.url-pattern>./*<./url-pattern>.
<./filter-mapping>.
<./web-app>.
更多优质资料尽在百考试题论坛 百考试题在线题库