I am building a "typical" J2EE appplication using a layered architecture consisting of:
1) Data domain layer with data access object
2) Business objects layer
3) MVC layer using Servlets and JSPS, and Data Transfer objects to communicate with the business layer.
I want to be able to validate data inputted by users on web screens. Now, this validation obviously has to be performed in the MVC layer. But surely, it is normal to define validations within the data domain objects also.
a) What is the best design approach for validating data, in such a way that it is re-usable and not have to be maintained seperately in 2 places?
There is, unfortunately, no best answer to this question. The various locations have different advantages and disadvantages.
2) Validation in the Java layer (DAO, Business Object or your MVC controller) has access to all of the server's information, and the best language (Java), but is Java specific.
3) Constraints in the database are language-independent validation logic, but give terrible error messages.
Put your validation logic in the best place for the kind of logic you are performing. Often this means duplicate validation logic in the various layers. While this may seem painful, think of it this way: each layer (client, server, database) is really a separate application. Therefore, for the most robust possible application, each layer should be responsible for its own validation.
In practice, many people ignore this rule, and only validate in one place. Also, people various code-generation strategies to define your validation logic in one place, and the generate the validation logic for each layer (at compile-time or even run-time).
Same in case of Data Side validation also,that we need to do on Data Laye/on Business Layes ,for this also better use Wrapper class which defines all the of error messages.