Accessing Databases


Using Beans

Once you become proficient at developing JSP pages, you'll realize that it would be helpful to encapsulate some of the data. Resin and the JSP specification provide this ability through the use of JavaBeans. Beans can be used to perform some specific functionality or handle the data from an HTML form. The Resin server is friendly to JavaBeans and will automatically compile them if they're placed in the WEB-INF/classes directory of the application directory. Throughout this chapter, we have been using the application directory called /hello. This means we need to create a WEB-INF/classes directory under the /hello directory and place all of our bean code in it. All of the JSP tags for beans are shown in Table 3.4.

Table 3.4: JSP Tags for Our Beans

TAG NAME

PURPOSE

<jsp:setProperty ... param="param"/>

Sets a bean property to a parameter value; requires a set method.

<jsp:setProperty ... value="value"/>

Sets a bean property to value; requires a set method.

<jsp:useBean ...>...

Creates a new bean and variable for the page.

<jsp:getProperty name="name" ... />

Prints a property defined in the bean; requires a get method.

Let's look at building a JavaBean that will keep track of the total visits to our Web site and display the count as needed. First, we write the JavaBean code:

 public class Counter {   private int hits;   public int getHit()   {     return hits;   }   public void setHit(int i) {     ++hits;     //save in database or file   } } 

The JavaBean code just looks like any other class you might write in Java; however, you must follow a specific format in order to use some of the JSP bean tags. Any property or attribute in the bean needs to have a set and get method if the JSP code will be setting and getting the properties value. The Resin server will automatically call the appropriate set and get methods depending on the property specified. We see this action in the next code snippet.

At this point, place the previous code in the /hello/WEB-INF/classes/Counter. java file. The Resin server automatically compiles it after it detects the new file. Now let's write code to access the JavaBean:

 <jsp:useBean id='counter' scope='application' class='Counter'/> <HTML> <BODY> Please enter your Name: <form method='post' action='name.jsp'> <input type='text' name='username' size='35'><BR> <input type='submit'> </form> <BR> <jsp:setProperty name="counter" property="hit" value='1'/> Total Visits: <jsp:getProperty name="counter" property="hit"/> </BODY> <HTML> 

Place this code in a file called login.jsp. Notice this code contains several new tags. The first is the <jsp:useBean> tag. This tag tells the Resin server that our JSP page will use a bean and that if the bean hasn't been previously created for the specified scope, it needs to be instantiated. The bean is specified by the class attribute of the tag. In this code, you tell the server the bean is called Counter and that it should be found in a file called Counter.java. The scope of the bean is specified by the scope attribute, which has a value of application. This means the bean should be created and placed in the Application object to be used by all the code in the application. Other possible values for the scope attribute are session and page. These two values further narrow the scope of a bean. Once the bean is instantiated, you are able to identify it through the code using the value assigned to the id attribute.

The HTML displays a username/password form and then performs two operations on our Counter bean. The first is found in the <jsp:setProperty> tag. In this tag, you tell the server that it should call the set method of the hit property of our Counter bean. The bean is identified by the name attribute and the property by the property attribute. The setProperty tag requires a value attribute, which is passed to the properties set method.

After calling the setHit() method, our code executes the <jsp:getProperty> tag. This tag obtains the value of a bean's property using the property's get method. To test the bean, browse to the URL http://localhost: 8080/hello/login.jsp. In most cases, you will get an error on the browser saying the Counter bean isn't available. Check the /WEB-INF/classes directory and you find there is no CLASS file in the directory. This means the Resin server didn't compile the bean source file. This is because the Resin server has to know about the code you need compiled.

In order to get Resin to process the bean code, you must add an entry to the Resin configuration file letting the server know there is a new application being hosted by the server. An application is simply a directory with JSP, HTML, and bean files in it. The entry you need to add to the Resin.conf file is:

 <web-app id='hello'/> 

If the server is running, the change to the configuration file is automatically processed and the bean code is compiled. Try to browse to the application URL given earlier. You should see a form that displays Total Visits: 0 at the bottom of the page. Hit the reload button a few times and you will see the count increment.

If you look at the bean code, you find that the set method for the hits attribute doesn't take advantage of the parameter value passed to it. Unfortunately this is how the JavaBean code works for the <jsp:setProperty> tag; a value is required. However, this doesn't mean you can't add your own methods to the bean. Consider the following code with an incHit() method added:

 public class Counter {   private int hits;   public int getHit() {     return hits;   }   public synchronized void incHit() {     hits++;   } } 

The new method accurately increments the hits property regardless of the number of simultaneous browses to the Web page. Now let's change the <jsp:setProperty> tag to a call on the new method. Consider the following:

 <% counter.incHit(); %> Total Visits: <jsp:getProperty name="counter" property="hit"/> 

Here you add the appropriate JSP tags that allow you to execute Java code. You use the identity name of your bean and call the incHit() method directory. Change the login.jsp page and bean code to use the new method and then browse to the login.jsp page again. You will see the counter start at 0 again and increment as you reload the page. Obviously, you need to save the counter variable to either a file or a database row in order to keep the counter value persistent.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net