Well, there are a couple of things you could do to improve your application,
without using a framework...
1. Write a little utility method that sets properties on an object based
upon request parameter values (Struts does this for you out of the box)...
public void setPropertiesFromParameters( Object bean, HttpServletRequest
request )
{
// Use something from commons beanutils
}
So, in your servlet...
MemberValidateForm form = new MemberValidateForm();
setPropertiesFromParameters( form, request );
This eliminates a LOT of code, provided you name your request parameters
using some sort of standardized naming convention.
2. Your validate method should probably take an instance of some sort of
error message collecting object (called ErrorMessages for example) as a
parameter rather than the values for all of the properties. The values for
the properties should be set before calling the validate method (using
aforementioned utility method). Your JSP page would then iterate through
the error messages in the "collector" object and print them out. Also, you
could use commons-validator (like Struts does).
You should REALLY switch to using some form of a framework rather than
building this stuff yourself!
-----Original Message-----
From: sdgesa gaeharth [mailto:pollux1234567890@xxxxxxxxx]
Sent: Thursday, September 09, 2004 11:54 AM
To: users@xxxxxxxxxx
Cc: james@xxxxxxxxxxxxxxxxxxxx
Subject: RE: [cinjug-users] newbie trying to learn m-v-c
Well I think I got it!! Thanks for the support. Here
is my final servlet. One last question...Is there
anything I can do to improve this app(besides using a
framework). Is it considered "thread-safe" and
efficient? Or Should these lines(
this.firstName=firstName;) be in the
MemberValidateForm() method instead of the validate
method?
thanks again
package com.bdi.www.members;
import java.util.*;
public class MemberValidateForm {
private String firstName;
private String lastName;
private String emailAddress;
private String homePhone;
private Map errors;
public boolean validate(String firstName,String
lastName,String emailAddress,String homePhone) {
this.firstName=firstName;
this.lastName=lastName;
this.emailAddress=emailAddress;
this.homePhone=homePhone;
boolean allOk=true;
if (firstName.equals("")) {
errors.put("firstName","Please enter your first
name");
firstName="";
allOk=false;
}
if (lastName.equals("")) {
errors.put("lastName","Please enter your last
name");
lastName="";
allOk=false;
}
if (emailAddress.equals("") ||
(emailAddress.indexOf('@') == -1)) {
errors.put("emailAddress","Please enter a valid
email address");
emailAddress="";
allOk=false;
}
if (homePhone.equals("")) {
errors.put("homePhone","Please enter your phone
number");
homePhone="";
allOk=false;
}
return allOk;
}
public MemberValidateForm() {
firstName="";
lastName="";
emailAddress="";
homePhone="";
errors = new HashMap();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public String getHomePhone() {
return homePhone;
}
public void setFirstName(String firstName) {
this.firstName =firstName;
}
public void setLastName(String lastName) {
this.lastName =lastName;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress=emailAddress;
}
public void setHomePhone(String homePhone) {
this.homePhone=homePhone;
}
public Map getErrors() {
return errors;
}
}
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
|