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.
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.
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.
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;
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());
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.
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; }
}
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
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 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.
double interest = 10000.0 * myBean.getRate();
Such a statement will only work if myBean.getRate()
returns a double.
String result = request.getParameter("rate").substring(0,1);
double[] numbers = myBean.getNumbers(); double total = 0.0; if (numbers != null) { for(int i=0; i < numbers.length; i++) {
total = total + numbers[i]; } }
String[] stringNumbers = request.getParameterValues("numbers"); String result = ""; if (stringNumbers != null) { for(int i=0; i < stringNumbers.length; i++) {
result = result + stringNumbers[i].substring(0,1); } }
No, it is possible to populate a bean wihtout using populateBean.
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, ........