JavaBeans
JavaBeans
Store data/object at
server site Session object
Tracking But, SessionID is
Session stored at client
Mechanism
Located
at client’s Cookies
system fie
Store
data/object at
client site Located
URL rewriting
at query
string
Day 7, 8, 9 – Search
Break Down
Web/App JSP
Server
1. Click Login
2. Check Login
4. Response the
result page
Java
Beans/
Welcome page/ BLO
invalid page 3. Call
4. Query DB
DAO
DB
Client Server
JSP Standard Actions
Java Beans
• JavaBeans technology is the component architecture for the Java 2
Platform, Standard Edition (J2SE).
• JavaBeans technology is based on the JavaBeans specification.
• Components (JavaBeans) are reusable software programs that you
can develop and assemble easily to create sophisticated applications.
– Are reusable components which define the interactivity of Java objects
– Are reusable software components that work independently on a workstation
and with a set of other distributed components.
• Encapsulate state and behavior of software component
• The different point between Java Bean and Java class
– Java Bean implemented from Serializable
• The process of create a copy of an object suitable for passing to another
object/ package classes into streams of bytes that are transmitted
through networks or saved to the disk
JSP Standard Actions
Java Beans
• In order to function as a JavaBean class, an object class must obey
certain conventions about method naming, construction, and
behavior.
– These conventions make it possible to have tools that can use, reuse, replace,
and connect JavaBeans.
• A Bean a simple Java Class that follows certain coding conventions
– Bean class should always use a package name
– Bean class must have a public no-argument constructor
– The properties of bean (persistence) is not declared “public”. They are
accessed through getter and setter methods.
– Public getter method is used to retrieve the properties of a bean class
– Public setter method is used to set the properties of a bean class
• Notes:
– The first character of each property should name in lower case then the
accessor methods are used along with property name with the first character
of each word in upper case (Ex: length – getLength and setLength)
– The dataType of properties is boolean then the getter method is isXxx instead of
getXxx
JSP Standard Actions
Java Beans
• A JSP page accesses Java Beans using tag action and
gets the result of processing without how to JavaBeans
implementation and process
• Java Bean tags are combined with JSP elements
• Java Bean tags are translated into single java Servlet
classes on the server
• JSP technology directly supports using JavaBeans
components with JSP language elements
JSP Standard Actions
Standard Actions
• An alternative to inserting java code into designed presentation
page
• Are performed when a browser requests for a JSP page
• Are used for
– Forwarding requests and performing includes in page
– Embedding the appropriate HTML on pages
– Interacting between pages and JavaBeans
– Providing additional functionality to tag libraries
• Syntax: <prefix:actionName att=“…”>...</prefix:actionName>
• Some properties
– It use “jsp” prefix
– The attributes are case sensitive
– Value in the attributes must be enclosed in double quotes
– Standard actions can be either an empty or a container tag
• A JSP provides 03 tags that use interact with JavaBean
– jsp:useBean, jsp:setProperty, jsp:getProperty
JSP Standard Actions
The <jsp:useBean> tag
• Is used to locate or instantiate a JavaBeans component
• First tries to locate an instance of the Bean otherwise it instantiates
the Bean from a class
• To locate or instantiate the Bean, the <jsp:useBean> follows the
following steps:
– Attempts to locate a Bean (means attribute containing object) within the scope
– Defines an object reference variable with the name
– Stores a reference to it in the variable, if it retrieves the Bean
– Instantiates it from the specified class, if it cannot retrieve the Bean
• Syntax
<jsp:useBean id=“<identifier>” class=“<class name>” [scope = “scope name”]/>
– id: is used to name the attribute name, to refer to the Bean instance in specify
scope
– class: a class name implementing a Bean processing.
– scope: the lifespan of a bean (sharable). Default value is page
JSP Standard Actions
The <jsp:useBean> tag
• Use scriptlet element to declare Java Bean:
<%
className id = (className) scope.getAttribute(“identifier”);
if (id == null) {
id = new className();
scope.setAttribute (“identifier”, id);
}
%>
• Ex
– <jsp:useBean id=“book1” class=“store.book”/>
– similar to
<%
store.book book1 = (store.book) pageContext.getAttribute(“book1”);
if (book1 == null) {
book1 = new store.book();
pageContext.setAttribute(“book1”, book1);
}
%>
JSP Standard Actions
• Casting The <jsp:useBean> tag
<jsp:useBean id=“<identifier>” class=“<class name>” type = “<dataType>”
[scope = “scope type”]/>
– type: Java – DataType
• JSP Scriptlet element
<%
dataType id = (dataType) scope.getAttribute(“identifier”);
if (id == null) {
id = (dataType) (new className());
scope.setAttribute (“identifier”, id);
}
%>
• Ex
– <jsp:useBean id=“book1” class=“store.book” type=“library.magazine” />
– Similar to
<% library.magazine book1 = (library.magazine) pageContext.getAttribute(“book1”);
if (book1 == null) {
book1 = (library.magazine) (new store.book());
pageContext.setAttribute(“book1”, book1);
} %>
JSP Standard Actions
The <jsp:useBean> tag
• Other syntax
<jsp:useBean …> statement </jsp:useBean>
– Ex
<jsp:useBean id="count" class="ABean.AccessBean" scope="application">
<jsp:setProperty name="count" property="firstPage" value="ATest.jsp" />
</jsp:useBean>
• Notes:
– If using some specified symbol in command, the symbol “\” should put such as ‘
(\’); “ (\”), \ (\\), % (\%), ...
– The JSP scriptlets use id as a variable.
JSP Standard Actions
The <jsp:getProperty> tag
• Retrieves a bean property value using the getter methods and
displays the output in a JSP page
• Before using <jsp:getProperty> you must create or locate a bean
with <jsp:useBean>
• Drawback
– Fails to retrieve the values of an indexed property
– Fails to directly access enterprise beans components
• Syntax
<jsp:getProperty name=“<identifier>” property=“<attr name>” />
– name: the identifier declared in jsp:useBean
– property: the property name is implemented in Java Bean
• Use scriptlet command
<%= <identifier>.getXxx() %>
• Ex
<jsp:getProperty name=“book1” property=“title”/>
Similar to <%= book1.getTitle()%>
JSP Standard Actions
The <jsp:setProperty> tag
• Sets the value of the properties in a bean, using the bean’s setter
methods
• Before using <jsp:setProperty> you must create or locate a bean
with <jsp:useBean>
• Syntax
<jsp:setProperty name=“<identifiers>” property=
“<attr name>” value=“<const/ expression>” />
• Use scriptlet command:
<%
id.setXxx(<value>);
scope.setAttribute(“identifier”, id);
%>
• Ex: <jsp:setProperty name=“book1” property=“title” value=“JSP Book” />
Similar to <% book1.setTitle(“JSP Book”);
pageContext.setAttribute(“book1”, book1);
%>
JSP Standard Actions
The <jsp:setProperty> tag
• Assign a expression to action setProperty
– String sMsg = request.getParameter("sms");
– <jsp:setProperty name="msg" property="message" value="<%= sMsg %>" />
• Use param properties in setProperty: receive an inputted value
from a request (from other JSP page, application, or URL)
– <jsp:setProperty name="msg" property="message" param=“message” />
• To set values to whole bean properties, the symbol “*” is assigned
to setProperty
<jsp:setProperty name="msg" property=“*" />
• Notes:
– The Bean Action is executed only if all of properties is assigned values
because the engine does not assign a null value with lacked properties.
– In some web server, the exceptions are thrown if a property value is assigned
to double
– The converted mechanism does not ensure a valid value for property
(instead of manual casting)
– The parameters name should similar to properties name implemented in
Bean
JSP Standard Actions
Steps in design Web Application
following MVC patterns
• Step 1
– create View
• Step 2
– Depends on View, determine some persistence controls that are
implemented as the Java Bean (Model) ‘s properties
– Implement the Java Bean with accessor methods and some
utilitie methods that are used to query the properties of Java Bean
• The methods are designed following the OO standard
• Step 3
– Create servlet (Controller)/JSP page combining View & Model
MVC1
Requirements
JSP Standard Actions
• page
Scope of JavaBeans
– Accessible for current page. The life span tills the current page displayed
– The information is stored in pageContext (get values through the getAttribute
method) – default scope
• application
– Current and any successive request from the same Web Application. Global to
all JSP and servlet.
– The information is stored in ServletContext.
– The life span is Application
• session
– The information is stored in HttpSession combined current request (get
values through the getValues method of Session interface (Implicit Object)).
– The life span tills the session destroyed, time out, or Web browser closed.
• request
– Accessible for current request (get values through the getAttribute methods)
– The information is stored in ServletRequest
– The life Span tills the request processed and the response is sent back to the Web
browser
JSP Standard Actions
Scope of JavaBeans
• Notes:
– Using JavaBeans with Session or Application scope, a
JSP page must declare a tag action jsp:useBean with a
same id and class name.
– JSP, JSP/ Servlet engine defines scope in executing Web
application, if the instance bean existed, the engine does
not instantatiate new one (the tag jsp:useBean is
omited) to execute Bean’s methods. Otherwise, the
engine create a new instance.
Dispatching Mechanisms
The <jsp:include> tag
• Can be used to include the response from another
file within the JSP page output.
• To incorporate the content or insert a file from
another page into current page
• Syntax
<jsp:include page=“…” flush=“true” />
• The file whose response should be included has to
reside somewhere in your web application but
doesn’t have to be present until the page is
actually requested
Dispatching Mechanisms
The <jsp:include> tag – Example
Dispatching Mechanisms
The <jsp:include> tag – Example
Dispatching Mechanisms
The <jsp:include> tag – Example
Dispatching Mechanisms
The <jsp:include> tag – Example
Dispatching Mechanisms
<jsp:include> vs. <%@ include …%>
<jsp:include>
Dispatching Mechanisms
<jsp:include> vs. <%@ include …%>
servletContext
scopeObj header headerValues
request
pageScope
response requestScope
sessionScope
session
applicationScope
EL Implicit Objects
Objects Descriptions
Q&A
Next Lecture
• How to remove completed Scripting Element
(Java Code) in JSP (View)? Complete the
View of MVC 2 Design Pattern
– JSTL
• How to build the data grid tag library using
in JSP?
– Tag Libraries
• Model
• Classical, Simple, and Handles
• How to implement the custom Tag Lib and use it in JSP
Next Lecture
Day 1, 2, 3, 4, 5, 6 – Login
Servlet, JDBC
Day 7, 8, 9 – Search
Break Down
Browse to directory
containing taglib.
Should not be
changed
Coercion