Log in Filter

Source: Internet
Author: User

1  Public classLoginfilterImplementsFilter {2      Public voiddestroy () {3 4     }5 6      Public voidDoFilter (servletrequest request, servletresponse response,7Filterchain chain)throwsIOException, servletexception {8         /*9 * 1. Get the user in sessionTen * 2. Determine if NULL One * > If NULL: Save error message, forward to msg.jsp A * > If NOT NULL: Release -          */ -HttpServletRequest req =(httpservletrequest) request; theObject user = Req.getsession (). getattribute ("Sessionuser"); -         if(User = =NULL) { -Req.setattribute ("Code", "error");//in order to display the X picture -Req.setattribute ("msg", "You are not logged in, please log in first"); +Req.getrequestdispatcher ("You want to jump to the landing page"). Forward (req, response); -}Else { +Chain.dofilter (request, response);//Release A         } at     } -  -      Public voidInit (Filterconfig fconfig)throwsservletexception { -  -     } -}
The Web. XML is configured as
<Filter> <Display-name>Loginfilter</Display-name> <Filter-name>Loginfilter</Filter-name> <Filter-class>Filter class Path</Filter-class> </Filter> <filter-mapping> <Filter-name>Loginfilter</Filter-name> <Url-pattern>The page you want to filter</Url-pattern> </filter-mapping> <filter-mapping> <Filter-name>Loginfilter</Filter-name> <Servlet-name>The servlet you want to filter</Servlet-name> </filter-mapping> </filter-mapping>

Log in 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.