Filter过滤器,统一网站字符编码

首先建立字符编码过滤器的类文件EncodingFilter.java


package cn.itcast.web.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		response.setContentType("text/html;charset=utf-8");// 设置response字符编码方式为utf-8
		request.setCharacterEncoding("utf-8");// 设置response字符编码方式为utf-8
		chain.doFilter(request, response);// 放行过滤内容

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}

在web.xml中配置EncoidngFilter过滤器

<?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">
	<error-page>
		<error-code>500</error-code>
		<location>/sys_500.jsp</location>
	</error-page>

	<filter><!--配置字符编码方式过滤器-->
		<filter-name>EncodingFilter</filter-name>
		<filter-class>cn.itcast.web.filter.EncodingFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern><!--过滤器作用范围-->
	</filter-mapping>

	<filter>
		<filter-name>MappingFilter</filter-name><!--配置错误信息过滤-->
		<filter-class>cn.itcast.web.filter.MappingFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>MappingFilter</filter-name>
		<url-pattern>/*</url-pattern><!--过滤器作用范围-->
		<dispatcher>ERROR</dispatcher>

	</filter-mapping>
	<servlet>
		<servlet-name>FromServlet</servlet-name>
		<servlet-class>cn.itcast.web.servlet.FromServlet</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>ToServlet</servlet-name>
		<servlet-class>cn.itcast.web.servlet.ToServlet</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>GETServlet</servlet-name>
		<servlet-class>cn.itcast.web.servlet.GETServlet</servlet-class>
	</servlet>



	<servlet-mapping>
		<servlet-name>FromServlet</servlet-name>
		<url-pattern>/FromServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>ToServlet</servlet-name>
		<url-pattern>/ToServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>GETServlet</servlet-name>
		<url-pattern>/GETServlet</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

注:web.xml配置中,各个配置信息先后顺序如下(myecplice中,鼠标放在<web-app上,会自动弹出下面内容)

Content Model : (((description*, display-name*, icon*)) | distributable | context-param | filter | filter-
 mapping | listener | servlet | servlet-mapping | session-config | mime-mapping | welcome-file-list | 
 error-page | jsp-config | security-constraint | login-config | security-role | ((env-entry*, ejb-ref*, ejb-
 local-ref*, ((service-ref*)), resource-ref*, resource-env-ref*, message-destination-ref*, persistence-
 context-ref*, persistence-unit-ref*, post-construct*, pre-destroy*)) | message-destination | locale-
 encoding-mapping-list)*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值