Customized error pages for Glassfish V3

There are 2 main reason you want to create custom-made error pages to catch 404 (and others).

Default 404 Error Page

  1. It is more professional to catch an error and guide the user either back to a start page or some kind of error handling (appearing as part of you application and not some default Glassfish page which leaves him pretty much nowhere).
  2. You dont reveal any information about the application server and remove the most basic information for any hacker: What server software is running on this host ?

In Glassfish we can make use of 2 levels of error handling. At a global level, for the complete domain, and the application level. The application level error handling overrides the global handling. You can create a 404 error page that leads the user back to the application index and at the global level you create a message that does NOT contain a link to an application (entering a wrong context will show you there is no such application, but dont give you a hint where else to go!)

How to do it ?

Application Level

  • Create a webpage in your web folder (eg. 404.html) with your application design template and add a link to the index page if necessary.
  • Open the web.xml and add
    ...
     <error-page>
     <error-code>404</error-code>
     <location>/404.html</location>
     </error-page>
    </web-app>
    
  • or use the assistant in Netbeans

    web.xml in Netbeans

Glassfish Global Level

  • Open you admin console and add a new property
    send-error_1
    code=404 path=/tmp/404.html reason=Resource_not_found
    better: code=404 path=${com.sun.aas.instanceRoot}/docroot/404.html reason=Resource_not_found

    Glassfish Console

     

  • or execute asadmin
    asadmin set server.http-service.virtual-server.server.property.send-error_1=”code=404 path=/tmp/404.html reason=Resource_not_found”
  • or do it manually in the domain.xml
    ...
     <config name="server-config">
     <http-service>
     <access-log />
     <virtual-server id="server" network-listeners="http-listener-2,http-listener-1">
     <property name="send-error_1" value="code=404 path=/tmp/404.html reason=Resource_not_found" />
     </virtual-server>
     <virtual-server id="__asadmin" network-listeners="admin-listener" />
     </http-service>
    ...
    
  • Remark: You should place the code=xxx in front the property string (like above sample). There are some forum threads and tutorials putting it at the end (which does not work for me).
Advertisements