JavaSercer Pages Using a bean to capture and
JavaSercer Pages Using a bean to capture and validate user input is one aspect of building a web application that’s easy to maintain and extend as requirements change. But it’s not the only option. If you’re a page author and intend to use JSP to develop sites using components from third parties, you may wonder how you can capture and validate input without access to a Java programmer who can develop the beans. Don’t worry; we’ll see another alternative in Chapter 9. Processing and validating input can also be performed by a servlet instead of a JSP page. If you’re a programmer, you’ll find examples of this approach in Chapter 14. In this part of the book, however, we use JSP pages for all aspects of the applications so we can focus on JSP features. And one JSP feature makes it very easy to capture user input, so let’s see how it’s done. 5.2.1.1 Setting JavaBeans properties from user input In this next example, we capture information about web site users. It could be the frontend to a newsletter subscription site, for instance. In order to send the users information that might interest them, we register the birth date, sex, and lucky number, along with the full name and email address, for each person that signs up for the service. To capture and validate the user input, the example uses a bean named com.ora.jsp.beans.userinfo.UserInfoBean, with the properties described in Table 5.2. If you’re a programmer, you may want to skip ahead to peek at the source code for this bean class in Chapter 15. Table 5.2, Properties for com.ora.jsp.beans.userinfo.UserInfoBean Property Name Java Type Access Description userName String read/write The user’s full name birthDate String read/write The user’s birth date in the format yyyy-mm-dd (e.g., 2000-07-07) emailAddr String read/write The user’s email address in the format name@company.com sex String read/write The user’s sex (male or female) luckyNumber String read/write The user’s lucky number (between 1 and 100) valid boolean read true if the current values of all properties are valid, false otherwise As shown in the Access column, all properties except valid are read/write properties. This means that, in addition to using the bean’s properties to generate output (like in Example 5.1), the property values can be set based on user input. The HTML form shown in Example 5.2 allows the user to enter information corresponding to the bean properties. page 47
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services