Home > Articles > Programming > Java

Designing Dynamic Response JSPs

  • Print
  • + Share This
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

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.

Listing 1  dynamicTitles.jsp

 1: <HTML><HEAD><TITLE>Dynamic Titles</TITLE></HEAD>
 2: <BODY>
 3: <CENTER>
 4: <%  String[] colorArray = { "00", "11", "22", "33",
 5:                             "44", "55", "66", "77",
 6:                             "88", "99", "AA", "BB",
 7:                             "CC", "DD", "EE", "FF"};
 8:     for(int j=1; j<5; j++){
 9:         String fgColor = colorArray[j*3]+colorArray[j*3]+colorArray[j*3];%>
10:         <H<%=j%>>
11:         <FONT COLOR=<%=fgColor%>>
12:         JSPs are great !!!
13:         </FONT>
14:         </H<%=j%>>
15: <%  }   %>
16: </CENTER>
17: </BODY></HTML>

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.

Figure 1

dynamicTitles.jsp produces a set of progressively smaller and lighter HTML headers.

  • + Share This
  • 🔖 Save To Your Account