Filter, Air Filter
Filter: 1. Category
Create a class that implements the javax. servlet. Filter Interface
Write the filtering logic in the doFilter () method
2. Configure
Configure the <filter> and <filter-mapping> elements in web. xml
Example 1:
Check whether the page is logged on. If you are not logged on, you will be redirected to the logon page.
1. Create a class to implement the javax. servlet. Filter Interface
2. Write the filtering logic in the doFilter () method
Package com. itnba. maya. filter; import java. io. IOException; import java. util. *; import javax. servlet. *; import javax. servlet. http. *; public class StateFilter implements Filter {private ArrayList <String> list = new ArrayList <String> (); // create a set, put the page without authentication @ Override public void destroy () {// method stub automatically generated by TODO} @ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// Conversion Type: HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; HttpSession session = req. getSession (); // obtain the request interface path String a = req. getRequestURI (); // long path String B = req. getContextPath (); // short path String c =. substring (B. length (); // capture the path if (list. contains (c) = false) {// if (session. getAttribute ("user") = null) {// determines whether the session has a value. If no value exists, go to the login page res. sendRedirect ("cs1.jsp");} else {chain. doFilter (req, res) ;}} else {chain. doFilter (req, res) ;}@override public void init (FilterConfig arg0) throws ServletException {String val = arg0.getInitParameter ("allowpage"); // read the web. value of init-param in xml configuration // split into array String [] arr = val. split (","); // put all arrays in the set list. addAll (Arrays. asList (arr ));}}
3. Configure the <filter> and <filter-mapping> elements in web. xml.
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version = "3.1"> <filter-name> statefilter </filter-name> <! -- Name it, which is the same as the following --> <filter-class> com. itnba. maya. filter. StateFilter </filter-class> <! -- The package name and Class Name of the filter class --!> <Init-param> <param-name> allowpage </param-name> <! -- Call the init (FilterConfig arg0) method in the filter class to obtain the following value --!> <Param-value>/cs1.jsp, /Cs1 </param-value> </init-param> </filter> <filter-mapping> <filter-name> statefilter </filter-name> <! -- Name it by yourself, which is the same as above --> <url-pattern>/* </url-pattern> <! -- The page to be filtered/* indicates all --> </filter-mapping> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>
In this way, the logon page is displayed.
Example 2:
Filter sensitive words and replace them ***
1. Create a class to implement the javax. servlet. Filter Interface
2. Write the filtering logic in the doFilter () method. You need to define a request using the internal class.
Package com. itnba. maya. filter; import java. io. IOException; import java. util. arrayList; import java. util. arrays; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletRequestWrapper; import javax. servlet. http. httpServletResponse; public class Filter implements javax. servlet. filter {private ArrayList <String> list = new ArrayList <> (); @ Override public void destroy () {// method stub automatically generated by TODO} @ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request; httpServletResponse res = (HttpServletResponse) response; HttpServletRequest mreq = new MyRequest (req); // open the next chain and do not use the original request; chain. doFilter (mreq, res) ;}@ Override public void init (FilterConfig arg0) throws ServletException {// put sensitive words in the Set String val = arg0.getInitParameter ("minganci "); string [] ss = val. split (","); list. addAll (Arrays. asList (ss);} // internal class, define the request class MyRequest extends HttpServletRequestWrapper {private HttpServletRequest request; public MyRequest (HttpServletRequest request) {super (request); this. request = request;} @ Override // rewrite getParameter public String getParameter (String name) {// obtain submitted content String txt = this. request. getParameter ("txt"); // modify the sensitive word for (String s: list) in the set {txt = txt. replaceAll (s, "***") ;}return txt ;}}}
3. Configure the <filter> and <filter-mapping> elements in web. xml.
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version = "3.1"> <filter-name> Filter </filter-name> <filter-class> com. itnba. maya. filter. filter </filter-class> <! -- The package name and Class Name of the filter class --!> <Init-param> <param-name> minganci </param-name> <! -- Call the init (FilterConfig arg0) method in the filter class to obtain the following value --!> <Param-value> sb, cnm, tmd </param-value> </init-param> </filter> <filter-mapping> <filter-name> Filter </filter-name> <url-pattern>/ * </url-pattern> <! -- The page to be filtered/* indicates all --> </filter-mapping> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>
Jsp page
<% @ 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">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
All sensitive words become ***