JSP: Creating Dynamic Titles

Tuesday Mar 20th 2001 by Stephanie Kaminaris

Add flair to your Web site. Learn how to create dynamic titles using JSP.

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.

Consider the simple task of generating a string repeatedly. This can easily be done by putting the string inside a for loop. The listing below 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: 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 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.


There are many ways to use JSPs to generate dynamic content. This article showed you how to generate dynamic titles.

About the Authors:

Jose Annunziato received his Master's and Doctor of Science degrees in computer science from the University of Massachusetts. Lowell. Dr. Annunziato works for BEA Systems where he develops courseware products such as WebLogic Server, WebLogic Commerce Server, and WebLogic Enterprise.

Stephanie Fesler Kaminaris has written hundreds of pages of course materials on Web development, such as HTML, JavaScript, Active Server Pages, VBScript, and CGI programming. She has also written course materials for BEA's WebLogic Server courses, such as WebLogic as a Web Server, Java Server Pages, Servlets, and Web Applications.

This article is based on techniques presented in Sams Teach Yourself JavaServer Pages in 24 Hours (0672320231) - a book by Jose Annunziato and Stephanie Fesler Kaminaris, with Sams Publishing.

© Copyright Sams Publishing, All Rights Reserved

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved