- 111 - <%=counter.getCount() %></center> </body> </html> You can see that the only difference between these two JSPs is the values of the HTML <title> and <h3> tags. To see how a session bean works, copy both of these JSPs to the <SERVER_ROOT>/purejsp/ directory and open your browser to the following URL: http://yourserver:8080/purejsp/SessionBean1.jsp You should see an image similar to Figure 11.3 . Go ahead and hit your reload button several times. You should see the count increase each time the page is reloaded. Now use the same browser instance to open the following URL: http://yourserver:8080/purejsp/SessionBean2.jsp You will see the count increment from the last count from the first JSP. This is because the Counter bean is stored in the session of the client. Now open a completely new instance of the browser and you will see the value of the count property is reset. This is because each instance of a client creates its own instance of the HttpSession, which is where the Counter bean is stored. Figure 11.3: The output from SessionBean1.jsp. application Beans with application scope are accessible within pages processing requests that are in the same application space as the page in which they were created. References to the object will be released when the runtime environment reclaims the ServletContext. More simply put, this means that, until the JSP engine is restarted, the bean created with application scope will be available. Beans with application scope are best used when you need to share information between JSPs and servlets for the life of your application. To give an example of application scope, we are going to use two JSPs. The first will load the Counter bean using an id of counter and a scope of application. It will then print out the current value of the Counter bean, using the Counter.getCount() method. Listing 11.7 contains the source for our first JSP. Listing 11.7: ApplicationBean1.jsp <%@ page errorPage="errorpage.jsp" %> <! Instantiate the Counter bean with an id of "counter" > - 112 - <jsp:useBean id="counter" scope="application" class="Counter" /> <html> <head> <title>Application Bean Example 1</title> </head> <body> <H3>Application Bean Example 1</H3> <center><b>The current count for the counter bean is: </b> <%=counter.getCount() %></center> </body> </html> Our second JSP does exactly as the first except that, because both beans have an id of counter and application scope, it will find the bean and not have to create it. Listing 11.8 contains the source for our second JSP. Listing 11.8: ApplicationBean2.jsp <%@ page errorPage="errorpage.jsp" %> <! Instantiate the Counter bean with an id of "counter" > <jsp:useBean id="counter" scope="application" class="Counter" /> <html> <head> <title>Application Bean Example 2</title> </head> <body> <H3>Application Bean Example 2</H3> <center><b>The current count for the counter bean is: </b> <%=counter.getCount() %></center> </body> </html> - 113 - Go ahead and copy both of these JSPs to the <SERVER_ROOT>/purejsp/ directory and open your browser to the following URL: http://yourserver:8080/purejsp/ApplicationBean1.jsp Hit the reload button a few times and watch the count go up. You will see a page similar to Figure 11.4 . Figure 11.4: The output from ApplicationBean1.jsp. Now open another browser window to the second JSP using the following URL: http://yourserver:8080/purejsp/ApplicationBean2.jsp Hit the reload button a few times and watch the count go up. You will see, as each page increments the other page's instance, that both of these pages share the same instance of the Counter bean. They will share this instance until the JSP engine is shut down. Summary In this chapter, we covered how JSP beans are scoped. You should feel comfortable with the different types of JSP scope. You should also understand how the life of a JSP bean is determined by its scope. In Chapter 12 , we cover how to integrate JSPs and HTML forms. Chapter 12: JSP and HTML Forms Overview In this chapter we are going to take a look at HTML forms. We are also going to look at how JSPs handle requests from HTML forms. What Is an HTML Form? An HTML form is one of the most common ways to gather information from a Web client. It is made up of two basic parts: the <form> tag and the <input> tags. Both of these elements are described below. The <form> Tag The <form> tag is what defines the form. It tells the HTML browser how to display the form and how the browser should submit the form. The basic syntax of an HTML <form> tag is listed below: <form method="GET|POST" action="URL"> </form> The <form> tag's attributes are described in Table 12.1 . Table 12.1: The Attributes for the <form> Tag Attribute Definition method="GET|POST" This attribute determines which request method will be used to transmit the gathered data. The default method is GET. action="URL" This attribute defines the target of the form. It can be a CGI application, Java servlet, or JSP. - 114 - The differences between the GET and POST requests are very important to understand. The GET appends form data to a URL in the form of a query string. The data sent in the query string is in the form of tag/value pairs separated by ampersands (&). A sample URL with an appended query string is listed below: http://yourserver/search.jsp?keyword1=PureJSP&keyword2=servlets In this example, our tag/value pairs are listed to the right of the ? character. The ? character is used to separate the URL from the query string when using a GET request. You should note that the data sent using a GET request is visible in the URL. The amount of data you can send in the query string is also limited to 255 bytes. The POST method passes data in the request headers sent from the client. It can send an unlimited amount of data in the query string and the query string will not be displayed in the URL. The <input> Tags The <input> tags are used to capture form data. A basic form is made up of two <input> types. The first are just your basic elements, such as text fields and radio buttons. The second is the Submit button, which actually sends the data to the target URL referenced in the action attribute. The basic syntax of an <input> tag is listed below: <input type="text" name="inputname" value="inputvalue"> The equivalent syntax for creating a Submit button is listed below: <input type="Submit" value="Submit"> The <input> tag's attributes are described in Table 12.2 . Table 12.2: The Attributes for the <input> Tag Attribute Definition type="text|submit|etc." This attribute determines which type of input object is being used. name="inputname" This attribute represents the tag in the tag/value pair that is sent in the request. value="inputvalue" This attribute represents the value in the tag/value pairs sent in the request. Using a JSP to Create an HTML Form Creating an HTML form using a JSP is just like creating a form using any other tool. JSP just gives you the ability to create form elements dynamically. An example of this would be to create a form to gather a user's name and shipping address. The dynamic part would be to have a default value for the user's company name, if the user has already logged in. Listing 12.1 contains a sample JSP that builds this type of form. Listing 12.1: CreateForm.jsp <html> <head> <title>Create A JSP Form</title> </head> <body> - 115 - <form action="/purejsp/RetrieveFormData.jsp" method="post"> <table align="center" cellspacing="2" cellpadding="2" border="1"> <tr> <td colspan="3"> <b>Company:</b> <% String company = request.getParameter("company"); if ( company != null ) { %> <input type="text" name="company" value="<%=company %>" size="40" maxlength="40"> <% } else { %> <input type="text" name="company" size="40" maxlength="40"> <% } %> </td> <tr> <td colspan="3"> <b>Street:</b> <input type="text" name="street" size="43" maxlength="43"> </td> </tr> <tr> <td> <b>City:</b> <input type="text" name="city" size="20" maxlength="20"> </td> <td> <b>State:</b> <input type="text" name="state" size="2" maxlength="2"> </td> <td> <b>Zip:</b> <input type="text" name="zip" size="5" maxlength="5"> </td> </tr> - 116 - <tr> <td> <input type="Submit" value="Submit"> </td> </tr> </table> </form> </body> </html> You can see in Listing 12.1 that the only thing differentiating this HTML form from others you may have seen is that it contains a JSP scriptlet section that checks for the existence of the user's company name in the request. If the user name is there, the scriptlet sets the value of the company input to the value retrieved from the request. To see this in action, copy the JSP to the <SERVER_ROOT>/purejsp/ directory and open your browser to the following URL: http://yourserver:8080/purejsp/CreateForm.jsp?company=Sams You will see a page similar to Figure 12.1 . Figure 12.1: The output from CreateForm.jsp. Retrieving Form Data with a JSP Now we need to process the data sent from the CreateForm.jsp. To do this we are going to create a bean that has properties that match the tag/value pairs sent in the request. Listing 12.2 contains the source for our Company bean. Listing 12.2: Company.java /** This bean is used to encapsulate simple * company data. **/ public class Company { private String company = null; private String street = null; - 117 - private String city = null; private String state = null; private String zip = null; // Default Constructor public Company() { } // Public Accessors public String getCompany() { return company; } public void setCompany(String value) { company = value; } public String getStreet() { return street; } public void setStreet(String value) { street = value; } public String getCity() { - 118 - return city; } public void setCity(String value) { city = value; } public String getState() { return state; } public void setState(String value) { state = value; } public String getZip() { return zip; } public void setZip(String value) { zip = value; } } The source for the JSP used to retrieve the form data is included in Listing 12.3 . - 119 - Listing 12.3: RetrieveFormData.jsp <%@ page errorPage="errorpage.jsp" %> <! Instantiate the Company bean with an id of "company" > <jsp:useBean id="company" scope="request" class="Company" /> <! Set the values of the company bean to the request values > <jsp:setProperty name="company" property="*" /> <html> <head> <title>Retrieve Form Data</title> </head> <body> <! Output the values of the bean retrieved from the request > <b>Company: </b> <%=company.getCompany() %><br> <b>Street: </b> <%=company.getStreet() %><br> <b>City: </b> <%=company.getCity() %> <b>State: </b> <%=company.getState() %> <b>Zip: </b> <%=company.getZip() %> </body> </html> You can see that the first thing this JSP does is create an instance of the company bean, with request scope. After the bean is created, we set the properties of the bean to the values passed in the request. This is done by using the <jsp:setProperty> standard action. We tell the standard action that we want to use the company bean and set the property attribute to an asterisk (*) character. This tells the <jsp:setProperty> standard action to search the request for parameter names that match the properties of the company bean. If there are any matches, then the standard action will set the bean properties to those values. To see the results of this JSP, copy it to the <SERVER_ROOT>/purejsp/ directory, compile and move the Company.java.class to the <SERVER_ROOT>/purejsp/WEB-INF/classes, and press the Submit button on the form created by the CreateForm.jsp. You will see an image similar to Figure 12.2 . - 120 - Note You can use the request.getParameter() method to retrieve form data if you do not have matching parameters. Figure 12.2: The output from RetrieveFormData.jsp. Summary In this chapter, we covered how you can retrieve form data using JSPs. You should now feel comfortable with retrieving data from forms using either GET or POST requests. In Chapter 13 , we cover how you can incorporate a shopping cart into a JSP. Chapter 13: JSP and a Shopping Cart Overview One of the more common components used in today's electronic commerce applications are shopping carts. In this chapter, we are going to create a JavaBean that encapsulates a shopping cart, a JSP that displays the contents of the shopping cart, and a JSP that uses the created shopping cart. Creating a Shopping Cart The shopping cart we are creating provides transient storage to hold any number of items selected by the user. It provides the functionality to add items, remove items, and change the quantity of an item in the cart. In a normal application you would create an object that would represent a cart item, but for this example we are just adding the properties of an item. Listing 13.1 contains the source for the shopping cart. Listing 13.1: ShoppingCart.java import java.lang.String; import java.lang.Integer; import java.lang.Float; import java.util.Hashtable; import java.util.Enumeration; public class ShoppingCart { protected Hashtable items = new Hashtable(); . commerce applications are shopping carts. In this chapter, we are going to create a JavaBean that encapsulates a shopping cart, a JSP that displays the contents of the shopping cart, and a JSP that. standard action. We tell the standard action that we want to use the company bean and set the property attribute to an asterisk (*) character. This tells the <jsp:setProperty> standard. just adding the properties of an item. Listing 13.1 contains the source for the shopping cart. Listing 13.1: ShoppingCart .java import java. lang.String; import java. lang.Integer; import java. lang.Float;