jsf - Servlet filter which is mapped on /home is not invoked when I open /home.xhtml -
i new java enterprise edition. started learning youtube videos, , started reading http://docs.oracle.com/javaee/6/tutorial/doc/ finished chapter 15.
i tried make own filter.
i didn't use java servlet class. because want use jsf pages, , far know possible use managed beans jsf pages, whereas servlet classes work jsp. ok.
as far know usefulness of login filter: https://stackoverflow.com/tags/servlet-filters/info
[...] particularly useful when have multiple pages you'd check logged-in user. instead of copypasting same logic on pages, can use filter have in single place.
it useful (as know) in case when user type url directly browser page require logged in user, filter redirect him login page or continue if logged in.
i searched simple example learn didn't find. put simple example:
i have 2 jsf pages
1 named home.xhtml (which require logged in user)
other 1 named login.xhtml (filter must redirect if non-logged users seek home)
login.xhtml:
<h:form> <h:panelgrid columns="2"> <h:outputlabel value="name:"/> <h:inputtext value="#{user.name}"/> <h:outputlabel value="password:"/> <h:inputsecret value="#{user.password}"/> </h:panelgrid> <h:commandbutton id="btn" value="login" action="#{user.login()}"/> </h:form> home.xhtml:
<h:body> hello #{user.name}. welcome </h:body> user:
@managedbean @sessionscoped public class user implements serializable { string name; string password; authentication authentication; public user() { authentication = new authentication(); } //getters , setters name , password. public string login() { if (this.getname().equals("user") &&(this.getpassword().equals("1234"))) { authentication.setloggedin(true); facescontext context = facescontext.getcurrentinstance(); context.getexternalcontext().getsessionmap().put("auth", authentication); return "home"; } else { authentication.setloggedin(false); facescontext context = facescontext.getcurrentinstance(); context.getexternalcontext().getsessionmap().put("auth", authentication); return "login"; } } } authentication:
@managedbean @sessionscoped public class authentication implements serializable { private boolean authenticated; public authentication() { authenticated = false; } public boolean isloggedin() { return authenticated; } public void setloggedin(boolean authenticated) { this.authenticated = authenticated; } } loginfilter:
@webfilter(value = "/home") public class loginfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception { //throw new unsupportedoperationexception("not supported yet."); } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws servletexception, ioexception { httpservletrequest req = (httpservletrequest) request; authentication auth = (authentication) req.getsession().getattribute("auth"); if (auth != null && auth.isloggedin()) { system.out.println("filter working"); chain.dofilter(request, response); } else { system.out.println("filter working"); httpservletresponse res = (httpservletresponse) response; res.sendredirect(req.getcontextpath() + "/login.xhtml"); } } @override public void destroy() { //throw new unsupportedoperationexception("not supported yet."); } } faces-config:
<navigation-rule> <from-view-id>/login.xhtml</from-view-id> <navigation-case> <from-outcome>home</from-outcome> <to-view-id>/home.xhtml</to-view-id> <redirect/> </navigation-case> <navigation-case> <from-outcome>login</from-outcome> <to-view-id>/login.xhtml</to-view-id> <redirect/> </navigation-case> </navigation-rule> web.xml:
<context-param> <param-name>javax.faces.project_stage</param-name> <param-value>development</param-value> </context-param> <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>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/login.xhtml</welcome-file> </welcome-file-list> now when type url of home.xhtml page (after clearing history & cookies) browser assumed redirect me login page. instead goes home empty value name:
hello #{user.name}. welcome rendered hello . welcome
even system.out.println("filter working"); not print anything.
are sure filter called? if there nothing printed system.out guess not. problem might servlet mapping.
you specified this:
@webfilter(value = "/home") public class loginfilter implements filter {...} i think matches url /home. try use /* or /home* (which limiting, not recommend it) instead.
another thing: if hello #{user.name}. welcome output, facesservlet not called. might have 2 reasons:
- you use wrong mapping. try call page
/faces/home.xhtmlor/home.jsfinstead. url depends on type of mapping have inweb.xml. - the
facesservletnot configured correctly/at inweb.xml.
Comments
Post a Comment