Passing data between Web pages using JavaBean and JSP parameters
Introduction: Web Architecture designer Brett McLaughlin demonstrates how the combination of JavaBean components and JSP technologies enables you to store and deliver data between Web pages, and how this can be done to achieve more dynamic site design.
So far, we've focused on more basic topics in the JSP Best Practices series. In the first two articles, you learned how to use the JSP include mechanism to introduce external content to your Web site or Web application. We use two different include pseudo directives: static include command and dynamic jsp:include tag.
To date, there is no need to create any type of communication between the parent page (in our example, the home page of a Web site) and the content contained. But the scheme is too simple. When you want to program an actual web site or Web application interface, you typically need a communication mechanism to pass data between the parent page and the included file. For example, your site might have a title or message originating from the home page, and it needs to be provided to the header or footer of the page. In this article, you will learn how to pass data between pages, and how to use that data in the included pages.
Note: All the best practices in this series are based on the JavaServer Pages technology. To run any of these practice examples, you need to install a WEB container that conforms to JSP technology on the local machine or test server. You will also need to encode the JSP pages using a text editor or IDE.
JavaBean component for storing data
Let's look at a site where each page has a short "banner" (such as "books:a shelf full learning" or "cds:music worth listening to") and headings. The parent page (sometimes called the home page) determines the banner for each page, and the page header (the included page) processes the HTML to output the banner. For this scenario to work, the main page must be able to pass the banner to the page header, and the page header must be able to accept the page title and display it as requested.
First we need some kind of object to store the data being passed. It happens to be (not accidental) that the JavaBean component fits both this and the JSP technology very well. The bean only needs to use the value method (accessor) and the assignment method (mutator) to process the data. You might learn from other Java programming experiences that get () is a value method because it accesses data, and set () is an assignment method because it modifies the data.
Listing 1 shows the code for the kind of bean we need. The Pageheaderinfo bean contains information about the Web page header.
Listing 1. Pageheaderinfo JavaBean
package com.newInstance.site.beans;
import java.io.Serializable;
public class PageHeaderInfo implements Serializable {
/** The title of the page */
private String pageTitle;
/** The slogan of the page */
private String pageSlogan;
public String getPageTitle() {
return pageTitle;
}
public void setPageTitle(String pageTitle) {
this.pageTitle = pageTitle;
}
public String getPageSlogan() {
return pageSlogan;
}
public void setPageSlogan(String pageSlogan) {
this.pageSlogan = pageSlogan;
}
}