日期:2014-05-19  浏览次数:20657 次

JSP中文乱码过滤器问题----急,在线等。
在网上看了好久,还是没解决了。高手们帮帮忙吧。
我贴代码。
web.xml配置如下。
XML code

<?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">
  <display-name></display-name>
  
  <!--中文乱码处理过滤-->
   <filter>
   <filter-name>EncodingFilter</filter-name>
   <filter-class>com.filter.Filter_charset</filter-class>
   <init-param>
   <param-name>encoding</param-name>
   <param-value>utf-8</param-value>
   </init-param>
   </filter>

   <filter-mapping> 
   <filter-name>EncodingFilter</filter-name> 
   <url-pattern>/*</url-pattern> 
   </filter-mapping> 
 
   <!--中文乱码处理过滤-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>








过滤器类如下:
Java code

package com.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 Filter_charset implements Filter{

    protected String encoding = null;          // 接收字符编码
    public void destroy() {
        // TODO Auto-generated method stub
        this.encoding = null;
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        if (this.encoding != null)
        {
            request.setCharacterEncoding(this.encoding);
            response.setCharacterEncoding(this.encoding);
            System.out.println(this.encoding);
        }
        chain.doFilter(request, response);
        
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        this.encoding = filterConfig.getInitParameter("encoding");
        
    }


}






提交页面如下:
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  <head>
  </head>
  <body>
    <form action="TEST/filter/1.jsp">
    输入<input type="text" name="name"><br>
    <input type="submit" value="提交">
    </form>
  </body>
</html>





接收页面如下:
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
  </head>
  <body>
   <%
       String name = request.getParameter("name");//得到提交表单的输入内容