intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Java Server Pages: A Code-Intensive Premium Reference- P12

Chia sẻ: Cong Thanh | Ngày: | Loại File: PDF | Số trang:10

89
lượt xem
7
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Java Server Pages: A Code-Intensive Premium Reference- P12:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basic structure. This should help you outline your reading plan if you choose not to read the text from cover to cover. This introduction gives you an overview of what each chapter covers.

Chủ đề:
Lưu

Nội dung Text: Java Server Pages: A Code-Intensive Premium Reference- P12

  1. You can see that the only difference between these two JSPs is the values of the HTML and tags. To see how a session bean works, copy both of these JSPs to the /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 - 111 -
  2. Application Bean Example 1 Application Bean Example 1 The current count for the counter bean is: 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 Application Bean Example 2 Application Bean Example 2 The current count for the counter bean is: - 112 -
  3. Go ahead and copy both of these JSPs to the /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 tag and the tags. Both of these elements are described below. The Tag The 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 tag is listed below: ... The tag's attributes are described in Table 12.1. Table 12.1: The Attributes for the 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. - 113 -
  4. 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 Tags The tags are used to capture form data. A basic form is made up of two 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 tag is listed below: The equivalent syntax for creating a Submit button is listed below: The tag's attributes are described in Table 12.2. Table 12.2: The Attributes for the 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 Create A JSP Form - 114 -
  5. Company: Street: City: State: Zip: - 115 -
  6. 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 /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; - 116 -
  7. 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() { - 117 -
  8. 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. - 118 -
  9. Listing 12.3: RetrieveFormData.jsp Retrieve Form Data Company: Street: City: State: Zip: 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 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 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 /purejsp/ directory, compile and move the Company.java.class to the /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. - 119 -
  10. 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(); - 120 -
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2