1, the first encounter with the process of adding filters, is learning Struct1, Chinese garbled
A few key words to note
2, what is called the package
the so-called package is the meaning of packaging, that is, the following programs are in this package, so at first you see the SRC drop-down menu below are some of their own built to separate the role of the structure
3, built up, you can build sub-files under the package, can be class files, can also be a variety of jsp,servlet files.
4, any servlet files or display interface files to be configured in the Web. xml file to be loaded, and is automatically loaded
5, write the code for the reminder place, you can use his suggestions, such as writing filer when the book did not give Import.filter guide package, resulting in the program can not be carried out, then found that there is no such header files, so to properly add these files.
Below is a list of filer Chinese garbled steps
1. Build a Package
2, in the package under the building of a class file, is the book of that class ( note that the book does not add header files )
3, Web. XML in the middle plus a few lines of configuration code can be ( Note is the middle of the configuration file, not directly in the </web-app> add)
Class Characterencodingfilter:
1 PackageCom.helloweenvsfei.filter;2 3 Importjava.io.IOException;4 ImportJavax.servlet.Filter;5 ImportJavax.servlet.FilterChain;6 ImportJavax.servlet.FilterConfig;7 Importjavax.servlet.ServletException;8 Importjavax.servlet.ServletRequest;9 ImportJavax.servlet.ServletResponse;Ten One Public classCharacterencodingfilterImplementsFilter { A - PrivateString characterencoding; - Private Booleanenabled; the - - Public voidInit (filterconfig config)throwsservletexception { - +characterencoding = Config.getinitparameter ("characterencoding"); - +Enabled = "true". Equalsignorecase (Characterencoding.trim ()) A|| "1". Equalsignorecase (Characterencoding.trim ()); at } - - - Public voidDoFilter (servletrequest request, servletresponse response, -Filterchain chain)throwsIOException, servletexception { - in if(enabled | | characterencoding! =NULL) { - request.setcharacterencoding (characterencoding); to response.setcharacterencoding (characterencoding); + } - the Chain.dofilter (request, response); * } $ Panax Notoginseng Public voiddestroy () { -Characterencoding =NULL; the } +}
Web. XML Add in
1<filter>2<filter-name>characterEncodingFilter</filter-name>3<filter-class>4 Com.helloweenvsfei.filter.CharacterEncodingFilter5</filter-class>6<init-param>7<param-name>characterEncoding</param-name>8<param-value>UTF-8</param-value>9</init-param>Ten<init-param> One<param-name>enable</param-name> A<param-value>true</param-value> -</init-param> -</filter> the -<filter-mapping> -<filter-name>characterEncodingFilter</filter-name> -<url-pattern>/*</url-pattern> + </filter-mapping>
Learning Notes _ The first Strut program _ The Chinese garbled, filter solution and process Summary