11.8 Scriptlet Example

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 .


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net