Javaweb Project Custom Filter (character set filter) __javaweb

Source: Internet
Author: User
a simple character set filter

This content is designed to record the process of extending the filter in the Javaweb project and does not completely solve the garbled problem in the project 1, custom a filter class

Package com.jiuqi.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; /** 1. Title:characterfilter<br> 2. 
 Description: Character Set filter 3. 4. @author Qiuchangjin 5. @version April 13, 2018 */public class Characterfilter implements Filter {private String character;//Character Set encoding type/*
        * * Initialization of filter/@Override public void init (Filterconfig filterconfig) throws Servletexception {
    Reads the initialization parameters of the filter configuration in the web.xml character = Filterconfig.getinitparameter ("character"); @Override public void Dofilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filt Erchain) throws IOException, servletexception {//Read initialization parameters character Servletrequest.setcharact
        Erencoding (character); servletresponse.seTcharacterencoding (character); Servletresponse.setcontenttype ("text/html;
        Charset=utf-8 "); Filterchain.dofilter (ServletRequest, servletresponse); Release, go to next filter}/** * Before the filter is destroyed, the recovery of some resources/@Override public void Destroy () {}}
2, add Filter in the Web.xml in the Web-inf directory, filter loading is in order
<?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_2_5.xsd "id=" webapp_id "version=" 2.5 "> &LT;DISPLAY-NAME&GT;MYTEST&LT;/DISPL Ay-name> <!--define a filter--> <filter> <!--filter name--> <filter-name>c Haracterfilter</filter-name> <!--filter implementation class--> &LT;FILTER-CLASS&GT;COM.JIUQI.FILTER.CHARACTE
            Rfilter</filter-class> <!--initialization parameters--> <init-param> <!--parameter names--> <param-name>character</param-name> <!--parameter values, encoded as Utf-8--> &LT;PARAM-VALUE&G T;utf-8</param-value> </init-param> </filter> <!--define the URL address to intercept--> <f
Ilter-mapping> <!--filter's name-->        <filter-name>CharacterFilter</filter-name> <!--to intercept the URL, here is all intercept--> <url-patt Ern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index. Jsp</welcome-file> </welcome-file-list> </web-app>
3. Project Code Structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.