Hi, I have standard J2EE project: web interface and EJB business model. Data from user input has to be validated. Where shall I put validation code: servlets/JSP or in business model? What is standard practice? Maybe there is some kind of pattern or best practice for this. Thanks and regads, Vladas
Hi Vladas, you have a few options here. 1. If youre using a business delegate you can incorporate validation within that tier. 2. Use servlet filters as decorators to validate your data before it leaves the web tier. 3. Validate in EJB tier with session beans. This can cause unnecessary overhead if your EJB tier is remote. 4. Use Struts. It has good inherent data validation support. 5. Place validation code within the value objects. 6. Validate in Entity EJB. This is not recommended, as Enity EJB should not be used for business logic. And your value object may not map directly to the EJB. Personally I would go for a design with clean separation of responsibilities, and use validation helper classes in the web tier, which is closer to where you are presenting and modyfying the data objects. For simple flow of logic, use a servlet filter to validate your javabeans before calling BD or EJB's. For more complex requirements go with Struts, it is a higher learning curve but is powerful. Whichever you choose, your validator helper classes should be portable enough to be called from any tier within your app. The patterns you should consider for implementing validation helpers are Factory and Strategy.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database