Advertisment

Use JavaBeans with JSP

author-image
PCQ Bureau
New Update

Using JavaBeans to process your data has the advantage of separating the processing part from the presentation part. This gives you clean and easily maintainable code in large programs like those used for e-commerce and weather reports; and is also helpful in situations where the programmer is separate from the Web designer, as it becomes easy for both to do their jobs independently. Here, we’ll use last month’s (see HTML Forms with JSP, page 143,

PCQuest June 2001) FormJSP and move all the code written for it into JavaBeans (see box ‘What is a JavaBean’).

Advertisment

Let’s first run our JSP called BeanJSP, which uses a JavaBean called DatabaseBean to connect to the database and query or update it. Copy the file BeanJSP.jsp and product.html from the cdrom/sorc_cod/ jsp directory to /home/httpd/html/ jsp directory. Also create a subdirectory named pcquest under the jsp directory. Next, copy the file DatabaseBean.java from the CD to /opt directory. Change the password variable in DatabaseBean.java to your MySQL password. Compile DatabaseBean.java using the javac compiler and copy the file named DatabaseBean. class–created after compilation–to the pcquest directory. 

We must now add the directory /home/httpd/ html/jsp to the CLASSPATH variable so that Tomcat can locate the DatabaseBean.class file. Issue the following command for this.

export CLASSPATH=$CLASSPATH:/

home/httpd/html/jsp

Advertisment

You may want to append this line to the file named profile in /etc directory to make the setting persistent on subsequent reboots. Now stop and start Tomcat using the shutdown.sh and startup.sh scripts respectively. Open the file product.html in a text editor and change the name of the JSP file from FormJSP.jsp to BeanJSP.jsp in the form’s Action attribute. Now, to see the JSP working, key in the following URL in Netscape or Lynx Web browser:

http://127.0.0.1:8080/jsp/product.html

There is no change in the working or the display of the JSP. What we’ve done is moved all the processing code into the

JavaBean. 

Advertisment

Let’s take a detailed tour through the code for

DatabaseBean. Open the file DatabaseBean.java in a text editor to see the code. With the statement,

package pcquest;

we’ve declared that the DatabaseBean class belongs to the package pcquest. This is why we’ve placed it in a subdirectory pcquest under the directory jsp–a directory included in the CLASSPATH. The instance variables username, password, product_id, product_name and product_price are used for the same purpose as in the JSP. Then, adhering to the JavaBean specification, we’ve provided a default constructor, DatabaseBean( ). The statements in the method updateDatabase( ) will be familiar to you from last month’s article (see HTML Forms with JSP, page 143, PCQuest June 2001), with one difference. Instead of displaying the success message, ‘The database has been updated’, or the error message, ‘in case of an error’, this method returns the corresponding string when it is called by the JSP. This will become clearer as we go through the code of BeanJSP. Again adhering to the JavaBean specification we provide a get and set method for all the instance variables, though we will use only the set method. The username and password instance variables are declared as final, or constants. So we don’t have corresponding get and set methods for them. 

Advertisment

Now let’s parse through the code of BeanJSP in BeanJSP.jsp file. The line

uses the tag to instantiate (create an object of the class) DatabaseBean. With id attribute of the tag we give a name, dbBean, to the DatabaseBean object. This object can subsequently be referred by this name. The class file corresponding to the DatabaseBean is specified by 



the class attribute. Through the statements

Advertisment





’ />


’ />


’ />

we call the corresponding set methods of the instance variables, product_id, product_name, and product_price. For this we are using the tag. This tag has the following syntax.

” property=”” value= “” />

Advertisment

These statements will work only if we have a corresponding set method for each instance variable, specified by the property attribute of the JavaBean. With value parameter, we supply the text keyed in the text fields of the HTML form. This is done by calling the getParameter( ) method of the request object, as before. We’ve enclosed the method call within the JSP expression tags, so that the method call gets evaluated and the return value is assigned to the value attribute. Note that we have used single quotes (‘) to surround the expression tags. If we had used double quotes (“), it would conflict with the double quotes used to specify the argument–id, name, price–of the getParameter( ) method. Next, within the JSP scriptlet tags, we call the method updateDatabase( ) of the DatabaseBean as:

String result = dbBean.updateDatabase( );

The return value of updateDatabase( ) method, a string representing the success or error message, is stored in the variable named result, which is subsequently displayed on the Web page using the out.println( ) method.

Advertisment

Magic of Beans

The three statements corresponding to tag in the BeanJSP can be substituted with a single, simple statement as:







For this to work, however, all the form text fields in product.html must have the same name as the corresponding instance variables in the DatabaseBean or vice versa. So if you replace values given for the ‘name’ attribute in product.html from id, name, and price to product_id, product_name, and product_ price respectively, the JSP will be able to extract the text keyed in and assign them to the corresponding instance variables in the DatabaseBean using the set methods.

Shekhar Govindarajan

Advertisment