Hidding stack traces to users in Struts

When an error happens in a web application a stack trace is shown to the user. How to avoid this and show a beautiful error page while logging internally the error that happened? Use the Struts exception handler.

Add the following code to your struts-config.xml. If you don’t want to show different error messages you only need that exception with type=”java.lang.Exception”. The other ones are to show different error messages using Spring exception hierarchy.

    <exception key="errors.optimistic.locking.failure" path=".error"
    <exception key="errors.data.access.resource.failure" path=".error"
    <exception key="errors.data.retrieval.failure" path=".error"
    <exception key="errors.data.access" path=".error"
    <exception key="errors.transaction" path=".error"
    <exception key="errors.unknown" path=".error"

  • errors.optimistic.locking.failure=The object was already modified
  • errors.data.access.resource.failure=Can’t connect to the database
  • errors.data.retrieval.failure=The object you try to access doesn’t exist
  • errors.data.access=Data access error
  • errors.transaction=Transaction error
  • errors.unknown=Unknown error

If you’re using log4j for logging add to your log4j.properties

log4j.rootLogger=ERROR, stdout
log4j.logger.org.apache.struts.action.ExceptionHandler=DEBUG, stdout

# Console output...
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

This way the stack traces are logged to standard output (in Tomcat this is logged to logs/stdout.txt)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s