JSPs using JavaBeans

Download Report

Transcript JSPs using JavaBeans

Java WWW Week 4
Java Server Pages and beans






Format of lecture:
The “N-Tier” model
Introduction to JavaBeans
Enterprise beans, graphical beans
How JSP and JavaBeans can work together
Summary
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 1
Java WWW Week 4
The N-tier model
 Up to now we have used 2 or 3 tiers i.e. the client (presentation
layer), the http server (JSP application), possibly a DB (Storage)
 For N-tier we will use our HTTP server for java beans and JSPs
 N-tier means separation of data; application logic; presentation
 We can introduce JavaBeans as a tier - the JSP will “talk to” the
bean and the bean can handle for example access to a database
 Beans can be used by many JSPs for common functionality and
can help to slim down the JSP code
 Therefore, what are JavaBeans? They can be thought as
“middleware” but we need more detail than that……
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 2
Java WWW Week 4
Java Beans – a definition
 JavaBeans are a portable, platform independent component
model written in the Java programming language, developed in
collaboration with industry leaders, e.g. IBM. Circa 1998.
 They are “pure Java” – no platform-specific behaviour
 They enable developers to write reusable components once and
run them anywhere, benefiting from the platform independent
power of Java technology.
 See the JavaBeans homepage link below for more info
http://www.oracle.com/technetwork/java/javaee/ejb-141389.html
 Java beans are very, very useful!
 There are also what are called Enterprise Java Beans (EJBs), see
http://en.wikipedia.org/wiki/Ejb
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 3
Java WWW Week 4
JavaBeans – “nuts and bolts”




A JavaBean is developed as a .java file extension
It HAS to be compiled into a .class file extension
The name of the Bean MUST match the name of the
class
The Bean cannot be run standalone - it has to be
called by another Java program - in our case a JSP


the JavaBean has no main method (therefore it cannot run
on its own accord)
it can have variables and methods which can be called by
another Java program – in our case a JSP
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 4
Java WWW Week 4
JSP and JavaBeans Architecture
 For Tomcat, the bean must be compiled in the JDK environment
and the .class file placed in a beans folder.
 This is a dedicated folder whose path is (in your web folder)
WEB-INF\classes.
 Compiled beans should be placed into a package (i.e. a
subfolder) within the beans folder. The package is named in the
bean source code and also in the JSP using the bean.
 For e.g. mywebfolder\WEB-INF\classes\jonathansbeans
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 5
Java WWW Week 4
Example using Screen grabs
 A JavaBean called GuestBean.
 The source file would be saved as GuestBean.java
 Below shows the top part of the JavaBean code:
package jonathansbeans;
import java.sql.*;
public class GuestBean
{
String email, firstName, lastName;
etc.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 6
Java WWW Week 4
JavaBean structure
 ANY Java class can be a JavaBean provided it
follows certain conventions.
 A bean must have attributes/properties with
corresponding “get” and/or “set” methods.
 So you need at least one get and optionally a set
method.
 It follows that any existing Java class can be
“converted” to a bean (by modifying the source code
(.java) and removing the main method if it has one).
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 7
Java WWW Week 4
JavaBean structure
 A JavaBean can have what are called properties
which are essentially either a get or set prefix with a
variable name suffix – see next slide.
 Properties are manipulated using get and set access
methods:
 public <datatype> getXXX( )
 public void setXXX(<datatype> XXX)
 XXX is the attribute/property name.
 Examples follow after the next slide.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 8
Java WWW Week 4
Bean get and set syntax
 The general syntax is
 get + AttributeName
 set + AttributeName
 The attribute name will follow the naming
conventions being employed by the
development team.
 e.g. the first letter of the variable name should
be capitalised and the rest lower case.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 9
Java WWW Week 4
get method
 A get method is ALWAYS used to access a property:
public int getFieldId( )
{
return fieldId;
}
A method
 The property accessed may be a calculated value:
public double getInterest( )
{
return fieldBalance * fieldInterestRate;
}
variables
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 10
Java WWW Week 4
set method
 A property is ALWAYS set using a set method
public void setFieldBalance(double fieldBalance)
{
this.fieldBalance = fieldBalance;
}
 Calculated properties obviously do not need a
set method.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 11
Java WWW Week 4
JavaBeans – objects
 A class is a blueprint that represents some
real-world entity that we wish to model in our
system.
 This blueprint specifies the structure of the
data for the entity and a series of methods
(functions) that can act upon that data.
 To work with a class in our code, we build an
object from the class. This is like building an
office block from the blueprint.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 12
Java WWW Week 4
JavaBeans – objects
 So, on the server we could have many objects
of the JavaBean class in existence (i.e. my
use of the JavaBean would create an object,
another user’s use of the JavaBean would
create another object – two separate objects
that share a similar structure).
 Therefore we can conclude that JavaBeans
can be used in a multi-user environment.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 13
Java WWW Week 4
JSP and JavaBeans
 JSPs can use JavaBeans using a set of action
tags.
 useBean action:
 used to access a JavaBean class instance (object) on the
server
 setProperty action:
 used to set the attributes of a JavaBean class instance
(object) on the server
 getProperty action:
 used to get the value of an attribute of a JavaBean class
instance (object) on the server
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 14
Java WWW Week 4
useBean syntax
<jsp:useBean id="orderedbook"
class="jonathansbeans.Books" />
This is the full package and class specifier as used
in your bean code. The id name is entirely up to you.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 15
Java WWW Week 4
useBean scope
 useBean action tag can specify the “scope” of a bean lifecycle.
 Used to access a JavaBean utility class on the server – can have
4 values which represent the “visibility” of the bean through the
lifetime of the users request – the scope – 4 scope types exist:
 page – the default – current page i.e. the latest invocation of the
JSP.
 request – throughout the lifetime of the user’s request.
 session – until session is finished.
 application – until the server is stopped.
 The scope can be specified using the scope attribute in the
useBean action tag:
<jsp:useBean id=“something” class=“package.beanname”
scope=“request” / >
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 16
Java WWW Week 4
JSP action tags - forward
 JSPs can chain together using a forward
action tag.
 forward Action - used to forward the request to
another JSP.
 <jsp:forward page="SimpleGuestList.jsp" />
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 17
Java WWW Week 4
Forward model
 JSPs are “chained” in the Forwarding requests model.
 A JSP accepts a request from the user, possibly uses
a JavaBean and then forwards the request to another
JSP - hence the above term of chaining JSPs
together.
 This is different to the include model as there is no
primary JSP responsible for sending the response
back to the user.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 18
Java WWW Week 4
Benefits of beans?
 Separation of work and herein lies the
advantages:
 simplification.
 reducing the size of JSPs by delegating work out to
beans.
 One disadvantage of forward model is that
one JSP is responsible for the request and
another JSP takes care of the response could get messy if the chain was big.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 19
Java WWW Week 4
Summary
 It should have struck you by now that a JavaBean can
be used by many JSPs - this is good practice.
 For example, the GuestBean can service a number of
different JSPs.
 a JSP to add Guests.
 a JSP to delete Guest.
 How? Answer - by providing a method which the JSPs
can call.
Version 2.4 Nov 2012
[email protected],
Edits by nas1
Slide 20