Transcript lecture
JSP, Structure and JavaBeans
JSP models
Assignment context “Register as a member of the
site/show member details/update member details”
JSP models
JSPs calling other JSPs i.e. breaking up work
JSPs with Update SQL examples
Summary
Assignment Progress review – making sure you are
clear about what you are doing with the prototype
Java Beans – notes
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
The update problem
How to tackle for the assignment?
What do we need to do? Understand the SQL
Solution using the Guests example
We need to allow the user to select a guest record
Let the user view it
Allow the user to modify it (not key however) and then write it
back to the database i.e. update it
Are there any aspects we don’t wish to allow the user to do?
Yes – they cannot alter the key, in this case both the email
address and lastname
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
The update solution
So, we need…
The form that calls a jsp which simply displays the result from
the fetch in a set of text boxes
rather than a table format
this allows the user to do something with the data
We can allow the user to change the content of the text box
for firstname and then call a jsp to do the update to the
database
then call another jsp to display the confirmation or return to
our starting point
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
UPDATE SQL
A JSP which can use the parameters from the update
form and call a JSP to update the database
Use the Update SQL command to modify existing
records in a table
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
See: http://www.w3schools.com/sql/sql_update.asp
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Demo
Now follows
We’ve put the example in a folder on nas1/examples
called updateguestexamples_withoutbean
http://fcet11.staffs.ac.uk:8080/nas1/examples/update
Guests.html
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Your assignment
You can use the same approach to modify and update
a member entry in the database
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Summary
You have seen a simple JSP update example
JSPs may be linked and embedded
This type of web design starts to look like a service
oriented structure i.e. JSPs that will service other
JSPs or HTML forms given the appropriate
information
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment progress
You should have by now thought of a sequence of
events that the prototype needs to follow ……your
work in UML should help you with this
Use Case Script
The membership aspect you can tackle with the JSPs
we have reviewed over the last two weeks …add a
member; view and update a member
How much detail do we need to hold about a
member?
Email address; name ….do we need anything else?
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Orders Process
You need to understand how the 3rd party shopping
cart works – what will it accept? …true OO – we don’t
need to know how it is built but need to know what
parameters to send it
What do we need to send the shopping cart? The
product selected by the member; and other
information …product desc; price
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment progress
So how to do this?
There needs to be a member login form where we
check they are a member….if yes they get directed to
a product page……that is what the membership is for
– to allow access – see following example which you
will need to augment……..
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment progress
What should the product page be?
Choices here ….it could be a simple static HTML page which
shows products with buy now buttons
A buy now hypertext link to the shopping cart 3rd party
software….example on next slide
Your page would just have a few example CDs or DVDs to
demonstrate the proof of concept
How the buy now button links to the shopping cart is
explained in the Mal’s E-Commere help but we will show you
on the screen to be clear
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment Progress
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment Progress
Alternative to the HREF example is to use a FORM using POST
instead of GET
What this means is you link to the cart using a HTML form
instead of an ordinary hyperlink.
<FORM METHOD="POST"
ACTION="http://fcet11.staffs.ac.uk:8080/nas1/shopcart/add.jsp">
<INPUT TYPE="HIDDEN" NAME="shopID" VALUE="ab12345">
<INPUT TYPE="HIDDEN" NAME="product" VALUE="North
Atlantic Marine Chart">
<INPUT TYPE="HIDDEN" NAME="prodID" VALUE="987">
<INPUT TYPE="HIDDEN" NAME="price" VALUE="29.50">
<INPUT TYPE="SUBMIT"> </FORM>
You can add something to the cart using a drop down list, radio
buttons or any HTML form tag.
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Assignment Progress
Alternatively you could do a more sophisticated
product page….but you have not been requested to
do this.
This would involve a products table
A form which retrieves the products and shows them
to the user …very similar to the update guest example
The user then selects a product to buy
The difference with this solution would be that the
products displayed would be dynamic i.e. from the
database
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Orders – use of shopping cart
Add order – via shopping cart. An order can be for
more than one item
Deleting orders – via shopping cart – just make sure
all quantities are zero and this will empty the shopping
cart
Update order – via shopping cart – change quantity or
remove item by zero quantity
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Java Beans
What now follows are notes and a demo of
component technology …called Java Beans
No need to do this for the assignment (purely
optional) but an understanding is necessary for the
exam
The “OO ness” of Beans is the interesting aspect
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
Java Server Pages and beans!
The “N-Tier” model
Introduction to JavaBeans
How JSP and JavaBeans can work together
Demo
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
The N-tier model
Up to know we have used 2 tier i.e the client (presentation) and
the http server (JSP application)
For N-tier - where we are heading? - we will use our HTTP server
for java beans and JSPs
N-tier means separation of data; application logic; presentation –
Why is this good software engineering? Discussion ……..
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……
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
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 enable developers to write reusable components
once and run them anywhere, benefiting from the
platform independence power of Java technology.
See the JavaBeans homepage link below for more
info http://java.sun.com/products/javabeans/
Java beans are useful and at Enterprise level they are
called Enterprise Java Beans (EJBs)
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
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
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
JavaBeans – objects
A JavaBean is a Java class, a blueprint that represents some
real-world entity that we wish to model in our system.
It 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.
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.
Jonathan Westlake, ed: Nic Shulver
JSP, Structure and JavaBeans
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.
Jonathan Westlake, ed: Nic Shulver