JavaSercer Pages 5.2.3 Formatting HTML Output If you enter a value containing double quotes in the Name field of the userinfo2.jsp page, it doesn’t work right. For example, try “Prince, “the artist”" and you’ll see what I mean. Only “Prince,” appears in the Name field, and the Birth Date field is not shown at all. What’s going on here? A look at the HTML code generated by the JSP page using your browser’s View Source function reveals what’s wrong:
Name:
In the JSP file, double quotes are used to enclose the value of the element’s value attribute, so when the value itself includes a double quote, the browser gets confused. The first double quote in the value is interpreted as the end of the value. That’s why you see only “Prince,” in the field. Even worse, the rest of the value interferes with the interpretation of the rest of the form, causing the next input field to be ignored in most browsers. One solution to this problem would be to use single quotes around the values instead, since HTML accepts either single quotes or double quotes. But then you would have the same problem if the user enters a value that includes a single quote. Fortunately, there’s a better way. What’s needed is special treatment of all characters that can cause HTML interpretation problems when we generate HTML from dynamic strings. One way to handle this is to let the bean take care of the special treatment. The UserInfoBean can do this through another set of properties: userNameFormatted, birthDateFormatted, emailAddrFormatted, sexFormatted, and luckyNumberFormatted. These are read-only properties that simply represent formatted versions of the corresponding real property values. The bean is designed so that when you use these property names, all troublesome characters in the real property values - such as single quotes, double quotes, less-than symbols, greater-than symbols, and ampersands - are converted to their corresponding HTML character entities (i.e., ', ", <, >, and &). The browser handles the converted values with no problem. If you’re curious about the Java code for the formatted properties, it’s described in Chapter 15. Example 5.5 shows a JSP page that uses the new properties. It’s not always a good idea to have a bean handle this type of formatting, though. A bean is easier to reuse if it doesn’t contain logic that is specific for one type of use, such as generating strings suitable for HTML. When we look at scripting elements and custom actions, we will revisit the subject of HTML formatting and look at other solutions to this problem. Try the final version of this example by clicking on the “User Info 3 example” link. Now everything works fine, even if you happen to be Prince, “the artist.” page 53
Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Sandzak jsp web hosting provider
This entry was posted
on Saturday, December 2nd, 2006 at 7:41 pm and is filed under Java.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.