Java JSP coding standards, checklist and best practices

The following can be mentioned as some of the best practices to follow in a Java based project.
Most of these items , I have personally encountered in my previous projects and some I learned it the hard way.I will explain individually on importance of each one of these in future posts.

  • Provide meaningful descriptions for every class and method.
  • Import Statement should be as minimal and as narrow as possible.
  • All class variables and method variables should be initialized with base values.
  • For any operation on String, Null Check should be mandatory to avoid Null Pointer Exception.
  • Any connection obtained should cleared and closed.
  • Always close streams.
  • Stored Procedures always preferrable compared to SQL queries from Java.
  • In case of more than 3-4 ‘if’ conditions a case statement will be better choice.
  • Don’t initialize String values with “null”.
  • After catching exceptions don’t consume it, throw the exceptions to base class/ handle it.
  • Use the framework used in the application. MVC is generally followed in most applications and validate this with existing code.
  • All user inputs should be validated using Javascript.
  • Do not use any class which is copyrighted and we don’t have access/permission/ability to change the source code.
  • While using String operations StringBuffer is always preferred over a String “+” operation.
  • No hard coding for userIds or security Strings. Use property files.
  • Code should not have any System.out.println statements, this will affect performance in server. User logger instead.
  • Commented code and unused code should be removed and cleared.
  • While using RAD/Ecipe/WSAD use formatter tool to format your code. Set the parameters for line maximum length and this will give a readable code.
  • In JSPs use style classes effectively this will save time when changing the look and feel.Do not enter values in tag elements, which can be declared in a style class.
  • Use existing code/functions for achieving most operations, this will save time and create a standard way of coding.
  • Do not use drprecated classes/methods. These functions will stop working in future Java revisions.
  • Write reusable classes, which can be used in multiple screens/functions.
  • Define a valid failure case in struts config.
  • Passing arguments by reference will save memory and improve performance if there are multiple calls for the same method.

What is the use of managed-bean-scope flag in the faces-config.xml (JSF)?

What is the use of managed-bean-scope flag in the faces-config.xml in JSF ?


Managed bean scope defines the lifespan of managed bean. There are 4 possible values: “application”, “session”, “request” and “none”.

The request scope bean is short-lived. If the bean is needed and created when server receives an HTTP request, it will be destroyed as soon as the response is sent back to the client.

The session scope bean persists from the time that a session is established until session termination. A session terminates if the web application invalidate session manually, or if it times out (this timeout period is defined in your servlet container configuration, and usually lasts few minutes). This means that after your bean is created, it will last for some time and between requests. For example, in a shopping application, you want the server to remember the contents of the shopping cart for some user across several pages.

Application scope beans are persisted for the duration of the application.

“None” is not exactly a scope, and is used for beans that are used only as managed properties of another beans.

Services.exe using up all your memory ? and windows hangs after launch?

I believe many would have faced this problem when launching windows. Whenever your system reboots due to power failure / Application error / Windows Error, the system becomes extremely slow. This problem mostly occurs when you are running Windows 2000/XP/VISTA. Developers using heavy Java (JVM) applications like WSAD , Sun One Studio face this problem.

This problem can be any one of the following :

1. Services.exe eating up your memory (50-70% of RAM).
2. Hard drive problems.
3. Memory Hardware Problems.
4. Virtual Memory problems

Here Im going to tell you how to fix the first Problem.

Whenever Windows recognises any failure either software/Hardware it makes a entry in the system log. And when you start your machine again this system log entry will be checked and Windows will try to search for a possible solution / or try to analyse the causes of failure.How to check this..

Click ctrl + alt + del – Open your task manager and click processes. And see how much memory the process services.exe is using, If it exceeds say 10%, you can be sure you are having the diagnostic problem.In the below picture the services.exe is running normally.

Task Manager showing services.exe

Now to the solution .. Go to Control Panel, click Administrative tools. In Administrative tools select Event Viewer, there will three event logs on left side namely Application,Security and System. Select each one of them separately and right click. In the menu click Clear All Events. When Prompt asks for saving log click No. Do the same thing for the three categories.
Clear All events to fix services.exe problem
Basically you have cleared all your system logs.So once you restart your Windows your servies should show only 1-5% usage. And most probably your problem should be solved.

If you still face any more problems. You can drop me a mail or check my other posts…