Generally simple and common use of filters, I think in addition to configure the character encoding filter is the login filter
The main process of logging in filters can be summed up in a sentence: first, when you log in the designated good sign in the session, the operation of the filter according to the sign in the session to determine whether the need to filter. (as if it were nonsense ...) )
Filter Code:
PackageCom.ahoo.filter;Importjavax.servlet.*;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;Importjava.io.IOException;/** * @authorXueyuan * @dater 2017/2/20.*/ Public classLoginfilterImplementsFilter {@Override Public voidInit (Filterconfig filterconfig)throwsservletexception {} @Override Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain Filterchain)throwsIOException, servletexception {httpservletrequest req=(httpservletrequest) request; HttpServletResponse resp=(httpservletresponse) response; HttpSession Session=req.getsession (); //GET Request PathString Path =Req.getrequesturi (); //get the field as a judge in the sessionstring pwd = (string) session.getattribute ("passwd"); //determine if the requested path contains a request for a login page//if it contains, then do not filter to continue the Operation if(Path.indexof ("/back/login.do") > 1) {Filterchain.dofilter (req, resp); } Else { //If not included, then it is necessary to determine whether there is a sign in the session, if there is no flag, then do not let him see, let him go to login, the contrary to perform operations! if(pwd = =NULL|| "". Equals (PWD) {Resp.sendredirect ("/back/login.do"); } Else{filterchain.dofilter (req, resp); } }} @Override Public voiddestroy () {}}
Configuring filters in Web. xml
<Filter> <Filter-name>Login</Filter-name> <Filter-class>Com.ahoo.filter.LoginFilter</Filter-class> </Filter> <filter-mapping> <Filter-name>Login</Filter-name> <!--Filter A folder below me here is the operation in the back directory - <Url-pattern>/back/*</Url-pattern> </filter-mapping>
Reference: http://www.cnblogs.com/haojieli/p/5559854.html thanks to the selfless dedication of the park friends!
Javaweb Filter--Login filter