JSP Best Practice: use JSP: Include to control dynamic content

Source: Internet
Author: User
Tags perl script
Writer, o'reilly and Associates
July 2003

This article is a follow-up article by Java "informed person" Brett McLaughlin following the first JSP best practices article. In this article, the author shows you how to extend the inclusion function of Dynamic Content in JSP technology. Measure the test taker's knowledge about the differences between static include pseudo commands and dynamic JSP: include elements.
In the previous article in The New JSP Best Practices series, you learned how to use the JSP include Directive to include static content such as headers, footers, and navigation components into web pages. Like on the server, the JSP include Directive allows a page to extract content or data from another page. Listing 1 relives the include Directive.

Listing 1. jsp include pseudo commands

<! [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>

<% @ Include file = "header. jsp" %>
<% @ Include file = "navigation. jsp" %>
<% @ Include file = "bookshelf. jsp" %>
<% @ Include file = "/MT-blogs/index. jsp" %>
<% @ Include file = "footer. jsp" %>

</Body>
</Html>
]>

What do you need
All the best practices in this series are based on the assumerver Pages technology. To run any of the best practices, you must install JSP-compliant Web containers on your local machine or test server. You also need to use a text editor or IDE to encode JSP pages.

Although include is very suitable for incorporating static content into web pages, it is not satisfactory for dynamic content. In the previous article, we found this problem when trying to reload the cache file. Unlike most header and footer files, dynamic content changes frequently and must be updated at any time. We will first repeat the limitations of the include Directive, and then I will show you how to use the JSP: Include tag to expand the JSP inclusion capability.

High-speed cache Problems
One disadvantage of the JSP include directive is that it causes the web browser to cache all pages at a high speed. This makes sense when processing Static components such as footer, copyright notice, or a set of static links. These files will not change, so there is no reason for the JSP interpreter to continuously round the data. Wherever possible, high-speed caching should be implemented because it improves the performance of applications.

JSP testing and development
When building a web application or website, you may need to update a large number of headers, footers, and navigation links. It may be a pain to be forced to close the browser or clear its high-speed cache just to see the changes made to the included files. On the other hand, to end the development cycle, You have to thoroughly check and modify hundreds of pages that use the include Directive. This is also a pain point. My suggestion is to disable the browser cache during the test. In most cases, this can completely solve the problem. In rare cases, this does not work. In this case, you can restart the Web container on the browser or server to ensure that no high-speed cache is performed.

However, sometimes cache is not worth the candle. If the imported content comes from a program that uses dynamic data (such as weblog or database-driven JSP files), or if the content contained is HTML (such as timestamps) that frequently changes ), therefore, the latest versions of these files or programs must be displayed whenever a Web page is loaded. Unfortunately, the JSP include Directive does not support this function. In the test and development cycle (see "JSP test and Development" on the navigation bar), disabling high-speed cache in a browser usually solves this problem. However, for actual applications, performance is an important factor in any design decision-making process. Disabling high-speed cache is not a feasible long-term plan. A better solution is to use the JSP: Include tag.

JSP: Include tag
JSP: Include is just a pseudo command different from include. The advantage of JSP: Include is that it always checks for changes in the contained files. Later we will look at the way this new label works. But first, let's take a look at the two types of include code to see the similarities and differences between the two.

Listing 2 shows a simple page that uses the original JSP include Directive.

Listing 2. jsp include pseudo commands

<! [CDATA [
<% @ Page Language = "Java" contenttype = "text/html" %>
<HTML>
<Head>
<Title> JSP include element test </title>
</Head>
<Body>
This content is statically in the main JSP file. <br/>
<% @ Include file = "included.html" %>
</Body>
</Html>
]>

Listing 3 is the same page, but here it is converted to the JSP: Include tag.

Listing 3. Convert to JSP: Include

<! [CDATA [
<% @ Page Language = "Java" contenttype = "text/html" %>
<HTML>
<Head>
<Title> JSP include element test </title>
</Head>
<Body>
This content is statically in the main JSP file. <br/>
<JSP: Include page = "included.html" Flush = "true"/>
</Body>
</Html>
]>

Note the two differences between the two types of code. First, the JSP: Include element does not use the % @ syntax of the include Directive. In fact, JSP prefixes let the JSP compiler know that it should look for elements in the standard JSP tag set. Second, the attribute of the file to be included is changed from file to page. If you want to, you can test the new tag results by yourself. You only need to change the content of the included.html file in the previous article (see references), and then reload the browser page to view the new content immediately.

Flush Properties
You may have noticed the flush attribute in the JSP: Include sample code. As the name suggests, flush indicates whether to clear any existing buffer before reading the contained content. The flush attribute is required in JSP 1.1. Therefore, if the Code does not use it, an error is returned. However, in JSP 1.2, the default flush attribute is false. Since clearing is not an important issue most of the time, my suggestion is: for JSP 1.1, set flush to true; for JSP 1.2 and later versions, set it to disabled.

How does JSP: include work?
If you are a bit confused, you may be wondering why the JSP: Include flag is different from the include Directive. The truth is actually very simple: JSP: Include contains the URI response, not the URI itself. This means that the URI is interpreted and contains the generated response. If the page is HTML, you will get HTML that is not changed at all. However, if it is a Perl script, Java Servlet, or CGI program, the result will be interpreted from the program. Although the page is usually HTML, the actual program is exactly the means to achieve the goal. In addition, the results are never cached as quickly as they are when the include Directive is used. Although this is only a small change, it leads to all the differences in the behavior you see.

A hybrid combination Solution
The include Directive is useful on some websites. For example, if the site contains a few (or few) slightly unchanged headers, footers, and navigation files, the basic include Directive is the best option for these components. Because the include Directive uses high-speed cache, the content of the include Directive is cached at high speed only once. The results greatly improve the performance of the website.

However, for many web applications or sites, the carpet-based high-speed cache cannot solve the problem. Although the header and footer may be static, it is impossible for the whole site to be static. For example, it is common to extract navigation links from databases, and many JSP-based sites also extract content from dynamic JSP pages on other sites or applications. If you are processing dynamic content, you must use JSP: Include to process the content.

Of course, the best solution is to mix the two methods and use each structure in the most appropriate place. Listing 4 is an example of a hybrid combination solution.

Listing 4. Hybrid Collocation Solution

<! [CDATA [
<% @ Page Language = "Java" contenttype = "text/html" %>
<HTML>
<Head>
<Title> newinstance.com </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = 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 = ""/>
</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>
]>

The code above shows the example index page in the previous article. The navigation link and footer are static content, which can be changed at most once a year. For these files, I use the include Directive. The content pane contains the weblog and "bookshelf" components, which are dynamically generated. These two components need to be updated all the time, so I use the JSP: Include tag for them. The header. jsp file is a bit strange. This component is extracted from another static JSP page. However, as you will notice, it extracts the "tagline" from the include page and then displays it. To process this shared information, we must input parameters to the header file. To process those parameters, you must use the JSP: Include element.

If you want to know about the parameters, please rest assured that the subsequent articles will soon meet your needs. In the next article, I will explain JSP parameters and how they interact with the JavaBeans component. We will see you online at that time.

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.