HOME | J2ME | Struts | AJAX | SOAP | SOA MEDIA STREAMING AXIS |
Struts for Begginers
Introduction
MVC Design Pattern
Installation
Action Servlets
Hello World
Forms & ActionForms
Messages Resources
Data Bases
Exceptions
Acess ActionForms from JSP
Logic Tag

 

 

Struts for Beginners: Exception Handling

Back | Tutorial Home | Next

Like all other configurable features in Struts, Exceptions can be also handled using configurations. This configuration methodology is usually called declarative exception handling. Normal exception handling we do is programmatic exception handling.

Struts configuration file can be configured to catch exception. Not a single line is needed in your code needs to be changed to handle these exceptions.

Shut down your database and execute the login page. When you submit the login details, the browser overflows with exception messages. If you properly analyze these error messages, you will find that the root cause is “java.sql.SQLException”.

Now let us handle this exception the STRUTS WAY.

Create a JSP page “dbServerDown.jsp” with error message. This is page to be displayed when the exception occurs.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>
Service Unavailable
</title>
</head>
<body>
<html:errors/>
</body>
</html>

Add the exception handling declarations in the Struts configuration file

<action path="/loginAction"
type="com.salsa.UserLoginAction"
name="userLoginForm"
input="/loginForm.jsp"
cancellable="true">
<forward name="loginsuccess" path="/loginStatus.jsp" />
<forward name="logincancel" path="/loginCancel.jsp" />
<forward name="loginfailure" path="/loginFailure.jsp" />
<exception type="java.sql.SQLException"
key="db.exception"
path="/dbServerDown.jsp" />
</action>

Finally create the error message referred to as “key” in the above declaration. This error message should be configured in MessageResources.properties

db.exception=Internal Error, Please Try after some time....................

Run the login program in the browser. Do a proper login once and then shut down your database. Execute the login page again. When you submit the login details you will be shown the error message you have configured in the MessageResources.properties.

If we properly analyze the code above we haven’t done any changes in the Action class, all we have done is do configurations in the configuration files.

Back | Tutorial Home | Next

site comments powered by Disqus
Download our free toolbar

toolbar powered by Conduit

| Copyright © 2009. All rights reserved | Terms and Conditions | About | Contact | Feed Back |