[Translation] JavaBean Component and JSP Technology Integration

Source: Internet
Author: User
Tags html header
The Web architect Brett McLaughlin shows us how to use JavaBean and JSP technologies to store and transmit data on web pages, and how to design it to achieve greater flexibility.

So far, we have discussed a considerable amount of basic knowledge in the JSP Best Practices series. In the previous two chapters, you should learn how to use the JSP include mechanism to include content other than the website on pages or applications.Program. There are two different types of include: static include command and dynamic JSP: Include tag.

At the same time, we have not involved communication between the parent page (in our example, the homepage of the website) and the contained content. In fact, this situation is very common. When you start to build a real website or web application, you usually need such a communication mechanism. For example, your website may produce some small text such as the title on the homepage, which will appear on the header or footer page. In this section, you will learn how to transfer data between pages and how to use the data in included pages.

Note: All examples in this section are based on the assumerver Pages technology. To run them, you need to create a JSP-compatible Web container, on your local computer or on a server for testing. You also need a text editor or integrated environment to compile your JSP page.

JavaBean used to save data

Let's assume that a website contains a small "slogan" (for example, "Book: container full of knowledge" or "Recording: worth hearing") on each page ") and a title. The parent page (sometimes called the home page) determines the slogan of each page, but what really generates the HTML output is the header page, which is included. To achieve this goal, the parent page must be able to pass the slogan to the header page. The header page will get the slogan and output it as the page title.

First, we need an object to save the transmitted data. The JavaBean Component is such an appropriate choice. It works seamlessly with JSP technology, you can simply use the value method and value assignment method to control the data you want. Readers with a little Java programming experience may have thought that get () is a value method because it reads data, while set () is a value assignment method because it changes data.
List 1 shows a JavaBean Code , Pageheaderinfo contains the website header information. <! [CDATA [
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;
}
}
]>

As the first exercise, save the file as pageheaderinfo. Java and compile it. Next, place the obtained class file pageheaderinfo. Class in the WEB-INF/classes directory of your web application. Make sure that the directory contains the package structure, for example:
$ <Tomcat-root>/webapps/$ <WEB-APP-NAME>/WEB-INF/classes/COM/newinstance/site/beans/pageheaderinfo. Class

You can use a path like this to arrange the classes used in Web applications. Follow these steps to save the data to pageheaderinfo and obtain it on different JSP pages.

Transfer saved data

On our website, the header page contains code that passes different slogans to different pages. View the previous chapter and you will find that the header page (header. jsp) uses JSP: Include to mark included files. List 2 shows how the website homepage passes data to the header. jsp file by TAG. <! [CDATA [
<% @ Page Language = "Java" contenttype = "text/html" %>
<HTML>
<Head>
<Title> newinstance.com </title>
<Meta http-equiv = "Content-Type"
Content = "text/html; charsets = iso-8859-1"/>
<Link href = "/styles/default.css" rel = "stylesheet" type = "text/CSS"/>
</Head>

<Body>
<JSP: Include page = "header. jsp" Flush = "true">
<JSP: Param name = "pagetitle" value = "newinstance.com"/>
<JSP: Param name = "pageslogan"
Value = "Java and XML: Turning theory into practice"/>
</Jsp: Include>
<% @ Include file = "/navigation. jsp" %>
<JSP: Include page = "bookshelf. jsp" Flush = "true"/>

<JSP: Include page = "/MT-blogs/index. jsp" Flush = "true"/>

<% @ Include file = "/footer. jsp" %>
</Body>
</Html>
]>

It can be seen that the title is passed as a slogan.

You may have noticed that the actual existence of the JavaBean Component is not required when you create a page. I always write JavaBean first. There is a good reason: the JSP parameter name must match the JavaBean attribute name. To complete JavaBean first, you can ensure that you use the appropriate parameter name when writing the JSP page.

Obtain data

After coding the JSP parameter and the JavaBean attribute, once the data is passed to header. jsp, the page can start to get the data. List 3 shows the header. jsp page. Most of its content is HTML. Please note the JSP script in it. After you have studied the code, I will explain it to you later. <! [CDATA [
<! -- Begin header section -->
<% @ Page Language = "Java" contenttype = "text/html" %>
<JSP: usebean id = "pageheaderinfo"
Class = "com. newinstance. Site. Beans. pageheaderinfo">
<JSP: setproperty name = "pageheaderinfo" property = "*"/>
</Jsp: usebean>

<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<TD width = "91" Height = "50" align = "right" valign = "TOP"
Bgcolor = "#330066"> <font color = "# ffffff"> Src = "http://images.cnblogs.com/header-lions.gif"
Width = "90" Height = "60"> </font> </TD>
<TD colspan = "3" align = "Left" valign = "TOP"
Bgcolor = "#000000"> <Table width = "100%" Height = "60" border = "0"
Cellpadding = "0" cellspacing = "0">
<Tr>
<TD width = "261" rowspan = "2"> Src = "http://images.cnblogs.com/header-title.gif" width = "261" Height = "60"> </TD>
<TD class = "pagetitle" width = "249" Height = "55" align = "right"
Valign = "bottom"> <JSP: getproperty name = "pageheaderinfo"
Property = "pageslogan"/> </TD>
<TD width = "10" Height = "55"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "5"> Height = "5"> </TD>
<TD Height = "5"> Height = "5"> </TD>
</Tr>
</Table> </TD>
<TD width = "141" bgcolor = "#000000">
<Font color = "# ffffff"> & nbsp; </font>
</TD>
</Tr>
<! -- End header section -->
]>

The first line of code identifies the page as a JSP page, and then uses the JSP: usebean tag to declare that the JavaBean of pageheaderinfo needs to be accessed. The ID attribute specifies a name for the bean, you can use the bean name on the JSP page. The class attribute specifies the full name of the JavaBean class. The adjacent JSP: setproperty tag indicates that all attributes of the JavaBean (identified by the ID attribute) are assigned values based on the request data. That is, each attribute in the bean (such as pagetitle and pageslogan) find the request parameter corresponding to the name to assign values. These request parameters can be from the client browser or other JSP pages that contain this page. In this case, the unique request data is created by the parent page. For our website, the homepage (index. jsp) sends pagetitle and pageslogan, whose values are "newinstance.com" and "Java and XML: Turning theory into practice ".

Once the bean attribute is assigned a value, the page can use the data. For header. jsp, you can see in the code below that the data is used through the JSP: getproperty tag. JSP; The getproperty tag identifies the object from which data is retrieved through the name parameter, and the property tag identifies which property of the object. The obtained data value is automatically inserted into the output of the page, that is, the output to the parent page, so as to get a seamless and dynamic page slogan. It is so easy to transfer data between JSP pages! You can add any number of beans to a JSP page. Each bean can have any number of attributes to handle any complicated request data.

Handle demand changes

Changes are the biggest headache for software developers. You have written your bean and the code to use them on the JSP page, at this time, the requirements of Web applications may inevitably change. If this change requires more attributes (in most cases), you have to rewrite your JavaBeanSource code, Re-compile it, and make sure that the JSP page can correctly access the newly generated Bean class. In some cases, you do not need to process the bean. If you save or retrieve the attribute page, it is no longer used (that is, even if the page is still in the site directory, but you no longer use this page), you do not have to worry about the original code. Actually, I have encountered this situation!

My personal website's header page header. JSP is used to generate the HTML header. In the past, this page inserted a title into the title tag in the header of other pages. Later, I modified it in the header. the page title is no longer used in JSP. However, I did not remove pagetitle from pageheaderinfo. In fact, I did not even remove the JSP: Param tag in most JSP pages, the purpose of this tag is to set the title for the page. I don't think it is worth the effort, because I'm sure that keeping this data will not have any bad impact (maybe I will use it again one day !). Therefore, if you encounter the same situation, you don't need to waste time-it's better to have time to deal with these things than to add new, interesting, and practical features to your web application.

Next time

After learning how to pass data between JSP pages, try to write some useful JavaBean and see if they can be used on your site. Through contact with these beans, JSP: usebean, JSP: Param, and JSP: Get/setproperty, you should be able to make some cool functions! In the next best practice, I will show you how to add external content to the site using JSP. The jstl mark is similar to the include mark we are familiar, it makes JSP more flexible and efficient. Before that, please work hard to prepare. Goodbye!

(This articleArticleIt was translated a year ago. At that time, it was still fruitless to look for a translation job. See the original English document .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.