Java Servlet Filter Implements Web Logoff login filter __java

Source: Internet
Author: User

  Below is the Web logoff login filter I implemented using the servlet filter in my project:

Package com.sunfairs.filter; Import java.io.IOException; Import Java.util.Date; Import Javax.servlet.Filter; Import Javax.servlet.FilterChain; Import Javax.servlet.FilterConfig; Import javax.servlet.ServletException; Import Javax.servlet.ServletRequest; Import Javax.servlet.ServletResponse; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Import javax.servlet.http.HttpSession; Import Org.apache.commons.logging.Log; Import Org.apache.commons.logging.LogFactory; Import Com.sunfairs.bean.LoginInfo; Import Com.sunfairs.bean.LoginLog; Import com.sunfairs.constant.ShopConstants; Import Com.sunfairs.dao.LoginInfoDAO; Import Com.sunfairs.dao.LoginLogDAO; public class Logoutfilter implements Filter {private static final log = Logfactory.getlog (Logoutfilter.class); privat e Logininfodao Logininfodao; Private Loginlogdao Loginlogdao; public void Destroy () {} public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) tHrows IOException, servletexception {httpservletrequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response; HttpSession session = Httprequest.getsession (); Httprequest.setcharacterencoding ("UTF-8"); String Requestpath = Httprequest.getrequesturi (); if (Requestpath.indexof ("J_logout")!=-1) {String userName = (string) session.getattribute (Shopconstants.login_user); String realname = (string) session.getattribute (shopconstants.login_real_name); Logininfo logininfo = Logininfodao.findbyusername (userName); Logininfodao.delete (Logininfo); Loginlog Loginlog = new Loginlog (); Loginlog.setusername (UserName); Loginlog.setrealname (Realname); Loginlog.setoperate ("logout system"); Loginlog.setcreateddate (New Date ()); Loginlogdao.save (Loginlog); /** Session.removeattribute (shopconstants.login_id); Session.removeattribute (Shopconstants.login_user); Session.removeattribute (Shopconstants.login_real_name); Session.removeattribute (Shopconstants.login_power); Session.removeattribute (shopconstants.login_department); * * Session.invalidate (); Httpresponse.sendredirect ("/shop/system_login.jsp"); } else{Chain.dofilter (request, response);} public void init (Filterconfig arg0) throws servletexception {This.logininfodao = new Logininfodao (); This.loginlogdao = n EW Loginlogdao (); } }

Related Article

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.