Java Server Pages and Java Beans - Pace University



Java Server Pages and Java Beans

Java server pages (JSP) and Java beans[1] work together to create a web application. Java server pages are html pages that also contain regular Java code. This code is included between special tags that begin with ‘

The name, hello, refers to the bean. These say to set the bean properties, name and email. The property names must be the same as those in the bean, and the parameter names must agree exactly with those in the HTML file.

The rest of the JSP file just echoes the data back to the browser. It supplies the HTML code for the output page.

Hello JSP

The result looks like the following in the browser.

[pic]

Finally the bean for this example is very simple. It just stores the data using its mutator methods and returns it using the accessor methods. It does not have a constructor or any methods other than the gets and sets. A more realistic example would do something with the data before returning it.

public class HelloBean

{

private String name = "";

private String email = "";

public String getName() {return name;}

public String getEmail() {return email;}

public void setName (String n) {name = n;}

public void setEmail (String e) {email = e;}

} // HelloBean

Naming for the variables and get and set methods is determined by rules for JSP and cannot be changed. The variables must all begin with lower case letters. In the accessor and mutator methods, the get/set must be followed by an upper case letter, as in the example. If the variable contains upper case letters further on, they are to be included as is. For example, if the variable was called eMail, the accessor method for it would be getEMail (). Similarly if a variable is called firstName, the accessor method would be getFirstName (). Not following this convention is a common source of error.

Example for Finding an Address

A somewhat more realistic example uses the name in the form to find the address in a database. The form is now even simpler, since it only contains the name.

Find Address

Enter the name :

Name

The JSP file, on the other hand, is more complicated. The line

is similar to the one for the hello example. However, the line

is not. It provides a shorthand method for storing data in the bean’s instance variables. By using property="*", all the data in the HTML form is sent directly to the bean. If you use this, be very careful that the parameters in the HTML form are exactly the same as the instance variables in the bean. Case here is important. If you have name="Name" in the form, but String name; in the bean, the parameter will not be stored in the bean properly.[2]

The if-else statement is also a problem. The Java code must be carefully separated from the HTML code. Getting all the tags in the right place is tricky. All Java code blocks must be included in curly braces ({}) whether or not this is required by Java. Look carefully at the example below to see how they should be arranged.

Find Address JSP

The requested address:

The name was not in the database.

If the name is in the database, the output of the JSP file looks like that below.

[pic]

Next is the code for the bean, FindBean.java. It contains a method called processRequest () that connects to the database and finds the address. This part is the same as with the similar servlet.

package address_book;

import java.sql.*;

// FindBean is a Java bean that is used to locate an address in a database.

public class FindBean

{

private String name, email, telephone;

private boolean found;

// The accessor methods.

public String getName() {return name;}

public String getEmail () {return email;}

public String getTelephone () {return telephone;}

public boolean getFound () {return found;}

// The only mutator method needed.

public void setName (String n) {name = n;}

/* processRequest connects to the database, gets a ResultSet, and stores the data in the instance variables. */

public void processRequest ()

{

try

{

// Get a jdbc-odbc bridge and connect to addresses.mdb.

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection ("jdbc:odbc:addresses");

// Create a query and get a ResultSet.

Statement stmt = con.createStatement ();

String query = "Select * From AddressTable Where Name = '" + name + "'";

ResultSet rs = stmt.executeQuery (query);

// If the name is in the database, store the address in the instance variables.

if (rs.next ())

{

name = rs.getString ("Name");

email = rs.getString ("Email");

telephone = rs.getString ("Telephone");

found = true;

}

// If the address was not found, set the value of the variable found to false.

else found = false;

} catch (ClassNotFoundException e){System.out.println ("Class Not Found Exception.\n");}

catch (SQLException e){System.out.println ("SQL Exception");}

} // processRequest

} // FindBean

The servlet derived from the JSP file, find.jsp, and its compiled version, are stored in work/Catalina/localhost/org/apache/jsp. They are find_jsp.java and find_jsp.class. We can include them in the application by copying the org/apache/jsp folder to the classes folder.

[pic]

The files in this folder are either servlets or class files. They can now be included in the web application deployment descriptor, web.xml. The lines to add are:

org.apache.jsp.find_jsp

org.apache.jsp.find_jsp

and

org.apache.jsp.find_jsp

/find/*

The mapping definition, /find/*, can now be used in the index page in the usual way. The following form asks for a name and sends the data to the server. The servlet, find_jsp, then executes and returns a response to the browser.

Enter a name to find an email address.

Name

Grocery Store Database

A different example is that of a grocery store. To begin with, the store stocks only a few kinds of fruit. A table is shown below.

The table is called fruit, and it has four fields, id, name, quantity, and price. Id and name are both strings, quantity is an integer, and price is a double.

It is stored in a database called grocery. There can also be tables for vegetables, dairy, customers, and employees. We will see some of these other tables later.

We can make changes in the database using a SQL update statement. If we want to change both the quantity and the price for some fruit, we can use the following SQL statement.

String update = "Update fruit Set quantity = " + quantity

+ ", price = " + price + " Where id = '" + id + "'";

The variables, quantity and price, are numeric, so they are not surrounded by quotation marks. However, id is a string, so it has to have the single quotes inside of the double quotes.

The general form[3] of the update statement is

"Update table Set Field1 = parameter1, Field2 = parameter2 Where Key = key"

An HTML file that can be used to get the data follows. A more complete form would ask the client to confirm the new data.

Grocery Store

Change Quantity and Price

Product ID

New Quantity

New Price

The JSP file is a lot like the one for finding an address.

Change Quantity and Price JSP.

0)

{ %>

The changed values are:

Id:

Name:

Quantity:

Price:

The Id was not in the database.

The Java bean first connects to the database and then updates the data. If the update is successful, the method, executeUpdate, will return a value greater than 0. If the update fails, the value will be 0.

package grocery;

import java.sql.*;

import java.io.*;

// ChangeBean finds a specific product and changes the quantity and price.

public class ChangeBean

{

private String id, name;

private int quantity, success;

private double price;

// The accessor methods.

public String getId() {return id;}

public String getName() {return name;}

public int getQuantity() {return quantity;}

public double getPrice() {return price;}

public int getSuccess () {return success;}

// The mutator methods.

public void setId (String i) {id = i;}

public void setQuantity (int q) {quantity = q;}

public void setPrice (double p) {price = p;}

// processRequest connects to the database and them executes the update.

public void processRequest ()

{

try

{

// Get a jdbc-odbc bridge and connect to the grocery database.

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection ("jdbc:odbc:grocery");

// Create an update statement. If the update succeeds, the value of success will be positive.

Statement stmt = con.createStatement ();

String update = "Update fruit Set quantity = " + quantity

+ ", price = " + price + " Where id = '" + id + "'";

success = stmt.executeUpdate (update);

/* If the update is successful, get the data from the database and store it in the instance variables. */

if (success > 0)

{

stmt = con.createStatement ();

String query = "Select * From fruit Where ID = '" + id + "'";

ResultSet rs = stmt.executeQuery (query);

rs.next ();

id = rs.getString ("id");

name = rs.getString ("name");

quantity = rs.getInt ("quantity");

price = rs.getDouble ("price");

}

stmt.close ();

} catch (ClassNotFoundException e){System.out.println ("Class Not Found exception.");}

catch (SQLException e){System.out.println ("SQL Exception");}

}

} // class ChangeBean

References

1. Susan Anderson-Freed, Weaving a Website, Prentice Hall, 2002.

2. H.M. Deitel, P.J. Deitel, and T.R. Nieto, Internet & World Wide Web, How to Program, 2nd Edition, Prentice Hall, 2002.

3. Marty Hall & Larry Brown, Core Servlets and Java Server Pages, First Edition, Sun Microsystems Press/Prentice-Hall PTR Book, 2003.

4. Elliotte Rusty Harold, Java Network Programming, O’Reilly & Associates, Inc., 2000.

5. Karl Moss, Java Servlets Developer’s Guide, McGraw-Hill/Osborne, 2002.

6. Dave Raggett , A History of HTML, Chapter 2, Addison Wesley Longman, 1998, .

7. W3Schools Online Web Tutorials, .

-----------------------

[1] For more information about JSP and Java beans see Marty Hall & Larry Brown, Core Servlets and Java Server Pages, First Edition, Sun Microsystems Press/Prentice-Hall PTR Book, 2003.

[2] Case difference between the form, the JSP file, and the bean are one of the most common sources of error. The best thing to do is to keep the identifiers the same in all three places.

[3] See W3Schools at for more information on SQL.

-----------------------

[pic]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download