Use jsp: include to control dynamic content, jspinclude

Source: Internet
Author: User
Tags perl script

Use jsp: include to control dynamic content, jspinclude

This example describes how to use jsp: include to control dynamic content. We will share this with you for your reference. The details are as follows:

Listing 1. JSP include pseudo commands

<![CDATA[<%@ page language="java" contentType="text/html" %>

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.

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" %>

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" %>

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.

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" %>

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.

I hope this article will help you with jsp program design.

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.