Jsp/servlet automatically populates Java objects with request parameters: Form Bean

Source: Internet
Author: User

(1) The problem arises:


For the sake of simplicity we need a technique that can submit the form data all at once, that is to say: the Bean form

(2) The Beanutilities code is as follows:

Package Com.lc.ch04biaodanshuju;import Java.util.*;import Javax.servlet.http.*;import Org.apache.commons.beanutils.beanutils;public class Beanutilities {public    static void Populatebean (Object Formbean,                                  httpservletrequest request) {    Populatebean (Formbean, Request.getparametermap ());  }    public static void Populatebean (Object bean,                                  Map propertymap) {    try {      beanutils.populate (bean, PropertyMap );    } catch (Exception e) {      //Empty catch. The possible exceptions is      //java.lang.IllegalAccessException and/      / Java.lang.reflect.InvocationTargetException.      In both cases, just skip the bean operation.}}}  


(3) A servlet that collects insurance employee insurance information is used to determine a workable insurance plan and related costs.

Package Com.lc.ch04biaodanshuju;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Submitinsuranceinfo extends HttpServlet {public void doget (HttpServletRequest request, Httpservletres    Ponse response) throws Servletexception, IOException {insuranceinfo info = new Insuranceinfo ();    Beanutilities.populatebean (info, request);    Response.setcontenttype ("text/html");    PrintWriter out = Response.getwriter (); String DocType = "<!    DOCTYPE HTML public \ "-//W3C//DTD HTML 4.0" + "transitional//en\" >\n ";    String title = "Insurance Info for" + info.getname (); Out.println (DocType + "

(4) JavaBean of connected database


Package Com.lc.ch04biaodanshuju;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);  }   public void SetName (String name) {    this.name = servletutilities.filter (name);  }  Public String Getemployeeid () {    return (EmployeeID);  }    public void Setemployeeid (String employeeID) {    This.employeeid = Servletutilities.filter (EmployeeID);  }  public int Getnumchildren () {    return (Numchildren);  }  public void Setnumchildren (int numchildren) {    this.numchildren = Numchildren;  }    public Boolean ismarried () {    return (ismarried);  }  public void Setmarried (Boolean ismarried) {    this.ismarried = ismarried;  }}


(5)

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

(6) Jar package acquisition and use: we want to use the tool class must learn the jar package,: http://download.csdn.net/detail/u010870518/7867181 don't forget to add the configuration!







Jsp/servlet automatically populates Java objects with request parameters: Form Bean

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.