A simple java character set filter implementation


Copy codeThe code is as follows:
package dw05prj.util.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

/*Page character set filter stream*/

Examples of configuration in web.xml


public class CharEncodingFilter implements Filter {

private String charSet ;

public void init(FilterConfig parm1) throws ServletException {
charSet = parm1.getInitParameter(“charset”) ;
if (charSet == null && charSet.length() < 1)
charSet = “UTF-8” ;
System. Out. Println (“charencodingfilter — character set:” + this. Getcharset());

public void doFilter(ServletRequest parm1, ServletResponse parm2,
FilterChain parm3) throws IOException, ServletException {

//Set character set
parm3.doFilter(parm1, parm2);
//Every request to the server in the page is filtered — such as images, JS, HTML / JSP, and so on
//System.out.println (“—CharEncodingFilter: ” + this.getCharSet()) ;

public void destroy() {
// TODO: Add your code here
this.setCharSet(null) ;

public void setCharSet(String charSet) {
this.charSet = charSet;

public String getCharSet() {
return (this.charSet);