Hi I) What is the correct approach of programming. 1) Validating the form fields in the same servlet and call next page if correct. or 2)calling next page using 'Form Action' and validating fields in next page and if anything wrong then redirecting to previous servlet. II) Where (sites) can I find good study material for form validation etc. using servlet. Thanx Anu
This is a standard and well-known issue with server-side Java programming. The typical design pattern that is used to handle this is the Model View Controller (MVC) pattern. Basically the 'front' controller (usually a servlet) receives all incoming HTTP requests and delegates the processing to the appropriate business object(s) depending upon the action being submitted/requested by the user. The front controller will also determine the next screen that should be displayed to the user. This prevents a chain of dependancy occuring between servlets in the the application. The most commonly used MVC framework used to create Java applications is Struts. It can be a bit daunting at first but it does a pretty good job of handling form-based application. Hope this helps
Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
I) What is the correct approach of programming. 1) Validating the form fields in the same servlet and call next page if correct. or 2)calling next page using 'Form Action' and validating fields in next page and if anything wrong then redirecting to previous servlet.
In my opinion, I would go with option (2). This will allow you to keep your responsibilities seperate .i.e. presentation(form page) from business(rules to determine valid input data) logic. If not, your presentation page may become full of business logic, which could make the presentation page less useable and maintainable. Like Andy stated the MVC is a very popular architecture, which is based on separation of responsibilities. There are plenty of resources on the internet that will give you all of the information you may need, not to mention the Struts project. craig
In my views, It depends on the application requirements. The advantage of the first approach is good performance. The ability to validate the input on client side, is very important in some cases. I have worked with both the appraoches. and in the first one I used .js files for validations The second approach clearly seperates the designer from business logic.