JavaSercer Pages Here, the userName property is set to the value of a request parameter named someOtherParam. You can also explicitly set a bean property to a value that is not sent as a request parameter with the value attribute: Here, the luckyNumber property is set to the value 13. You typically use the value attribute only when you set the bean properties based on something other than user input, for instance values collected from a database. 5.2.1.2 Validating user input Never trust your users, at least not when it comes to entering information in the format you need. Often, you need to make sure the input is valid before you continue to process a request. A date, for instance, can be written in many different formats. If you don’t live in the United States, you probably have had to fill out both an I-94 and a customs declaration form to be admitted by an immigration officer. You may have noticed that on one of the forms you need to write your birth date as yyyy/mm/dd and on the other you write it as mm/dd/yyyy. I always get it wrong. Four of the UserInfoBean’s properties require a special format: birthDate, emailAddr, sex, and luckyNumber. A good place to make sure the input is valid is in the bean itself, which is exactly what the UserInfoBean does. With this bean, if you try to set any of the above properties to a value that isn’t valid, the bean will leave the property unset. In addition, the bean has a true/false (Boolean) property named valid. This property has the value false unless all other properties have been set to valid values. Let’s see this in action. Example 5.3 displays the property values using the action:
User Name: Since a property is set only if the value is valid, no values are shown for improperly specified properties. Try it. Click on the “User Info 1 example” link under the Chapter 5 header in the book examples main page shown in Figure 5.1. Enter both valid and invalid values in the form and look at the result produced by the userinfo1.jsp page when you click Submit. A sample result is shown in Figure 5.4. Figure 5.4. Output from userinfo1.jsp Note that the Birth Date information is missing (at my age, you’re not so eager to reveal your birth date), so the input is marked as invalid. page 50
Note: If you are looking for good and quality webspace to host and run your java application check professional java hosting services
This entry was posted
on Saturday, December 2nd, 2006 at 12:10 am 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.