jsf 2 - Login Logout in JSF 2 -
jsf 2 - Login Logout in JSF 2 -
i tried login , logout method refer article http://www.itcuties.com/j2ee/jsf-2-login-filter-example/ login method fine logout doesn't work!
loginbean.java
@managedbean @sessionscoped public class loginbean implements serializable { @ejb private usersfacadelocal usersfacade; public loginbean() { } // public string dologin() { string redirect = ""; if (usersfacade.loginuser(email, password)) { loggedin = true; redirect = navigationbean.redirecttomain(); } else { facescontext facescontext = facescontext.getcurrentinstance(); facescontext.addmessage(null, new facesmessage(facesmessage.severity_warn, "username or password incorrect", null)); redirect = navigationbean.redirecttologin(); } homecoming redirect; } // public string dologout() { externalcontext ectx = facescontext.getcurrentinstance().getexternalcontext(); httpservletresponse response = (httpservletresponse)ectx.getresponse(); httpsession session = (httpsession)ectx.getsession(false); session.invalidate(); loggedin = false; homecoming navigationbean.redirecttologin(); } // private string email; private string password; private boolean loggedin; @managedproperty(value = "#{navigationbean}") private navigationbean navigationbean; public string getemail() { homecoming email; } public void setemail(string email) { this.email = email; } public string getpassword() { homecoming password; } public void setpassword(string password) { this.password = password; } public boolean isloggedin() { homecoming loggedin; } public void setloggedin(boolean loggedin) { this.loggedin = loggedin; } public navigationbean getnavigationbean() { homecoming navigationbean; } public void setnavigationbean(navigationbean navigationbean) { this.navigationbean = navigationbean; } }
loginfilter.java
public class loginfilter implements filter{ @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpservletrequest req = (httpservletrequest)request; httpservletresponse res = (httpservletresponse)response; loginbean session = (loginbean)req.getsession().getattribute("loginbean"); string url = req.getrequesturi(); if(session == null || session.isloggedin() == false) { if(!url.equals("/login.xhtml")) { res.sendredirect(req.getcontextpath() + "/login.xhtml"); } res.sendredirect(req.getcontextpath() + "/login.xhtml"); } else { chain.dofilter(request, response); } } @override public void destroy() { }
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.1" 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"> <filter> <filter-name>loginfilter</filter-name> <filter-class>ejb.dao.loginfilter</filter-class> </filter> <filter-mapping> <filter-name>loginfilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> <servlet> <servlet-name>faces servlet</servlet-name> <servlet-class>javax.faces.webapp.facesservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>faces servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>faces/main.xhtml</welcome-file> </welcome-file-list> </web-app>
after logged in, when click logout button it's redirect login page. when re-create , paste main page url new tab in browser session still there.
any suggestion. in advance
jsf jsf-2 jsf-2.2 managed-bean
Comments
Post a Comment