Thursday, July 8, 2010

ideal-way-of-exception-handling-in-multi-layer-java-web-application

Exception handling in web application is indeed a tricky task and it varies according to scenarios. There are no fix rules but yes I will give you a generic idea about better way of exception handling. Generally well structured application has four layers: Presentation Layer (JSP/HTML), Business Logic Layer (POJO containing business logic) and Database Access Layer (POJO to interact with Databse) and Data Layer(Database itself). First I will enlist all possible exception handling strategies and then describe suitable strategy for each layer.

Possible Strategies:
1) Manual exception handling using try/catch block to take necessary action
2) Using ‘throws’ clause to throw exception back to the caller method
3) Handling logical errors that needs to be displayed back to user (This is not exactly exception handling, you will come to know about this in later part of article.)
4) If you are using Spring framework then ‘Exception handling in Spring’
5) In case of JSP, Page Directive named as ‘errorPage’

FYI: Error code(e.g. 404, 408) handling in Web.xml is different from Exception handling, Don’t mix up. Know more about ‘Error code handling’ and ‘Exception handling in Spring’ here. (I don’t want to reinvent the wheel by rewriting this article.)

Applying Strategies To Each layer:
a) Database Access Layer:
All methods inside this layer are called by respective methods of Business Layer. This layer is not ideal place to handle exception. So, you can choose the below option,
1) Strategy#2: Define your method to throw exception back to Business method and handle it there.
e.g. boolean validateUserDAO(String name, String pwd) throws SQLException{…}

b) Business Logic Layer:
All validation part and business logic comes here.
1) Strategy#5: Usually validation related/logical errors are needed to display back to user beside proper field. E.g. “Username/Password is invalid.” – Such error message should appear just next to Username/Password field. In this case, with proper logic you need to route proper message to User. Attach attribute with proper error message in ‘Request Scope’ and render it in JSP.
2) Strategy#1(Manual handling)
3) Strategy#4 (Spring Framework strategies)
Let me put one sample model method here,

boolean validateUserModel(){
String name = request.getParameter(“..”); String pwd = request.getParameter(“..”);
try{
if(daoObject.validateUserDAO(name,pwd))
return true;
else{
request.setAttribute(“invalidLogin”,”username/password is invalid..”);
return false;
}
} catch(SQLException se){
request.setAttribute(“SQLError”,”Server is down.. Try again later”);
return false;
}
}


c) Presentation Layer:
If you are following good programming practice then chances of getting exception in JSP are less. It should contain rendering logic, strictly no business logic. Basically there are two ways,
1) Strategy#5: <%@ page errorPage=”/error.jsp” %>
This directive instructs to route the flow to ‘error.jsp’ in case of some error. This style of exception handling is bit awkward but of course depends on situation.
2) Strategy#1: Manual handling at least possible places.
Find Sample JSP below,


..
Username:
Password:
${invalidLogin}

${SQLError}
..

FYI: ${invalidLogin} is an Expression Language. It simply says, if attribute with name ‘invalidLogin’ exist in any of the scope then print its value otherwise do nothing. Not even printing ‘null’ in case of such attribute does not exist. Google it to know more about it.

d) Data Layer
As Database automatically propagates the exception to the Database Access Layer and in turn it throws back to Business Layer where it gets handled. No extra work needed, Sit back and relax!!

Let me conclude this, Exception handling has no strict rules. Important is knowing the possible strategies and their effectiveness.

No comments:

Post a Comment