字符编码的过滤器

通过配置参数encoding指明使用何种字符编码,以处理Html Form请求参数的中文问题

package com.Greatest.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;

//自定义HttpFilter方法 ,实现Filter接口
public abstract class MyFilter implements Filter {

	// 用于保存FilterConfig方法
	private FilterConfig fconfig;

	// 不建议子类直接覆盖,若直接覆盖,可能会导致filterconfig成员变量初始化失败
	@Override
	public void init(FilterConfig Config) throws ServletException {
		this.fconfig = Config;
		init();
	}

	// 供子類繼承的初始化方法 可以通过getFilterConfig()方法获取FilterConfig对象
	protected void init() {
		// TODO Auto-generated method stub

	}// 直接返回init(ServletConfig)的FilterConfig对象

	public FilterConfig getFconfig() {
		return fconfig;
	}

	@Override
	public void destroy() {
	}

	// 原声的doFilter方法 在方法内部把ServletRequest ,ServletResponse转为了
	// HttpServletRequest 和 HttpSrevletResponse 并调用了
	// doFilter(ServletRequest Req, ServletResponse Resp, FilterChain Chain)
	// 若编写Filter的过滤方法 不建议直接继承该方法 而建议 继承
	// public abstract void doFilter(HttpServletRequest
	// request,HttpServletResponse response,
	// FilterChain Chain) 方法
	@Override
	public void doFilter(ServletRequest Req, ServletResponse Resp, FilterChain Chain)
			throws IOException, ServletException {
		HttpServletRequest requesr = (HttpServletRequest) Req;
		HttpServletResponse response = (HttpServletResponse) Resp;

		doFilter(requesr, response, Chain);

	}// 抽像方法 为Http请求定制 必须实现的方法

	public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain Chain)
			throws IOException, ServletException;

}
package com.Greatest.Filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Enoding extends MyFilter {
	private String enoding;

	@Override
	protected void init() {
		enoding = getFconfig().getServletContext().getInitParameter("enoding");
	}

	@Override
	public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain Chain)
			throws IOException, ServletException {
		request.setCharacterEncoding(enoding);
		Chain.doFilter(request, response);
	}

}
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <context-param>
     <param-name>enoding</param-name>
     <param-value>UTF-8</param-value>
  </context-param>
  
  
  <filter>
     <filter-name>Enoding</filter-name>
     <filter-class>com.Greatest.Filter.Enoding</filter-class>
  </filter>
  <filter-mapping>
     <filter-name>Enoding</filter-name>
     <url-pattern>/enoding/*</url-pattern>
     <!-- 以后可能会写 (*.do)、(/enoding)... -->
<web-app>
<%@ 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="e.jsp" method="post">
      username:<input type="text" name="username"/>
      password:<input type="password" name="password"/>
      <input type="submit" value="Submit"/>
    </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>

猜你喜欢

转载自blog.csdn.net/qq_42676998/article/details/82813696