Jsp/Servlet automatically fills in Java objects based on request parameters: Form Bean and servletbean

Source: Internet
Author: User

Jsp/Servlet automatically fills in Java objects based on request parameters: Form Bean and servletbean

(1) Question:


For simplicity and convenience, we need a technology that can submit all form data at a time, that is, 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 two possible exceptions are      // java.lang.IllegalAccessException and      // java.lang.reflect.InvocationTargetException.      // In both cases, just skip the bean operation.    }  }}


(3) Use a servlet that collects Insurance Employee insurance information to determine feasible insurance plans and related fees.

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,                    HttpServletResponse 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 +                "<HTML>\n" +                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +                "<BODY BGCOLOR=\"#FDF5E6\">\n" +                "<CENTER>\n" +                "<H1>" + title + "</H1>\n" +                "<UL>\n" +                "  <LI>Employee ID: " +                   info.getEmployeeID() + "\n" +                "  <LI>Number of children: " +                   info.getNumChildren() + "\n" +                "  <LI>Married?: " +                   info.isMarried() + "\n" +                "</UL></CENTER></BODY></HTML>");  }}

(4) connect to the database's javabean


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"><HTML><HEAD><TITLE>Employee Insurance Signup</TITLE></HEAD><BODY BGCOLOR="#FDF5E6"><CENTER><H1>Employee Insurance Signup</H1><FORM ACTION="/servlet/coreservlets.SubmitInsuranceInfo">  Name:  <INPUT TYPE="TEXT" NAME="name"><BR>  Employee ID: <INPUT TYPE="TEXT" NAME="employeeID"><BR>  Number of Children:  <INPUT TYPE="TEXT" NAME="numChildren"><BR>  <INPUT TYPE="CHECKBOX" NAME="married" VALUE="true">Married?<BR>  <CENTER><INPUT TYPE="SUBMIT"></CENTER></FORM></CENTER></BODY></HTML>

(6) jar package acquisition and use: we want to use tools must learn jar package,: http://download.csdn.net/detail/u010870518/7867181 do not forget to add configuration!








How to obtain the servlet's javabean that stores form parameters in the jsp page

Javabean is compiled by yourself. In the servlet, obtain the parameters of the jsp page, and then call the method to encapsulate the parameters in the javabean.

Jsp creates a javabean object in the servlet and Passes parameters to it. Now, the servlet is called through the jsp page.

In theory, it is gone. Because we will use the. setAttribute () method to pass values between two pages and place the value to be saved in it. If the value is still present, we don't have to do this. So theoretically, it is no longer. I do not understand the specific principle.

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.