title: Set FacesMessage by Filter
body: <p>In my JSF Application, there is a <code>Filter</code> which is used to check the authenticati0n process. When the authentication is failed, the filter redirect to <code>login.xhtml</code>.</p>
  
  <p>How can I pass the <code>FacesMessage</code> to  my login page from <code>Filter</code>? </p>
  
  <p>Although, I used below, It is not OK.</p>
  
  <pre><code>FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
  </code></pre>
  
  <p>AuthenticationFilter.java</p>
  
  <pre><code>public class AuthenticationFilter implements Filter  {
      private FilterConfig config;
      private ServletContext servletContext;
  
      public void init(FilterConfig filterConfig) {
          config = filterConfig;
          servletContext = config.getServletContext();
      }
  
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
          HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
          HttpSession session = httpRequest.getSession();
          User user = (User) session.getAttribute(Constants.LOGIN_USER);
          if (user == null) {
              ...
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
  
              String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
              httpResponse.sendRedirect(loginView);
          } else if (!user.getRole().equals(Role.SYSTEM_ADMINISTRATOR)) {
              ....
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
  
              String loginView = httpRequest.getContextPath() + Constants.LOGIN_PAGE;
              httpResponse.sendRedirect(loginView);
          } else {
              filterChain.doFilter(servletRequest, servletResponse);
          }
          servletContext.log("Exiting the filter");
      }
  
      public void destroy() {
          //Nothing to do.
      }
  }
  </code></pre>