日期:2014-05-20 浏览次数:20946 次
<?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"> <!-- 字符过滤器--> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.pro.filter.EncodingFilter</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>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>*.act</url-pattern> </filter-mapping> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
package com.pro.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 EncodingFilter implements Filter { private FilterConfig filterConfig = null; public void init(FilterConfig fc) throws ServletException { this.filterConfig = fc; } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { String encoding = filterConfig.getInitParameter("encoding"); req.setCharacterEncoding(encoding); res.setCharacterEncoding(encoding); res.setContentType("text/html;charset=" + encoding); System.err.println("encoding"+encoding); System.err.println(req.getCharacterEncoding()); System.err.println(res.getCharacterEncoding()); System.err.println(res.getContentType()); chain.doFilter(req, res); } public void destroy() { this.filterConfig = null; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <form action="test.jsp" method="get"> <input type="text" name="message" /> <input type="submit"> </form>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%=request.getParameter("message") %>