Last active
August 25, 2017 19:18
-
-
Save jyeary/28a767f81aae835d63774e64c505b031 to your computer and use it in GitHub Desktop.
This filter starts an {@link HttpSession} at the start of the request to avoid JSF late session binding. Based on BalusC suggestions https://stackoverflow.com/questions/8072311/adding-hform-causes-java-lang-illegalstateexception-cannot-create-a-session.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bluelotussoftware.servlets.filter; | |
import java.io.IOException; | |
import java.text.MessageFormat; | |
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.HttpSession; | |
/** | |
* This filter starts an {@link HttpSession} at the start of the request to | |
* avoid JSF late session binding. Based on BalusC suggestions | |
* <a href="https://stackoverflow.com/questions/8072311/adding-hform-causes-java-lang-illegalstateexception-cannot-create-a-session">Cannot | |
* create a session after the response has been committed</a>. | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
public class SessionFilter implements Filter { | |
private FilterConfig filterConfig; | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
this.filterConfig = filterConfig; | |
} | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
if (request instanceof HttpServletRequest) { | |
HttpServletRequest httpServletRequest = (HttpServletRequest) request; | |
HttpSession session = httpServletRequest.getSession(true); | |
httpServletRequest.getServletContext().log(MessageFormat.format("Session created {0}", session.getId())); | |
try { | |
chain.doFilter(httpServletRequest, response); | |
} catch (IOException | ServletException t) { | |
httpServletRequest.getServletContext().log("An exception occurred during processing.", t); | |
} | |
} | |
} | |
@Override | |
public void destroy() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment