自定义抽象的 HttpFilter类, 实现自 Filter 接口
package com.lanqiao.javaweb; 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; // 自定义的 HttpFilter, 实现自 Filter 接口 public abstract class HttpFilter implements Filter { // 用于保存 FilterConfig 对象. private FilterConfig filterConfig; // 不建议子类直接覆盖. 若直接覆盖, 将可能会导致 filterConfig 成员变量初始化失败 @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; init(); } //供子类继承的初始化方法. 可以通过 getFilterConfig() 获取 FilterConfig 对象. protected void init() {} // 直接返回 init(ServletConfig) 的 FilterConfig 对象 public FilterConfig getFilterConfig() { return filterConfig; } /** * 原生的 doFilter 方法, 在方法内部把 ServletRequest 和 ServletResponse * 转为了 HttpServletRequest 和 HttpServletResponse, 并调用了 * doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) * * 若编写 Filter 的过滤方法不建议直接继承该方法. 而建议继承 * doFilter(HttpServletRequest request, HttpServletResponse response, * FilterChain filterChain) 方法 */ @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; doFilter(request, response, chain); } //抽象方法, 为 Http 请求定制. 必须实现的方法. public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException; //空的 destroy 方法。 @Override public void destroy() {} }
建立一个Filter类,其继承于父类HttpFilter
package com.atguigu.javaweb.encoding; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.atguigu.javaweb.HttpFilter; public class EncodingFilter extends HttpFilter{ //配置参数 private String encoding; @Override
//获取web.jsp文件里配置和映射里的encoding属性 protected void init() { encoding = getFilterConfig().getServletContext().getInitParameter("encoding"); } @Override public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { //System.out.println(encoding); request.setCharacterEncoding(encoding); filterChain.doFilter(request, response); } }
WEB-INF下的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">
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.lanqiao.javatest.encoding.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/encoding/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>
</web-app>
在WebContent下的encoding文件里的login.jsp和index.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="login.jsp" method="post"> username:<input type="text" name="username"/><br><br> <input type="submit" name="tt" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> hello:${param.username } </body> </html>