Friday, July 4, 2008

Advanced Exception Handling Techniques in Struts

How to handle Exceptions in Struts based J2EE Applications:

There are so many possible exceptions that could arise in our applications. Some practical scenarios are discussed here today. They are

1. Compile-time Exceptions
2. Run-time Exceptions

Compile-Time Vs Run-Time Exceptions:

Compile-time exceptions are the ones that are expected to occur by various situations. These exceptions are caught and distributed to various other pages with the proper messages or warnings.

For example, we can take the scenario of the user trying to login with the invalid user and password. How do we catch this in J2EE applications?

After retrieving the result set from the database table, Checking if the result set is not empty would mean that the user exists matching the entered name. If not, throw the exception that the user not available. This is checked-exception. This is not a bug in the application, because as a developer, you expect this case to occur while developing the code.

Other way, we directly retrieve the name of the user from the result set assuming that the row exists matching the user name. If this leads to NullPointerException while retrieving, then it would automatically mean that the user is not found. This is run-time exception. This is a bug in the application. As a developer, you must fore-cast the side-effects of reading the data from the empty result-set.

Exceptions in Struts based J2EE applications:

Usually there are two ways in which you can catch the exceptions. They are

1. Declarative Exceptions
2. Programmatic Exceptions

Declarative Exception Handling:

In this way, we can catch the exception from action classes by declaring the name of the exception and the path to go when the specified exception arises. The declarative exceptions can be placed as action-specific or global. In action specific, we define these exceptions and path inside the action tag. Otherwise, the exceptions are defined inside the global-exception tag. This would mean that, if this exception arises in any action class, it would automatically forwarded to the specified path known as error-page. The error-page then shows the exact warning message.

Entry in struts-config.xml for Declarative Exception:

<action path = "/login" type= "com.siva.LoginAction" parameter="name" input="/login.jsp" >
<exception key="error.invalidPassword" type="RuntimeException" path="/index.jsp" />

Entry in

error.invalidPassword = Invalid password entered. Please try agin.

Programmatic Exception Handling

In this way, we check lot of if inside the try-catch block. If the exception is caught in the try block, the control flows into catch block, where we add the errors into the messages and put it in the session. Then the control is forwarded manually to the error-page where the warning message is displayed. The drawback of this method, is the developer is fully responsible for the various flow of applications where the chance of misleading is high.

Sample Code to check the exception programmatically:

ArrayList matchedUser = (ArrayList) ReadFactory.getService().getUserNames();
UserDTO user = matchedUser.getUser();
String userName = null;
String userPassword = null;

if (user==null) throw new Exception();
userName = user.getName();
userPassword = user.getName();
catch( Exception ex)
ActionMessages errors = new ActionMessages();

errors.add("userName", new ActionMessage("error.userName.not.found"));

saveMessages(request, errors);

if (errors.size()==0) return mapping.findForward("success"); else mapping.findForward("failure");

Ok. Thats all for today. We will see some more design concepts in days to come. You expect lot more advanced concepts next week. Bye for now.

In the next post, we will see 'Advanced J2EE Design Concept - How to use Abstract Classes & Inheritance in J2EE applications effectively'.

Still to come posts : Servlet - Filter Concepts, Authorization, Authentication Techniques.




hai hari said...

what is an layered architecture

hai hari said...

what is diff b/w strong referece and weak reference

hai hari said...

in which case we use Filters,
and what is the main diff b/w the sessionManagement interfaces like HttpSession,Filters,Cookies,Urlrewriting