JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java...
Transcript of JAVABEANS IN JSPsite.iugaza.edu.ps/hmasry/files/Lab9-JavaBeans.pdf · Beans are regular Java...
In the name of Allah
Islamic University of Gaza
Faculty of Engineering
Computer Engineering Department
ECOM 5049
Advanced Internet Technology Lab
Lab # 9
JAVABEANS IN JSP
Eng. Haneen El-masry
2013
Advanced Internet Technology Lab
Eng. Haneen 2
Objectives
Understanding JavaBeans.
Understanding the benefits of beans.
Creating beans.
Accessing bean properties.
Explicitly setting bean properties.
Automatically setting bean properties from request parameters.
Sharing beans among multiple servlets and JSP pages.
What are JavaBeans?
Beans are regular Java classes that follow some simple conventions defined by the JavaBeans
specification:
A bean class must have a zero-argument (default) constructor.
A bean class should have no public instance variables (fields).
Persistent values should be accessed through methods called getXxx and setXxx.
Why You Should Use Accessors, Not Public Fields ?
You can put constraints on values.
You can change your internal representation without changing the interface.
You can perform arbitrary side effects.
Using Beans in JSP
jsp:useBean: It can either build a new bean or access a preexisting one.
<jsp:useBean id="beanName" class="package.Class" />
It can be thought of as equivalent to the scriptlet:
<% package.Class beanName = new package.Class(); %>
But jsp:useBean has two additional advantages:
- It is easier to derive object values from request parameters.
- It is easier to share objects among pages or servlets.
Advanced Internet Technology Lab
Eng. Haneen 3
jsp:getProperty: Allow access to bean properties.
<jsp:getProperty name="beanName" property="property" />
It is equivalent to the JSP expression:
<%= beanName.getProperty() %>
jsp:setProperty: Allow setting of bean properties.
<jsp:setProperty name="beanName" property="property" value="value" />
It is equivalent to the scriptlet:
<% beanName.setProperty("value"); %>
Example:
- JavaBean
Advanced Internet Technology Lab
Eng. Haneen 4
- JSP Page
- Output
Beans with Forms
To assign bean properties with request parameters, there are three cases:
1- Explicit Conversion & Assignment.
2- Associating Individual Properties with Input Parameters.
3- Associating All Properties with Input Parameters.
Advanced Internet Technology Lab
Eng. Haneen 5
Example:
- JSP Form:
- Java Bean:
Advanced Internet Technology Lab
Eng. Haneen 6
- SaleEntry1.jsp: Explicit Conversion
Advanced Internet Technology Lab
Eng. Haneen 7
- The Output:
Advanced Internet Technology Lab
Eng. Haneen 8
- SaleEntry2.jsp: Individual Parameters Associating:
- The Output
Advanced Internet Technology Lab
Eng. Haneen 9
- SaleEntry3.jsp: All Properties Association
- The Output
Advanced Internet Technology Lab
Eng. Haneen 10
JavaBeans Sharing
You can specify a scope attribute that associates the bean with more than just the current page.
If beans can be shared, it is useful to obtain references to existing beans, rather than always
building a new object. So, the jsp:useBean action specifies that a new object is instantiated only
if there is no existing one with the same id and scope.
<jsp:useBean id="…" class="…" scope="…" />
Values of the scope attribute:
1- page (<jsp:useBean … scope="page"/> or <jsp:useBean…>)
It is a default value. Bean object should be placed in the PageContext object for the duration of
the current request.
2- request (<jsp:useBean … scope="request"/>)
Bean object should be placed in the HttpServletRequest object for the duration of the current
request.
3- session (<jsp:useBean … scope="session"/>)
Bean will be stored in the HttpSession object associated with the current request.
4- application (<jsp:useBean … scope="application"/>)
Bean will be stored in ServletContext that is shared by all servlets and JSP pages in the Web
application.
Conditional Bean Operations
To execute some statements only if a new bean is created, not if an existing bean is found, use
this form of the action:
<jsp:useBean ...>statements</jsp:useBean>
Instead of the form:
<jsp:useBean .../>
Advanced Internet Technology Lab
Eng. Haneen 11
Examples:
- JSP Form
- JavaBean
Advanced Internet Technology Lab
Eng. Haneen 12
- Page Scope
The Output
1st Request
Advanced Internet Technology Lab
Eng. Haneen 13
2nd request
- Request Scope
BakedBeanDisplay.jsp
Advanced Internet Technology Lab
Eng. Haneen 14
BakedBeanDisplay2.jsp
The Output
Advanced Internet Technology Lab
Eng. Haneen 15
- Session Scope
The Output
1st Request.
2nd request that is in the same session.
Advanced Internet Technology Lab
Eng. Haneen 16
3rd request from another session.
- Application Scope
Advanced Internet Technology Lab
Eng. Haneen 17
The Output
1st Request
2nd Request in the same session
3rd request from different session.