Javaweb Filter--Login filter

Source: Internet
Author: User

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

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.