|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
When developing beans for processing form data, you can follow a common design pattern by matching the names of the bean properties
with the names of the form input elements. You would also need to define the corresponding getter/setter methods for each
property within the bean. For example, within the FormBean bean (shown in Listing 3), I define the property firstName, as well as the accessor methods getFirstName() and setFirstName(), corresponding to the form input element named firstName. The advantage in this is that you can now direct the JSP engine to parse all the incoming values from the HTML form elements
that are part of the request object, then assign them to their corresponding bean properties with a single statement, like
this:
<jsp:setProperty name="formHandler" property="*"/>
This runtime magic is possible through a process called introspection, which lets a class expose its properties on request. The introspection is managed by the JSP engine, and implemented via the Java reflection mechanism. This feature alone can be a lifesaver when processing complex forms containing a significant number of input elements.
package foo;
import java.util.*;
public class FormBean {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password1;
private String password2;
private String zip;
private String[] faveMusic;
private String notify;
private Hashtable errors;
public boolean validate() {
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 (email.equals("") || (email.indexOf('@') == -1)) {
errors.put("email","Please enter a valid email address");
email="";
allOk=false;
}
if (userName.equals("")) {
errors.put("userName","Please enter a username");
userName="";
allOk=false;
}
if (password1.equals("") ) {
errors.put("password1","Please enter a valid password");
password1="";
allOk=false;
}
if (!password1.equals("") && (password2.equals("") ||
!password1.equals(password2))) {
errors.put("password2","Please confirm your password");
password2="";
allOk=false;
}
if (zip.equals("") || zip.length() !=5 ) {
errors.put("zip","Please enter a valid zip code");
zip="";
allOk=false;
} else {
try {
int x = Integer.parseInt(zip);
} catch (NumberFormatException e) {
errors.put("zip","Please enter a valid zip code");
zip="";
allOk=false;
}
}
return allOk;
}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public FormBean() {
firstName="";
lastName="";
email="";
userName="";
password1="";
password2="";
zip="";
faveMusic = new String[] { "1" };
notify="";
errors = new Hashtable();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getUserName() {
return userName;
}
public String getPassword1() {
return password1;
}
public String getPassword2() {
return password2;
}
public String getZip() {
return zip;
}
public String getNotify() {
return notify;
}
public String[] getFaveMusic() {
return faveMusic;
}
public String isCbSelected(String s) {
boolean found=false;
if (!faveMusic[0].equals("1")) {
for (int i = 0; i < faveMusic.length; i++) {
if (faveMusic[i].equals(s)) {
found=true;
break;
}
}
if (found) return "checked";
}
return "";
}
public String isRbSelected(String s) {
return (notify.equals(s))? "checked" : "";
}
public void setFirstName(String fname) {
firstName =fname;
}
public void setLastName(String lname) {
lastName =lname;
}
public void setEmail(String eml) {
email=eml;
}
public void setUserName(String u) {
userName=u;
}
public void setPassword1(String p1) {
password1=p1;
}
public void setPassword2(String p2) {
password2=p2;
}
public void setZip(String z) {
zip=z;
}
public void setFaveMusic(String[] music) {
faveMusic=music;
}
public void setErrors(String key, String msg) {
errors.put(key,msg);
}
public void setNotify(String n) {
notify=n;
}
}
It is not mandatory that the bean property names always match those of the form input elements, since you can always perform the mapping yourself, like this:
<jsp:setProperty name="formHandler" param="formElementName"
property="beanPropName"/>
While you can easily map form input types like text-entry fields and radio buttons to a scalar bean property type like String, you have to use an indexed property type like String[] for handling checkboxes. Nevertheless, as long as you provide a suitable setter method you can still have the JSP engine
assign values even for non-scalar types by automatically parsing the corresponding form element values from the request object.
Observe the setter method for setting the values of selected checkboxes:
public void setFaveMusic(String[] music) {
faveMusic=music;
}
I've also shown a simple technique to avoid hardcoding the target resources within the controller. Here, the targets are stored
within an external properties file, forms.properties, as shown in Listing 4. This file may be located anywhere in the CLASSPATH that is visible to the JSP container. The jspInit() method declared within the page is automatically executed by the JSP container when the JSP page (or to be more accurate,
the servlet class representing the page) is loaded into memory, and invoked just once during the JSP's lifetime. By making
use of the ResourceBundle facility, your page can access the values for the target resources by name, just like you would with a Hashtable object.
process.success=success.jsp process.retry=retry.jsp
Server-side Java: Read the whole series -archived on JavaWorld