JSF Exception Handling

In this post, I will show how to create error handler for JSF and Restful web service by using Wildfly Application Server. In the programming development, exception handling is very important part in the development process. Handling exception properly improve user exprerient and elevate security. I'll show how to handle exception for JSF pages and webservices.

For this post, I have arranged content as below

  1. Handling Exception for JSF page with web.xml
  2. Handling Exception for Rest Webservice
  3. References

For my environment set up, I have set up as following

  • JDK 11
  • Wildfly Application Server 23.0.2

Handling Exception for JSF page with web.xml

Exception handling is the fundamental feature in Java. For the exception handling we could simply handle and configure through web.xml file. In the web.xml file, we could handle exception by error code or error type. To handle error, we just configure in web.xml file as below.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>sample-exception-web</display-name> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <context-param> <param-name>org.richfaces.skin</param-name> <param-value>ruby</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <error-page> <exception-type>java.io.IOException</exception-type> <location>/error.xhtml</location> </error-page> <error-page> <error-code>404</error-code> <location>/error/404.xhtml</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500.xhtml</location> </error-page> </web-app>

Next, we need to create error pages for each errors code include error.xhtml, /error/500.xhtml, /error/404.xhtml. For detail, I have create as below.

error.xhtml
      
        
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head></h:head> <body> <h2>Error page</h2> <h4>Error message</h4> #{requestScope['error-message']} <h4>Stack trace</h4> <ul> <ui:repeat value="#{requestScope['error-stack']}" var="stack"> <li>#{stack.className} - #{stack.methodName} - #{stack.fileName} (#{stack.lineNumber})</li> </ui:repeat> </ul> </body> </html>

/error/500.xhtml

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:rich="http://richfaces.org/rich" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:h="http://java.sun.com/jsf/html"> <h:head></h:head> <body> <rich:panel> <f:facet name="header"> Server Processing Error </f:facet> <h:outputText value="Processing Error !!!!!!!" /> </rich:panel> </body> </html>

error/404.xhtml

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:rich="http://richfaces.org/rich" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:h="http://java.sun.com/jsf/html"> <h:head></h:head> <body> <rich:panel> <f:facet name="header"> Page Not Found </f:facet> <h:outputText value="Page Not Found !!!!!!" /> </rich:panel> </body> </html>

Once, we finished create error page, we can see the result as following.

Handling Exception for Rest Webservice

When you provide any webservice to any application, instead of return html page, you need to provide a meta data as a response for the request. In the same manner, once exception handler occured, you also need to populate meta data and send it back to the client application.

In JAX-RS, Exception handlering could be implement easily as same as exception handlering in the servlet.Exception could be catch and thrown through custom exception handler. To create Custom Exception handler, we need to create class that implement ExceptionMapper. In the example, we use Entity class as the data to send back to client.

Entity.class

  
package pro.tikkwiki.sample.entity; public class Entity { private String message; public Entity(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

MyExeptionHanlder.java
    
      
package pro.tikkwiki.sample.exception; import java.io.IOException; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class MyExceptionHandler implements ExceptionMapper<IOException> { @Override public Response toResponse(IOException resp) { Entity ent = new Entity("Exception Entity"); return Response.status(500).entity(ent).build(); } }

Next, we need to create application endpoint and web service

ApplicationEndPoint.java

  
package pro.tikkwiki.sample.web.service; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/webservice") public class ApplicationEndPoint extends Application { }

And for the webservice, we created as below

MyWebService.class

  
package pro.tikkwiki.sample.web.service; import java.io.IOException; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import pro.tikkwiki.sample.entity.Entity; @Path("/entity") public class MyWebService{ public MyWebService() { super(); } @Path("/data") @GET @Produces(MediaType.APPLICATION_JSON) public Entity doIt() throws IOException { throw new IOException("IO Excetpion Error"); } }

Once we call the web service, we shall get below result

In this example, we have show how to handle IOException, But you can customize any exception you wish to.

References

For the references, I have use below.

  1. Exception Handling
  2. Servlets - Exception Handling

No comments:

Post a Comment

Feature Recently

Running Wildfly Application Server in Domain Mode

  Wildfly application server provides two modes of how to run application one wildfly application server. It is very simple if you run your ...

Most Views