The getParameter method makes it easy to read incoming request parameters: you use the same method from doGet as from doPost, and the value returned is automatically URL-decoded. Since the return value of getParameter is String, however, you have to parse the value yourself.
Now, in JSP, you can use the JavaBeans component architecture to greatly simplify the process of reading request parameters, parsing the values, and storing the results in Java objects.
The Apache Jakarta Commons package contains classes that make it easy to build a utility to automatically associate request parameters with bean properties. BeanUtilities.populateBean(Object formBean, HttpServletRequest request); as follows,
InsuranceInfo info = new InsuranceInfo();
BeanUtilities.populateBean(info, request);
public class InsuranceInfo {
private String name = "No name specified";
private String employeeID = "No ID specified";
private int numChildren = 0;
private boolean isMarried = false;
public String getName() {
return(name);
}
/** Just in case user enters special HTML characters,
* filter them out before storing the name.
*/
public void setName(String name) {
this.name = ServletUtilities.filter(name);
}
public void setNumChildren(int numChildren) {
this.numChildren = numChildren;
}
...
Comments
Post a Comment