Using Javabeans Components in JSP Documents: Agenda
Using Javabeans Components in JSP Documents: Agenda
JSP and Servlet Training Courses: http://courses.coreservlets.com JSP and Servlet Books from Sun Press: http://www.coreservlets.com
Agenda
Understanding the benefits of beans Creating beans Installing bean classes on your server Accessing bean properties Explicitly setting bean properties Automatically setting bean properties from request parameters Sharing beans among multiple servlets and JSP pages
Complex Application
Scripting elements calling servlet code directly Scripting elements calling servlet code indirectly (by means of utility classes) Beans Servlet/JSP combo (MVC) MVC with JSP expression language Custom tags
Persistent values should be accessed through methods called getXxx and setXxx
If class has method getTitle that returns a String, class is said to have a String property named title Boolean properties use isXxx instead of getXxx
with
private double speed; public double getSpeed() { return(speed); } public void setSpeed(double newSpeed) { speed = newSpeed; }
If users of your class accessed the fields directly, then they would each be responsible for checking constraints.
If users of your class accessed the fields directly, then they would each be responsible for executing side effects. Too much work and runs huge risk of having display inconsistent from actual values.
jsp:getProperty
This element reads and outputs the value of a bean property. It is used as follows: <jsp:getProperty name="beanName" property="propertyName" />
jsp:setProperty
This element modifies a bean property (i.e., calls a method of the form setXxx). It is normally used as follows: <jsp:setProperty name="beanName" property="propertyName" value="propertyValue" />
10
Purpose
Allow instantiation of Java classes without explicit Java programming (XML-compatible syntax)
Notes
Simple interpretation: <jsp:useBean id="book1" class="coreservlets.Book" /> can be thought of as equivalent to the scriptlet <% coreservlets.Book book1 = new coreservlets.Book(); %>
Purpose
Allow access to bean properties (i.e., calls to getXxx methods) without explicit Java programming
Notes
<jsp:getProperty name="book1" property="title" /> is equivalent to the following JSP expression <%= book1.getTitle() %>
12
Purpose
Allow setting of bean properties (i.e., calls to setXxx methods) without explicit Java programming
Notes
<jsp:setProperty name="book1" property="title" value="Core Servlets and JavaServer Pages" />
Example: StringBean
package coreservlets; public class StringBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }
16
17
18
20
21
22
23
This is extremely convenient for making "form beans" -- objects whose properties are filled in from a form submission.
You can even divide the process up across multiple forms, where each submission fills in part of the object.
24
Sharing Beans
You can use the scope attribute to specify additional places where bean is stored
Still also bound to local variable in _jspService <jsp:useBean id="" class="" scope="" />
Lets multiple servlets or JSP pages share data Also permits conditional bean creation
Creates new object only if it can't find existing one
25
27
31
Note:
Use different names (i.e., id in jsp:useBean) for different beans
32
34
36
37
40
41
42
45
46
47
50
51
52
Summary
Benefits of jsp:useBean
Hides the Java syntax Makes it easier to associate request parameters with Java objects (bean properties) Simplifies sharing objects among multiple requests or servlets/JSPs
jsp:useBean
Creates or accesses a bean
jsp:getProperty
Puts bean property (i.e. getXxx call) into servlet output
jsp:setProperty
Sets bean property (i.e. passes value to setXxx)
53
Questions?
54
JSP and Servlet Training Courses: http://courses.coreservlets.com JSP and Servlet Books from Sun Press: http://www.coreservlets.com