title: Intercepting Exceptions with custom JSF event handlers
body: <p>Is there any way to create a custom <code>ActionListener</code> class in JSF so that I can intercept an exception if it occurs on pressing the command button using my own ActionListener class</p>
  
  <p>I tried using the following code:</p>
  
  <code>firstpage.jsp</code>
  
  <pre><code>&lt;f:view&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;title&gt;firstpage&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;h:form&gt;    
          &lt;h:commandButton id="button" value="submit"
                           actionListener="#{databasebean.fetchUserName}"/&gt;
        &lt;/h:form&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/f:view&gt;
  </code></pre>
  
  <code>Dao.java</code>
  
  <pre><code>package getexception;
  
  import java.sql.*;
  import java.util.ArrayList;
  import javax.faces.event.ActionEvent;
  
  public class Dao {
  
      String firstname;
      private Connection con=null;
      private Statement stmt = null;
      private ResultSet rst = null;
      private ArrayList ar;
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String url = "jdbc:odbc:" + "new";
      public Dao() {
  
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getFirstname() {
          return firstname;
      }
      public void fetchUserName(ActionEvent ae) throws Exception{
          Driver d= (Driver)Class.forName(driver).newInstance();
          con = DriverManager.getConnection(url,"","");
          stmt = con.createStatement();
          rst=stmt.executeQuery("select firstname from employee where firstname like '%e'");
          if(rst.next()) {
              this.setFirstname(rst.getString("firstname"));
          }
          rst.close();
          stmt.close();
          con.close();
      }}
  </code></pre>
  
  <code>NewActionListener.java</code>
  
  <pre><code>package getexception;
  
  import com.sun.faces.application.ActionListenerImpl;
  import javax.faces.application.Application;
  import javax.faces.application.NavigationHandler;
  import javax.faces.context.FacesContext;
  import javax.faces.event.ActionEvent;
  
  public class NewActionListener extends ActionListenerImpl{
  
      public void processAction(ActionEvent event) {
      try {
          System.out.println("try block");
          super.processAction(event);
  
      } catch (Exception exception) {
          System.out.println("catch block");
          FacesContext facesContext = FacesContext.getCurrentInstance();
          Application application = facesContext.getApplication();
          NavigationHandler navigationHandler = application.getNavigationHandler();
          navigationHandler.handleNavigation(facesContext,null,"exceptionNavigation");
          facesContext.renderResponse();
          }
      }}
  </code></pre>
  
  <code>faces-config.xml</code>
  
  <pre><code>&lt;faces-config xmlns="http://java.sun.com/JSF/Configuration"&gt;
    &lt;managed-bean&gt;
      &lt;managed-bean-name&gt;databasebean&lt;/managed-bean-name&gt;
      &lt;managed-bean-class&gt;getexception.Dao&lt;/managed-bean-class&gt;
      &lt;managed-bean-scope&gt;application&lt;/managed-bean-scope&gt;
    &lt;/managed-bean&gt;
  
    &lt;navigation-rule&gt;
      &lt;navigation-case&gt;
        &lt;from-outcome&gt;exceptionNavigation&lt;/from-outcome&gt;
        &lt;to-view-id&gt;/error.jsp/&lt;to-view-id&gt;
      &lt;/navigation-case&gt;
    &lt;/navigation-rule&gt;
  
    &lt;application&gt;
      &lt;action-listener&gt;getexception.NewActionListener&lt;/action-listener&gt;
    &lt;/application&gt;
  
    &lt;managed-bean&gt;
      &lt;managed-bean-name&gt;backing_hellopage&lt;/managed-bean-name&gt;
      &lt;managed-bean-class&gt;getexception.backing.Hellopage&lt;/managed-bean-class&gt;
      &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
    &lt;/managed-bean&gt;
  &lt;/faces-config&gt;
  </code></pre>
  
  <code>error.jsp</code>
  
  <pre><code>  &lt;html&gt;
      &lt;f:view&gt;
        &lt;head&gt;
          &lt;title&gt;firstpage&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;
          &lt;h:form&gt;
            &lt;h:outputText value="Exception succesfully navigated"/&gt;
          &lt;/h:form&gt;
        &lt;/body&gt;
      &lt;/f:view&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>The code generated an SQL exception but it was not showing me the <code>error.jsp</code> page. </p>
  
  <p><strong>What have I misconfigured so that the error page is not showing when an exception fired?</strong></p>