Sunday, March 10, 2013

Best Practices Common and Important Architecture for Web Application Using Java


An architecture is the most important part for any software. The whole software depends on the architecture, you follow initially. It is very difficult or almost impossible, to change the architecture of any software application which is already build. A good architecture is utmost requirement of good Quality Attribute.

Today I will discuss the architecture for the web application. This architecture will help for new comers and also for others. This architecture is not only for Java based web application, but also for Web application built in PHP, dotnet , etc. I am here to design the general architecture. Generally if a good architecture is not followed, then at the end we try to fix issues according to our requirement and the application finally becomes too complex, to make changes. Please do read about Software Architecture http://en.wikipedia.org/wiki/Software_architecture

This blog is for people who want to design good architecture in their web applications. This architecture is the layered architecture. There are different frameworks available in java like struts,spring,etc. The Struts and Spring are themselves following MVC patterns. So, if you are using these framework, almost you are half-way to good architecture. But if you are not using these frameworks, then you need good architecture. Following is the general architecture which I generally follow.


Now, the question is "How can we implement them in any language". I will discuss this in terms of J2EE. In my next blog I will list some some of my classes for PHP which might be useful for all.

Views are JSP's and Controllers are Sevlets. Every new request goes to the servlet. Servlet will interact to Service or Business layer and then it forward the request to the desired JSP(view). Following are some of the guidelines, which are preferred.

  • Keep all the configurable parameters in the properties files. With properties file we can easily keep (key,value) pair.The simplest example is to keep SQL Connection properties in connection.properties file. The advantage of this approach is that, when we want  to change any value, in that case, we don't have to touch code, we can directly change in these files and also it can be easily maintained by non technical guys. These properties file are kept in a separate folder( this folder should be in classpath).
  • Create Interfaces for each property file. As we know, interfaces variables are "public static final". Now link each property (key) with a interface variable. The advantage of this approach is that you can directly use these variables, just by name of interfaces, irrespective of the fact that the value can change in near future. Also create a separate package for these interfaces.
  • Create POJO's. These POJO's are generally called Transfer Objects. These Transfer Objects are used to pass values from one layer to another. Create a package for POJO's
  •  Create useful utilities. The utilities are the objects which are commonly used. For example make a class of Database utility. The Database utility contains methods like getConnection(), closeConnection(). So, if tomorrow you want to switch to a different database you can just change the getConnection() method, without affecting other layers. Create a separate package for utilities.
  • Create separate package for controllers, exceptions(your own Exception handling), business logic
  • In case of JSP's make a separate folder for css, images, java script. If possible try to use Apache tiles. The advantage of using tiles is that you can make your layout. The layout may contain a header and footer. So, if in future you want to change the layout, you have to change in your layout JSP's and whole layout of your web-application gets changed.

Following diagram show the package wise architecture diagram.



Following is the sample application.Click here to download sample architecture

 Please give your valuable feedback and also tell the ways to improve the architecture.