Am creating a JSF application where I have embedded a applet in the main page. my problem is that i cant figure out how to check if a session exist before loading the applet into the main page and if the session does not exist I want to redirect the user to home page
Asked
Active
Viewed 1,567 times
1 Answers
1
I would recommend to 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 this filter to all 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 that JSF pages have suffix xhtml)

Leos Literak
- 8,805
- 19
- 81
- 156
-
-
I appended how to map this filter to web.xml. If you have not heard of this file, I recommend you to start with some tutorial. – Leos Literak Apr 26 '14 at 08:19