jonathan westlake, ed: nic shulver jsp, structure and javabeans jsp models assignment context...

23
Jonathan Westlake, ed: Nic Shulver 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

Upload: william-hopkins

Post on 28-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJSP 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 workJSPs with Update SQL examples

SummaryAssignment Progress review – making sure you are

clear about what you are doing with the prototypeJava Beans – notes

Page 2: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansThe update problem

How to tackle for the assignment?What do we need to do? Understand the SQLSolution using the Guests example

We need to allow the user to select a guest recordLet the user view itAllow the user to modify it (not key however) and then write it

back to the database i.e. update itAre 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

Page 3: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansThe update solution

So, we need…The form that calls a jsp which simply displays the result from

the fetch in a set of text boxesrather than a table formatthis 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

Page 4: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansUPDATE 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_nameSET column1=value, column2=value2,...WHERE some_column=some_value

See: http://www.w3schools.com/sql/sql_update.asp

Page 5: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansDemo

Now followsWe’ve put the example in a folder on nas1/examples

called updateguestexamples_withoutbeanhttp://fcet11.staffs.ac.uk:8080/nas1/examples/update

Guests.html

Page 6: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansYour assignment

You can use the same approach to modify and update a member entry in the database

Page 7: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansSummary

You have seen a simple JSP update exampleJSPs may be linked and embeddedThis 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

Page 8: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment 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 ScriptThe 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?

Page 9: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansOrders 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

Page 10: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment 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……..

Page 11: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment progress

What should the product page be?Choices here ….it could be a simple static HTML page which

shows products with buy now buttonsA buy now hypertext link to the shopping cart 3rd party

software….example on next slideYour page would just have a few example CDs or DVDs to

demonstrate the proof of conceptHow 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

Page 12: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeans

Page 13: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment Progress

Page 14: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment 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.

Page 15: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansAssignment Progress

Alternatively you could do a more sophisticated product page….but you have not been requested to do this.

This would involve a products tableA form which retrieves the products and shows them

to the user …very similar to the update guest exampleThe user then selects a product to buyThe difference with this solution would be that the

products displayed would be dynamic i.e. from the database

Page 16: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansOrders – 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

Page 17: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJava 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

Page 18: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJava Server Pages and beans!

The “N-Tier” modelIntroduction to JavaBeans How JSP and JavaBeans can work togetherDemo

Page 19: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansThe 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……

Page 20: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJava 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)

Page 21: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJavaBeans – “nuts and bolts”

A JavaBean is developed as a .java file extensionIt HAS to be compiled into a .class file extensionThe 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 JSPthe 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

Page 22: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansJavaBeans – 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.

Page 23: Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update

Jonathan Westlake, ed: Nic Shulver

JSP, Structure and JavaBeansSummary

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.