Populating Beans in Servlets

Getting the jar files

If you are running your servlet from the Tomcat server on ocelot, then all the necessary .jar files for populating beans are already on the server.

Developing Locally

If you are developing locally, then it is necessary to place the following three .jar files in the lib folder of the WEB-INF folder of the web application that will use beans. If the lib directory does not exist, then create it. The lib directory is a sibling of the classes directory. You can obtain the jar files by following these links to the textbook's web site.

Running a Stand-alone Tomcat Locally

There is the possibility of an error when you start a stand-alone Tomcat locally. Look in the logs directory and open the localhost log file. There will be several, pick the one with the current date. Scroll to the bottom and be sure that there is not an error regarding one of the .jar files. The error would look like this:

2003-10-23 08:34:13 WebappLoader[/cgs4854]: Reloading checks are enabled for this Context
2003-10-23 08:34:13 ContextConfig[/cgs4854] Exception processing JAR at resource path /WEB-INF/lib/commons-beanutils.jar
javax.servlet.ServletException: Exception processing JAR at resource path /WEB-INF/lib/commons-beanutils.jar
        at org.apache.catalina.startup.ContextConfig.tldScanJar(ContextConfig.java:930)
        at org.apache.catalina.startup.ContextConfig.tldScan(ContextConfig.java:868)
        at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:647)
 

The problem is that a certain temp directory does not exist. Find the folder for Tomcat that has the conf, logs, and webapps folders. Add another folder to this one named temp.

Importing the package

If you are using NetBeans, then you only have to place the jar files in the lib directory. If you are using JCreator or another IDE, you will have to add all three jar files to your Classpath variable. For more information on adding jar files in JCreator, refer to Customizing JCreator for Servlets.

If you are using the BeanUtilites class from the coreservlets package, then you only have to import
import coreservlets.beans.*;

If are not using the coreserlvets package, then add the following import statement to the .java file that will use populateBean from the the BeanUtils package.
import org.apache.commons.beanutils.BeanUtils;

Populating the Bean from a Servlet

In order to populate the bean, first create the bean, then call the populate method. Request is the HttpServletRequest object that the servlet is passed in doGet and doPost.

If you are using BeanUtilites from coreservlets, call the populate method with the bean and the request.
BeanExample myBean = new BeanExample();
BeanUtilities.populateBean(myBean, request);

If you are not ising coreservlets, then call the populate method with the bean and the Map of request parameters.
BeanExample myBean = new BeanExample();
BeanUtils.populateBean( myBean, request.getParameterMap());

Creating a Bean that Agrees with a Form in a Servlet

The idea of a Bean is to create a Bean Property for each form element in the servlet. A Bean Property is created by defining certain methods that are used to access a member variable in the Bean class. We will use two types of Bean Properties.

  1. A Single Value Property of a standard type: int, double, String. For this Bean Property, define get and set methods that agree with the name of the form element. A single value propery is used in text fields, hidden fields, password fields, textareas, radio groups, and single selection lists. For example, if you have a form element with the name first that should be a String, then add the following to the Bean
        private String first;
    public void setFirst(String first) {
    this.first = first;
    } public String getFirst() {
    return this.first; }

    If the type of the variable should be int, then replace String in the above code with int. If the type of the variable should be double, then replace String in the above code with double.

    There is an annoying problem with request.getParameter() : if the parameter is null, and it is printed, then the string "null" appears in the output. This problem can be fixed by modifying the set method in the Bean as follows

        public void setFirst(String first) {
    	if (first == null) {
    	    this.first = "";
            } else {
    this.first = first; }
    }
  2. An Array Property of a standard type: int, double, String. For this Bean Property, define two get and two set methods that agree with the name of the form element. An array property will be used with checkbox groups and multiple selection lists. For example, if you have a multiple selection list named team in your form that contains Strings, then add the following to the Bean
        private String[] team;
    public String[] getTeam() { return(team); } public void setTeam(String[] team) { this.team = team; } public String getTeam(int i) { return(team[i]); } public void setTeam(String team, int i) { this.team[i] = team; }

    If the type of the variable should be int, then replace String in the above code with int. If the type of the variable should be double, then replace String in the above code with double.

    Here is an example of a bean that has the above properties: BeanExample.java

Identifying the Properites

If your Bean only has Single Value properties, then you do not need to do anything else in order to have the populateBean method retrieve the data from the request and fill the member variables of the Bean. The advantage of using Beans is that the String input parameters will be translated into the appropriate types in the Bean.

If your Bean has an Array Property, then it is necessary to identify all the properties that your Bean has. This is done by creating a BeanInfo class. For example, if the Bean is defined in a class named BeanData, then the name of the BeanInfo class must be BeanDataBeanInfo.

Here is an example of a BeanInfo class for the Bean described above: BeanExampleBeanInfo.java

Now what?

Now you have two ways of accessing the data from your form. For String properties, then the big advantage is avoiding the word "null" from appearing in your form.

For int and double properties, there is a huge advantage: the data conversion from String to int or double is automatic.

Is populateBean necessary?

No, it is possible to populate a bean wihtout using populateBean.

Are Beans necessary?

No, Beans are not necessary, they are just convenient. It is possible to convert a String parameter to an int or a double directly. Here is some code that will change a String into an int

    int age;
try { age = Integer.parseInt(request.getParameter("age")); } catch (NumberFormatException nfe) { age = 0; }

This is what Beans do. They automatically convert a String to an int or a double. If the String is not a valid int or double, then a 0 is assigned.

It is also possible to place additional information in the Bean. For an example, look at the information on Sticky Forms using Beans. There is no limit to what can be placed in a Bean: error information, CSS class information, ........