Validation in EJB: Where should I do it? Part 1 One of the problems that developers face is deciding where to perform validation. Should it be done on the client side or the server side? The answer to this varies, depending upon the situation.If one validates on the client side, it's more efficient from a network standpoint. However, the validation must be duplicated for different user interfaces, which could create redundancy or, worse, inconsistent validation! If one validates on the server side, the advantage is a centralized location for validation, which many different user interfaces can access. The disadvantage is the network conversations that could be associated with this model. Enterprise JavaBeans (EJB) gives us yet another option: dependant object validation! A dependant object is offered as a design option for pass-by-value objects. It is "dependant" because it relies on the entity that owns it to give it contextual meaning. For example, a phone number object only has value in the context of the entity to which it belongs, such as an employee or customer object. Since dependant objects are normally small and granular, they are good choices for pass-by value. On the user endwhether it's via a Graphic User Interface (GUI), Servlet, or Java Server Page (JSP)the EJB component of the interface can create a dependant object when needed. That dependant object can then validate itself! This creates a situation where the validation actually occurs on the client, but the logic resides in the class, which lives on the server. In our next Java TechMail, we'll look at some sample code for this technique. |
Validation in EJB: Where should I do it? Part 2 If you are in the process of choosing between client-side validation and server-side validation, Enterprise JavaBeans (EJB) technology allows you to split the difference with dependant object validation.Below, you can view sample code for self-validation of a ZIP-code-dependant object: public class ZipCode implements java.io.Serializable { // Variables for parts of zip code public String zip; public String ext; // Start validation! public ZipCode(String zc) throws Exception { if(zc == null) { throw new ValidationException("Zip code can not be null"); } else if((zc.length() == 5) && !(isDigits(zc))) { throw new ValidationException("Zip code must be all numeric"); } else if(zc.length() == 10) { if(zc.charAt(5) == '-') { zip = zc.substring(0,5); if(isDigits(zip)) { ext = zc.substring(6); if(isDigits(ext)) { return; } } } } throw new Exception("Zip code must have the format of #####-####"); } // Function used by validation to determine if characters are // numeric. public boolean isDigits(String str) { for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); if(!Character.isDigit(c)) { return false; } } return true; } // Public methods public String getZip( ) { return zip; } public String getExt( ) { return ext; } public String getZipCode ( ) { return zip + '-' + ext; } } Keep in mind that dependant objects only work if your EJB implementation supports pass by value. Some CORBA implementations pass values by using CORBA structures. |