java - How to redirect a jsf page to home page if session does not exist -
am creating jsf application have embedded applet in main page. problem cant figure out how check if session exist before loading applet main page , if session not exist want redirect user home page
i recommend use filter:
public class loggedfilter implements filter { @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpservletrequest httpservletrequest = (httpservletrequest) request; httpsession session = httpservletrequest.getsession(false); if (session == null) { httpservletresponse httpservletresponse = (httpservletresponse) response; httpservletresponse.sendredirect(httpservletresponse.encoderedirecturl(httpservletrequest.getcontextpath() + "/")); } else { chain.dofilter(request, response); } }
and of course map filter jsf pages in web.xml
:
<filter> <filter-name>loggedfilter</filter-name> <filter-class>loggedfilter</filter-class> </filter> <filter-mapping> <filter-name>loggedfilter</filter-name> <url-pattern>*.xhtml</url-pattern> </filter-mapping>
(i assume jsf pages have suffix xhtml)
Comments
Post a Comment