As an example of code that is too complex for a JSP expression alone, Listing 11.8 presents a JSP page that uses the bgColor request parameter to set the background color of the page. Simply using <BODY BGCOLOR="<%= request.getParameter("bgColor") %>"> would violate the cardinal rule of reading form data: always check for missing or malformed data. So, we use a scriptlet instead. JSP-Styles.css is omitted so that the style sheet does not override the background color. Figures 11-5, 11-6, and 11-7 show the default result, the result for a background of C0C0C0 , and the result for papayawhip (one of the oddball X11 color names still supported by most browsers for historical reasons), respectively. Listing 11.8 BGColor.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Color Testing</TITLE> </HEAD> <% String bgColor = request.getParameter("bgColor"); if ((bgColor == null) (bgColor.trim().equals(""))) { bgColor = "WHITE"; } %> <BODY BGCOLOR="<%= bgColor %>"> <H2 ALIGN="CENTER">Testing a Background of "<%= bgColor %>"</H2> </BODY></HTML> Figure 11-5. Default result of BGColor.jsp . Figure 11-6. Result of BGColor.jsp when accessed with a bgColor parameter having the RGB value C0C0C0 . Figure 11-7. Result of BGColor.jsp when accessed with a bgColor parameter having the X11 color name papayawhip . ![]() |