How to eliminate duplication of validation code


Web tier: servlets, JSP, Web frameworks: How to eliminate duplication of validation code

  1. How to eliminate duplication of validation code (3 messages)

    Hi everyone.

    We have a web application that uses all of the main components of the J2EE architecture - namely:

    1) Java Servlet controller (with JavaBeans)

    2) JSP (View) (with JavaBeans)

    3) Session EJBs with business logic/shopping cart

    4) Entity EJBs (Model)

    5) Value Objects for performance reasons

    So, suppose, a customer has to register and provide details of name, addresss and few more bits and pieces. Let's suppose name and 2 lines of the address are mandatory.


    a) In the Servlet controller?
    b) In the JavaBeans representing the data in the web container?
    c) In the value object
    d) In the Entity EJB
    e) In ALL or a COMBINATION of the above

    I'm rightly confused here as to the best approach. I obviously do not want to be duplicating logic, if at all possible, yet I want good and quick feedback to the user.

    Can someone provide me with some guideance?


  2. hi jimmy,

    i think the kind of validations u r talkin bout shud go in the enntity model. since the entity objects represent the actual data namely name, address etc. each entity objects shud validated itself.

    for example u cud have a address entity with attributes like city, state, zip, address1, address2. now this entity shud validate it self by having a method validate which checks for the integrity of the data whether its null or not or minimum lengths etc.

    i think this is thebest way to data integrity validation.

    hope this helps. lemme know.

  3. The best way is to minimise the load on the server and do the validation on the client side. Therefore you can conveniently use javascript validation for length of the field, if the field is null or contains valid/invalid characters

    This is definitely fast and reliable.

    Hope this helps.
  4. Well client side validation is one solution, but the long term one and not the best one. since u have entity beans in ur application they shud be responsible for protecting the integrity of the data that they own.

    a simple example what if tomorrow i want to write a WML/HDML (hand held devices) front-end to ur application using ur existing infrastructure(session beans/entity beans). does that mean i do that validation again for my handheld devices. i dont buy that. the idea is to write such a middle tier/application infrastructure that it can be ported with minimal effort to any front end.

    and again i repeat since ur entity beans represent the data u want to validate they themselves shud be responsible of protecting the validity of that data. u dont want to delegate that task to someone else.

    yes client side validation avoid that extra network trip u incur when u ask ur entity beans to validate the data. but i thing that extra network trip is worth it for the clean application u will get.

    hope i have supported my logic well.