File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Repeated code in Servlet and Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Repeated code in Servlet and Bean" Watch "Repeated code in Servlet and Bean" New topic

Repeated code in Servlet and Bean

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
I have a Servlet and JavaBean working with my formPage.jsp where the jsp has input values and they are validated using JavaBean and Servlet. It works where it also gives a message of how many validations are needed and gives field validation message next to each field that has a validation issue.

Now I want to make it more efficient because I seem to be double coding where I am doing validation in both my Servlet and Bean. For example this condition is repeated in both Bean and Servlet:

Here is my Bean condense to show just one out of my 10 fields:

Servlet Controller condense to show 1 out of the 10 fields:


I am using Scriptlet because my environment will not allow EL or JSTL or Struts. We are using Tomcat 4.1.27.
[ August 07, 2007: Message edited by: Dan Parsons ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

One thought..

Remove the validation from your servlet and have the bean return a list of errors after all the fields have been populated.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Repeated code in Servlet and Bean
jQuery in Action, 3rd edition