Designing Dynamic Response JSPs

Generating dynamic content in Web applications is important when the content must reflect the most current and available data and personalized information. In this article, you’ll learn several methods for using JavaServer Pages to create dynamic content.

From the author of

From the author of

One of the main advantages of JavaServer Pages is the ability to generate dynamic
content. JSPs generate dynamic HTML pages by using Java control structures like
for loops and if statements. As a result, forms can be generated
dynamically following some specified logical layout.

Generating dynamic content in Web applications is important when the content
must reflect the most current and available data and personalized information.
An example of such an application is an online stock portfolio that allows users
to keep track of their stock information online, including options, stock listings,
current stock prices, and daily, comprehensive, high and low prices. A typical
page of such an application might include a table that lists a variety of stock
information in separate rows, generated by a JSP. You can use a JSP to dynamically
generate this sort of HTML.

Dynamic Titles

Consider the simple task of generating a string repeatedly. This can easily
be done by putting the string inside a for loop. Listing 1 provides a
simple example of a JSP that generates a dynamic response to the user. The example
consists of generating several progressively smaller HTML headers. Each header
is lighter in color than the preceding one.

Line 4 declares a colorArray containing a hexadecimal color string.
Color in HTML is defined as the combination of the colors red, green, and blue.
Each of these has hexadecimal values from 00 to FF (0 to
255 in decimal), and the three are combined by concatenating their values.
So, purple would be FF00FF, black would be 000000, white would
be FFFFFF, and so on.

These colors are combined in the fgColor string in line 9. Since all
three color components are the same, the resulting color is several shades of
gray, as Figure
1 shows. The index j of the for loop in line 8 is used to
reference the colorArray in line 9 and then in line 10 to declare progressively
smaller headers (H1, H2, … H4). The color of the header
JSPs are great !!! (line 12) is set using a FONT tag in line 11.