Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans...

138
Chapter 9 1 © copyright Janson Industries 2011 Java Design Concepts JSP’s MVC Beans JSP Tags

Transcript of Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans...

Page 1: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 1© copyright Janson Industries 2011

Java Design Concepts

■ JSP’s

■MVC

■Beans

■ JSP Tags

Page 2: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 2© copyright Janson Industries 2011

Java Design Concepts

■How do you like having to type in all that HTML for servlet?

■Make a few source code entry mistakes?

■Page Designer and JSP’s make it easier

Page 3: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 3© copyright Janson Industries 2011

JSP Example

▮ Java Server Pages are comprised of HTML with embedded Java statements, JSP tags, or JSTL tags HTML code

Java codeHTML codeJava codeHTML code▮ JSP Java statement types

▮ Scriptlet▮ Expression▮ Declaration▮ Directive

Page 4: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 4© copyright Janson Industries 2011

Java Statement Tags

▮ Java scriptlet syntax: <% java statement %>

▮ Java expression syntax: <%= java statement %>

▮ Java declaration syntax: <%! java statement %>

▮ Java directive syntax: <%@ java statement %>

Page 5: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 5© copyright Janson Industries 2011

JSP Example<HTML>

<HEAD>

<TITLE>

JSP Example

</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<% for (int i = 0; i < 5; i++) { %>

<P>Howdy from the JSP example.</P>

<% } %>

<P>The current date and time is:

<%= new java.util.Date() %>

</P>

</BODY>

</HTML>

A lot less code than a

servlet!

Scriptlet

Expression

Scriptlet

Page 6: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 6© copyright Janson Industries 2011

Java Statement Tags

▮ Java declaration allows you to define a method:

<%! private void error() {System.out.println(“Error,

Error!”); } %>

▮ Java directive allows you to specify JSP wide instructions:

<%@page import = “java.io.*” %>

Page 7: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 7© copyright Janson Industries 2011

JSP Example

▮ Problem with Java statements – not XML compliant

▮ Use JSP tags:

<jsp:scriptlet> for (int i = 0; i < 5; i++) { </jsp:scriptlet>

<jsp:expression> new java.util.Date() </jsp:expression>

<jsp:declaration> private void error() {System.out.println(“Error, Error!”); }

</jsp:declaration>

<jsp:directive.page import = "java.io.*" />

Page 8: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 8© copyright Janson Industries 2011

To Create, click on MyWeb, File, New, Web Page

Give JSP a name (JSPEx), click on JSP in the Basic Templates area then click Finish

Page 9: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 9© copyright Janson Industries 2011

Initial code inserted by RAD

Page 10: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 10© copyright Janson Industries 2011

In Navigation Pane, right click JSPEx and select Run on

Server

Cut and paste example code (from earlier slide) over RAD code

Page 11: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 11© copyright Janson Industries 2011

Do same using JSP tags not Java Statement tagsSolution

Page 12: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 12© copyright Janson Industries 2011

JSP

▮ JSP’s are actually converted into Servlets by the JSP container

▮ For instance, in Tomcat, for each .jsp file there is a .java and .class file created in

/Tomcat 5.0/work/standalone/localhost/test

▮ I.e. if I created Studio.jsp, Tomcat will create the files Studio$jsp.java & Studio$jsp.class

Page 13: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 13© copyright Janson Industries 2011

So, really, the advantage to JSP’s is ease of

construction for the programmer

Page 14: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 14© copyright Janson Industries 2011

JSP

▮ For JSPs, RAD generates a class file with the same name as the JSP preceded by an underscore

▮ _JSPEx.class

▮ The class file is stored in the WAR

▮ RAD doesn't provide access or even display the file (as far as I can tell!)

Page 15: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 15© copyright Janson Industries 2011

JSP

But using Computer you can drill into the server and find it

Page 16: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 16© copyright Janson Industries 2011

RAD JSP

▮ Design pane provides a GUI to build JSP’s

▮ Drop and drag Web page GUI components

▮ Property definition panes

▮ Format Java statements

▮ Insert predefined routines (scriptlets)

▮ For example, create a JSP called RAD

Page 17: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

© copyright Janson Industries 2011Chapter 9 17

Insert this text

RAD JSP

Page 18: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 18© copyright Janson Industries 2011

To Insert a Scriptlet, position cursor on page, click JSP, Insert Scriptlet

Page 19: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 19© copyright Janson Industries 2011

Display the Properties view of the Scriptlet

Page 20: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 20© copyright Janson Industries 2011

In properties pane specify Java codeIn this example, inserted the beginning of a

loop

Page 21: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 21© copyright Janson Industries 2011

Insert another scriptlet with the rest of the for

loop

Page 22: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 22© copyright Janson Industries 2011

Run on the server

Should have a <br> tag to insert a line in each loop

Page 23: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

© copyright Janson Industries 2011Chapter 9 23

RAD JSP

Save the code

Page 24: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 24© copyright Janson Industries 2011

Refresh the browser

Page 25: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 25© copyright Janson Industries 2011

▮ Need to be a Java programmer to implement▮ Cumbersome syntax▮ Knowledge of object oriented

programming

▮ No coding standards ▮ for (int i = -200 ; i < -100; i=i+20) {

▮ JSTL Tags are the solution (more later)

Java Statement Problems

Page 26: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 26© copyright Janson Industries 2011

JSP In Class Assg

▮ Create a JSP that uses a loop to print the system date five times

Page 27: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 27© copyright Janson Industries 2011

Java Design Concepts

■MVC design pattern for server-based applications

♦ Model

♦ View

♦ Controller

Page 28: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 28© copyright Janson Industries 2011

MVC

■ Model – classes that access/store data and perform business functions

■ View – classes that:♦ Enable users to input data/instructions♦ Format and display results

■ Controller – classes that manage the model and view

Page 29: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Means creates

Chapter 9 29© copyright Janson Industries 2011

Java MVC Example

DB

Data andinstructions

Data

View

User

Data andinstructions

Applicationresults

DataModel

Data

View

Controller

Page 30: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 30© copyright Janson Industries 2011

MVC

■ You can see the MVC pieces in the client app

■ However:

♦ Separation of pieces difficult with client technology

♦ Initially, user must interact directly with Controller

Page 31: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 31© copyright Janson Industries 2011

Customer

CustomerApp Model

CustomerFrameUser

run CustomerAppinstruction

EnterCustInfocreates

EmptyFrame

Frame (with data)& Display instruction

data

CustomerFramewith data

Controller

1

2

3

4

5

8

6

View

cust

Client App MVC Example

data 7

View Controller

Page 32: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 32© copyright Janson Industries 2011

Server Side MVC Example

JavaBean

JavaBean

DB

Controller

Model

JSP’s

View

UserHTML (response)

WebPages

ViewServlet

Data

Data

Data

Data

Data andinstructions

Page 33: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 33© copyright Janson Industries 2011

Employee MVC Example

EmpExtractor EmployeeEmpServlet

Controller Model

DispEmpInfoJSP

View

User

HTML (response)

RequestResponse

DispEmpGrossJSP

DispEmpTaxAmtJSP

Request

Employee

View

EnterEmpInfoForm

Data

Data

Page 34: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 34© copyright Janson Industries 2011

Employee Example

■ EmpServlet acted as the Controller

■ The JSPs and EmpExtractor were the View

■ Employee was the Model

■ The View created the Model

Page 35: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 35© copyright Janson Industries 2011

MyWeb Example

Func2Servlet

Customerobject

FuncServlet

co

CustServlet

User

Functions.html

SaleServlet

Data

Cust orSale.html

Cust.html

HTML (response)With Cust info

Sale.html

HTML (response)With Sale info

ControllerModel

View

requestresponse

coData

Page 36: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 36© copyright Janson Industries 2011

Java MVC Example

■ The current thinking is to separate the GUI from the programming logic

♦ Have “graphic designers” create the View (e.g. html and html of the JSPs)

♦ Programmers define the controller (servlets) & business logic (Model) as java beans

♦ JSPs access java beans

►No java code in JSPs, use tags to access beans

Page 37: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 37© copyright Janson Industries 2011

MVC Advantages

■ Separating html development from program development cuts down on

♦ Duplicate programming code (in web page, legacy interface, new interfaces, etc.)

♦ Errors

■ Cheaper “resources” develop interface rather than the more expensive programming personnel

Page 38: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 38© copyright Janson Industries 2011

MVC Advantages

■ Separating business logic and controller from interface means:

♦ New interfaces can be easily added►Web Services►Text messaging►EDI►Telephony

♦ Changes to DBMS only affect Model

Page 39: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 39© copyright Janson Industries 2011

Java MVC Example

Data andinstructions

Data

View

User

Data andinstructions

Applicationresults

DataModelView

Controller

User

Phone View

Page 40: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 40© copyright Janson Industries 2011

Java MVC Example

■ Many shops make legacy code part of the model and create servlets and JSPs for view and controller portion

■ Some shops recode all the model/legacy in Java

Page 41: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 41© copyright Janson Industries 2011

What is a Java Bean

■ A convention/standard for classes

♦ A public Java class

♦ Has a default null constructor

■ Usually has getter/setter methods for each private bean variable (i.e. each property) defined as follows:

♦ public VariableType getVariableName()

♦ public void setVariableName (VariableType x)

Page 42: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 42© copyright Janson Industries 2011

What is a Java Bean

■ But guess what?

■ On the server, you can define (just about) any class as a bean

♦ Just won’t be able to access the properties if you don’t have getters and setters

Page 43: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 43© copyright Janson Industries 2011

Server Beans

■ Many categories of Server beans:

♦ Entity – associated with a DB table, EJB

♦ Session – last for length of session (duration of browser-server communication)

♦ Request – goes with request

►(Also Page and Application)

■ Can be created/defined by either JSPs or Servlets

Page 44: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 44© copyright Janson Industries 2011

Server Beans

■ The Servlet or JSP has to: ♦ Identify the bean

■ Identifying the bean means♦ Giving the bean a name♦ Tying the bean to a Java class/object♦ Defining a bean type/scope

■ Once created, Servlets or JSPs can: ♦ Set a bean parameter/property ♦ Get a bean result/property

Page 45: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 45© copyright Janson Industries 2011

JSP Tags

■ In servlet, create object and tie to session (or request, or page, or app) by defining object as an attribute of the session:

Customer Cust = new Customer(); HttpSession sess = req.getSession();

sess.setAttribute("CustBean", Cust);

■ In JSPs, Bean tags used to work with beans: <jsp:xxx> </jsp:xxx>

■ Identify a bean and tie to a java class file with a useBean tag:

<jsp:useBean id=“CustBean“ class=“Customer“>

</jsp:useBean>

Page 46: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 46© copyright Janson Industries 2011

Accessing a Bean in a Servlet

■ Set: CustBean.setCustName(req.getParameter(“CustNameTF"));

■ Get:

CustBean.getContactPerson();

■ Because it’s a session bean, any servlet invoked during browser sess can access

Page 47: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 47© copyright Janson Industries 2011

■ Set and get bean properties with the Bean tags getProperty and setProperty♦ Name is name of bean

♦ Property is property name

■ setProperty used to set property value

■ getProperty used to get results

<jsp:setProperty name=“CustBean" property=“custName” param=“CustNameTF”/>

<jsp:getProperty name=" CustBean " property=“contactPerson" />

Accessing a Bean in a JSP

Page 48: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 48© copyright Janson Industries 2011

Java Bean Example

■ Servlet code: CustBean.setCustName(req.getParameter(“CustNameTF"));

■ Same as jsp tag:

■ Servlet Code: CustBean.getContactPerson();

■ Same as jsp tag:

<jsp:setProperty name=“CustBean" property=“custName” param=“CustNameTF”/>

<jsp:getProperty name=" CustBean " property=“contactPerson" />

Page 49: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 49© copyright Janson Industries 2011

Java Bean Example

▮ Of course, class must have set/get methods matching the property names

▮ public String getContactPerson(){...}

Page 50: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 50© copyright Janson Industries 2011

Java Bean Sale Example

When data entered into Sale.html, want to see....

Page 51: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 51© copyright Janson Industries 2011

Notice the formatting: date & time, line for each item

Java Bean Sale Example

...total in the Sale.jsp

Page 52: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 52© copyright Janson Industries 2011

Java Bean Sale Example

▮ Need a new class called Tx (transaction)

▮ Stores all sale info▮ item, qty, price, customer name

▮ Calculates total $ for sale

▮ Returns receipt info (getReceipt method)

▮ The new servlets, bean, and JSPs…

Page 53: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 53© copyright Janson Industries 2011

Sale Example

SaleServlet

Txobject

FuncServlet

TxBean

User

Functions.html

Sale.jsp

Creates

Cust orSale.html

Sale.html

HTML (response)with receipt info

ControllerModel

View

Redirects ReceiptInfo

Sale info

Page 54: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 54© copyright Janson Industries 2011

Java Beans and Servlets

▮ To create the bean in Sale servlet

▮ Create a Tx object

▮ Get a Session object from the request object

▮ Define the Tx object as a session attribute▮ TX object is a session bean

Page 55: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 55© copyright Janson Industries 2011

Tx object

▮ Need private variables for properties

▮ Then getters and setters for each property

private StringBuffer custName;

private StringBuffer itemName;

private int qty;

private double price;

Page 56: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 56© copyright Janson Industries 2011

Tx object▮ Will overload all the setters to

accept Stringspublic void setPrice(String d) {

this.setPrice(Double.parseDouble(d));}

public void setQty(String i) {

this.setQty(Integer.parseInt(i));}

public void setCustName(String string) {

StringBuffer sb = new StringBuffer(string);

this.setCustName(sb);}

public void setItemName(String string) {

StringBuffer sb = new StringBuffer(string);

this.setItemName(sb);}

Page 57: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 57© copyright Janson Industries 2011

Java Bean Example

▮ We won’t build the getReceipt method yet

▮ Instead we’ll CALTAL▮ Create Sale servlet to:

▮ Create Tx object and set the CustName property value (from the request)

▮ Define the bean TxBean▮ Redirect to Sale.jsp

▮ Create Sale.jsp to▮ Retrieve the customer name from TxBean

(using tags)▮ Display customer name

Page 58: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 58© copyright Janson Industries 2011

CALTAL Sale

Txobject

TxBean

User Sale.jsp

Creates

Sale.html(with all info)

HTML (response)with CustName

ControllerModel

View

CustName

CustName

Sale(Servlet)

Redirects

Page 59: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 59© copyright Janson Industries 2011

Java Bean Example

▮ Sale servlet:▮ 1: Create Tx object assign to variable txBean ▮ 2: Set the CustName property▮ 3: Define TxBean as a session bean▮ 4: Redirect to Sale jsp

import javax.servlet.http.HttpSession;

Tx txBean = new Tx();

txBean.setCustName(req.getParameter("CustName"));

HttpSession sess = req.getSession();

sess.setAttribute("TxBean", txBean);

resp.sendRedirect("Sale.jsp");

3:1:

2:

3:

3:

4:

Page 60: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 60© copyright Janson Industries 2011

Java Bean Exampleimport java.io.IOException;

: : : : :

import javax.servlet.http.HttpSession;

: : : : :

import javax.servlet.http.HttpServletResponse;

public class Sale extends HttpServlet implements Servlet {

Tx txBean = new Tx();

public void doGet …{…}

public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

txBean.setCustName(req.getParameter("CustName"));

HttpSession sess = req.getSession();

sess.setAttribute("TxBean", txBean);

resp.sendRedirect("Sale.jsp");

}

}

Page 61: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 61© copyright Janson Industries 2011

Java Bean Example▮ In Sale.jsp let RAD generate bean tags to:

▮ Retrieve the cust name from TxBean

▮ Display customer name

Create JSP, insert textThen click:

JSP, Insert Get Property

Page 62: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 62© copyright Janson Industries 2011

Notice property name begins with a lower case

letter

Page 63: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 63© copyright Janson Industries 2011

Page 64: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 64© copyright Janson Industries 2011

Page 65: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 65© copyright Janson Industries 2011

Must tie Sale.html to Sale servlet

Select the form

In Properties view,

click action button

Select servlet

Page 66: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 66© copyright Janson Industries 2011

Select Sale

Page 67: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 67© copyright Janson Industries 2011Time to test

Also, make sure Post is selected

Page 68: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 68© copyright Janson Industries 2011

Run Sale.html on server

Enter a customer nameClick the Submit button

Page 69: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 69© copyright Janson Industries 2011

Connections all work!! Now:

Change Sale servlet to set all properties

Add methods to Tx to:calc totalreturn formatted receipt

Change Sale.jsp to display receipt

Page 70: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 70© copyright Janson Industries 2011

txBean.setItemName(req.getParameter("ItemName"));

txBean.setPrice(req.getParameter("Price"));

txBean.setQty(req.getParameter("Qty"));

▮ Change Sale servlet to set all properties▮ Notice Strings are passed for all parms

▮ Add function to Tx to:▮ calc total: getTotal()▮ return formatted receipt: getReceipt()

Java Bean Example

Page 71: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 71© copyright Janson Industries 2011

Java Bean Example

▮ getTotal() not too tough

public double getTotal(){

double total = price * qty * 1.06;

return total;

}

Page 72: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 72© copyright Janson Industries 2011

Java Bean Example

▮ getReceipt() a little more involved▮ When this info inserted

▮ Want to see…

Page 73: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 73© copyright Janson Industries 2011

Notice the formatting: date & time, line for each item

Java Bean Example

Page 74: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 74© copyright Janson Industries 2011

Java Bean Example

▮ Tx needs:

▮ Date and time formatters▮ Currency formatter▮ Date object▮ Date and time properties w/ setters and

getters▮ Date and time properties must be initialized▮ getReceipt will generate the HTML for the

formatting

Page 75: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 75© copyright Janson Industries 2011

Java Bean Example

▮ Tx needs:▮ Date and time formatters▮ Currency formatter▮ Date object

import java.text.DateFormat;

import java.text.NumberFormat;

import java.util.Date;

private Date d = new Date();

private DateFormat dfMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);

private DateFormat tfShort = DateFormat.getTimeInstance(DateFormat.SHORT);

private NumberFormat cf = NumberFormat.getCurrencyInstance();

Page 76: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 76© copyright Janson Industries 2011

Java Bean Example

▮ Tx needs:▮ Date and time properties ▮ w/ setters and getters

private String date = null;

private String time = null;

public String getDate() {

return date; }

public String getTime() {

return time; }

private void setDate() {

date = dfMedium.format(d); }

private void setTime() {

time = tfShort.format(d); }

Page 77: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 77© copyright Janson Industries 2011

Java Bean Example

▮ Tx needs to:▮ Initialize the date and time

properties

▮ Have a getReceipt method that generates the HTML and info in the correct formatting

public Tx() {

this.setDate();

this.setTime();

}

Page 78: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 78© copyright Janson Industries 2011

Java Bean Example

Easiest way to generate the HTML is to create the page in Page Designer with static text and

formatting

Page 79: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 79© copyright Janson Industries 2011

Then copy the HTML source into the bean and

modify

Java Bean Example

Page 80: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 80© copyright Janson Industries 2011

Java Bean Examplepublic String getReceipt(){

String receipt = null;

StringBuffer sb = new StringBuffer();

sb.append("<div align='center'><table border='0'>");

sb.append("<tbody><tr>");

sb.append("<td align='center'>");

sb.append("<font size='4' color='#000000' face='Verdana'> ");

sb.append("TNT Salvage</font><br>");

sb.append("<font size='3' color='#000000' face='Arial'>");

sb.append(this.getDate() + "<br>");

sb.append(this.getTime() + "</font></td>");

sb.append("</tr>");

for (int i = 1; i <= this.getQty(); i++){

sb.append("<tr>");

sb.append("<td><font color='#0000a0'>" +

this.getItemName() + " " +

cf.format(this.getPrice()) + "</font></td>");

sb.append("</tr>");

}

Date/time, item name, and price inserted for static

text

Loop using getQty inserted

Change html " to '

Page 81: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 81© copyright Janson Industries 2011

Java Bean Examplesb.append("<tr>");

sb.append("<td align='center'><br>" +

"<font size='3' color='black' face='Arial'>" +

"Total Sale Amount: " +

cf.format(this.getTotal()) + "</font></td>");

sb.append("</tr>");

sb.append("<TR>");

sb.append("<td align='center'>" + this.getCustName() + "<br>");

sb.append("Thanks for shopping with us</td>");

sb.append("</tr>");

sb.append("</tbody>");

sb.append("</table></div>");

receipt = String.valueOf(sb);

return receipt;

}

Must convertsb to String and

return the HTML

Sub real data for static

text

Page 82: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 82© copyright Janson Industries 2011

Change Sale.jsp to get receipt property and delete static text and formatting

Page 83: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 83© copyright Janson Industries 2011

Java Bean Example

Page 84: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 84© copyright Janson Industries 2011

Sale Bean Example

Txobject

FuncServlet

TxBean

Functions.html

Sale.jsp

Creates

Cust orSale.html

Sale.html

HTML (response)with receipt info

ControllerModel

View

Redirects ReceiptInfo

Sale info

User

Sale(Servlet)

Page 85: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 85© copyright Janson Industries 2011

Sale Example

■ However, we have violated MVC!!

■ The TX class is creating the view

♦ getReceipt returns a web page/html – that’s the view's job!

♦ Should have a JSP get TX properties and format!

Page 86: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 86© copyright Janson Industries 2011

JSPs

▮ Will create new JSP called SaleJSP

▮ SaleJSP will get the info and format the receipt using:

▮ JSP standard action tags

▮ Bean tags

Page 87: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 87© copyright Janson Industries 2011

Sale Example

Sale(Servlet)

Txobject

FuncServlet

TxBean

User

Functions.html

SaleJSP

Creates

Cust orSale.html

Sale.html

HTML (response)with receipt info

ControllerModel

View

Redirects Propertyvalues

Sale info

Page 88: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 88© copyright Janson Industries 2011

JSPs

<jsp:getProperty name=“TxBean" property=“custName"/>

<jsp:getProperty name=“TxBean" property=“price"/>

▮ Use JSP getProperty tag to access the bean

▮ Can type in the above code or use Page Designer

Page 89: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 89© copyright Janson Industries 2011

Add a table with 6 rows 1 col with static text

More JSP Tags

Page 90: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 90© copyright Janson Industries 2011

Click Get Property and click 2nd row

Page 91: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 91© copyright Janson Industries 2011

Make sure the bean type is session, then specify Bean and Property names, click OK

Page 92: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 92© copyright Janson Industries 2011

JSP tag icon displayed

JSP tag result can be formatted by selecting the tag and clicking

FormatThen delete static text "date"

Page 93: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 93© copyright Janson Industries 2011

HTML and JSP tags that were generated

Page 94: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 94© copyright Janson Industries 2011

Time to test: Run Sale.html on server

Change Sale to redirect to SaleJSP

Page 95: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 95© copyright Janson Industries 2011

More JSP Tags

To test, enter info and click submit

Page 96: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 96© copyright Janson Industries 2011

More JSP Tags

Need to add tags to get other properties

Page 97: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 97© copyright Janson Industries 2011

More JSP Tags

Page 98: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 98© copyright Janson Industries 2011

Now, when the above is entered:

Page 99: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 99© copyright Janson Industries 2011

There are JSTL tags that allow looping To use the bean property qty (to control the

loop) the tag requires EL (Expression Language)

Of course, getTotal() & getPrice should be changed to return a formatted value plus

there’s no loop

Page 100: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 100© copyright Janson Industries 2011

▮ Create CustJSP:

▮ Access the Customer bean

▮ Display the customer info using bean tags

▮ Change Func2Servlet to create CustBean and redirect to CustJSP

In class assg 2

Page 101: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 101© copyright Janson Industries 2011

MyWeb Currently

Func2Servlet

Customerobject

FuncServlet

co

CustServlet

User

Functions.html

Data

Cust orSale.html

Cust.html

HTML (response)With Cust info

ControllerModel

View

requestresponse

coData

Page 102: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 102© copyright Janson Industries 2011

In class assg 2

Func2Servlet

Customerobject

FuncServlet

CustBean

User

Functions.html

CustJSP

Creates

Cust orSale.html

Cust.html

HTML (response)with receipt info

ControllerModel

View

Redirects Propertyvalues

Cust info

Solution

Page 103: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 103© copyright Janson Industries 2011

Looping with JSTL tags

■ The JSTL (Java Standard Tag Library) provides a forEach tag for looping

■ We could cheat and add to SaleJSP...

<c:forEach begin="1" end="2">

html and/or content to be repeated</c:forEach>

Page 104: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 104© copyright Janson Industries 2011

Since there are multiple items need a <br> at end

Page 105: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 105© copyright Janson Industries 2011

But, of course, this will always show the item twice regardless of

the qtyWant to loop the qty number of

times

Page 106: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 106© copyright Janson Industries 2011

Expression Language

■ Created for easy access to beans and properties (don’t need tags)

■ Syntax is: ${beanname.property}

■ EL is part of JSP not JSTL♦ i.e. EL can be used apart from JSTL tags

■ For example, adding the following : The item qty is: ${TxBean.qty}

Page 107: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 107© copyright Janson Industries 2011

Then save and refresh the browser

Page 108: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 108© copyright Janson Industries 2011

Get rid of text and EL then substitute the EL for 2 in the for

each tag’s end value

Results in...

Page 109: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 109© copyright Janson Industries 2011

Save and go back in the browser and change quantity to 4 then

click Submit

Page 110: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 110© copyright Janson Industries 2011

Page 111: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 111© copyright Janson Industries 2011

Other JSP And JSTL Tags

■ There are several useful JSP tags

♦ jsp:include – places a file’s content in a JSP

■ And JSTL tags for conditional logic♦ <c:if test=“condition”> body </c:if>♦ <c:choose>

►<c:when test=“condition”> body </c:when>►<c:when test=“condition”> body </c:when>

♦ <c:choose>

<jsp:include page=“footer” flush=“true”/>

Page 112: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 112© copyright Janson Industries 2011

A file in the project (footer) has an HTML snippet that defines a footer table to appear

at the bottom of every page

Page 113: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 113© copyright Janson Industries 2011

... add the include to the JSP, save the source ...

Page 114: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 114© copyright Janson Industries 2011

... refresh the browser

Page 115: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 115© copyright Janson Industries 2011

JSTL Tags and EL

▮ EL allows:

▮ Easy access to beans and implicit objects

▮ pageContext, request, response, param

▮ Implicit objects created and provided by server

▮ Use of all Java comparison operators (==, <, >, etc.) in EL condition definition

Page 116: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 116© copyright Janson Industries 2011

If tag and EL

▮ Allow JSP to do one thing on GET and another on POST (i.e. when the user responds)

▮ if tag with EL<c:if test=‘$

{pageContext.request.method==“GET”}’>Display initial page with form

</c:if>

▮ Notice no .equals when evaluating string!

Page 117: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 117© copyright Janson Industries 2011

JSTL if/when & EL Example

<c:if test=‘${pageContext.request.method==“POST”}’><c:choose>

<c:when test=‘${param.functionLB == "Display"}'><jsp:forward page="DispEmpInfoJSP.jsp"></jsp:forward>

</c:when><c:when test=‘${param.functionLB == "Gross"}'>

< jsp:forward page ="DispEmpGrossJSP.jsp"></jsp:forward>

</c:when><c:when test=‘${param.functionLB == "TaxAmt"}'>

< jsp:forward page ="DispEmpTaxAmtJSP.jsp">

</jsp:forward></c:when>

</c:choose></c:if>

Page 118: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 118© copyright Janson Industries 2011

▮ Create a CustJSP2 that:

▮ Accesses CustBean bean using EL instead of getProperty bean tags

▮ Change Func2Servlet to redirect to CustJSP2

In class assg 3

Page 119: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 119© copyright Janson Industries 2011

In class assg

Func2Servlet

Customerobject

FuncServlet

CustBean

User

Functions.html

CustJSP2

Creates

Cust orSale.html

Cust.html

HTML (response)with receipt info

ControllerModel

View

Redirects Propertyvalues

Cust info

Solution

Page 120: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 120© copyright Janson Industries 2011

▮ Customers will be able to order from phones

▮ Of course, need the new phone app with its View

▮ But watch how much we have to change our current app to add that view

New Mobile View

Page 121: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 121© copyright Janson Industries 2011

User runs app on phone…

Page 122: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 122© copyright Janson Industries 2011

…enters data, clicks button…

Page 123: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 123© copyright Janson Industries 2011

…gets this “receipt”

Page 124: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 124© copyright Janson Industries 2011

▮ The phone app will send a post request (just like our web app)

▮ However, the customer name will be proceeded by the text mobilePhoneOrder:

▮ The controller (Sale) will check for the text and if it is a mobile order will simply return the total

New Mobile View

Page 125: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 125© copyright Janson Industries 2011

Web-based Sale App

Sale(Servlet)

Txobject

FuncServlet

TxBean

User

Functions.html

SaleJSP

Creates

Cust orSale.html

Sale.html

HTML (response)with receipt info

ControllerModel

View

Redirects Propertyvalues

Sale info

Page 126: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 126© copyright Janson Industries 2011

Mobile Sale App

Txobject

PhoneSale

TxBean

User

Saleinfo

Creates

Salereceipt

ControllerModel

Sale info

Mobile View

Sale info

Sale total

Sale(Servlet)

Sale total

postrequest

response

Page 127: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 127© copyright Janson Industries 2011

Web/Mobile Sale App

Txobject

PhoneSale

TxBean

User

Saleinfo

SaleJSP

Creates

Salereceipt

Sale.html

HTML (response)with receipt info

ControllerModel

WebView

Redirects Propertyvalues

Sale info

Mobile View

Sale info

Sale total

Sale(Servlet)

Sale total

Page 128: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 128© copyright Janson Industries 2011

New Mobile View

import java.io.PrintWriter; : : : : :public class Sale extends HttpServlet implements Servlet { : : : : : public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { : : : : : sess.setAttribute("TxBean", TxBean);// new code for phone order view StringBuffer custNameSB = new StringBuffer(req.getParameter("CustName"));// checks if it’s a mobile phone order if (custNameSB.length() >= 17 &&

custNameSB.substring(0, 17).equals("mobilePhoneOrder:"))

Page 129: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 129© copyright Janson Industries 2011

New Mobile View

{// sets Customer name correctly

txBean.setCustName(custNameSB.substring(17));resp.setContentType("text/html");try {

PrintWriter out = new PrintWriter(resp.getOutputStream());

// gets the total from the bean and imbeds into the responseout.println(txBean.getTotal());out.close();

} catch (Exception e) {System.out.println("Sale Servlet failed: ");e.printStackTrace(); }

} else {// its not a mobile order so invoke the web view

resp.sendRedirect("SaleJSP.jsp"); } }}

Page 130: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 130© copyright Janson Industries 2011

New Mobile View

TxBean

PhoneUser

Creates

Post request

response(with Total)

Controller ModelView

Sale infoSale(Servlet)

Total

Txobject

▮ Notice how in the phone architecture the view does not access the model

▮ The controller acts a buffer between the two

Page 131: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 131© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML

▮ For example, this is the receipt screen

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" >

Page 132: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 132© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

<TextView android:id="@+id/nameTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TNT Salvage" /> </LinearLayout>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" >

<TextView android:id="@+id/itemTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" >

Page 133: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 133© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

<TextView android:id="@+id/custTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" >

<TextView android:id="@+id/footerTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Thanks for shopping with us" /> </LinearLayout>

</LinearLayout>

Page 134: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 134© copyright Janson Industries 2011

Mobile App Java Code

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

package com.phone.sale;

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;

Page 135: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 135© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

public class PhoneSaleActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Displays the order entry screensetContentView(R.layout.main);

}// Called when button clickedpublic void doClick(View arg0) {// Retrieves the text specified in the EditTextsEditText custNameET = (EditText) findViewById(R.id.nameET);String custName = custNameET.getText().toString();EditText itemNameET = (EditText) findViewById(R.id.itemET);String itemName = itemNameET.getText().toString();EditText qtyET = (EditText) findViewById(R.id.qtyET);String qty = qtyET.getText().toString();EditText priceET = (EditText) findViewById(R.id.priceET);String price = priceET.getText().toString();BufferedReader in = null;

Page 136: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 136© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

try {// Creates a client and request objectHttpClient client = new DefaultHttpClient();HttpPost request = new HttpPost(

"http://sth-radsrv-01.student.fccj.org:9080/MyWeb/Sale");

// Creates a list and populates it with the entered dataList<NameValuePair> postParameters = new

ArrayList<NameValuePair>();postParameters.add(new BasicNameValuePair("CustName",

custName));postParameters.add(new BasicNameValuePair("ItemName",

itemName));postParameters.add(new BasicNameValuePair("Price", price));postParameters.add(new BasicNameValuePair("Qty", qty));// Creates a form and ties the parameter list to the formUrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(

postParameters);// Ties the form to the requestrequest.setEntity(formEntity);// Executes the request and captures the responseHttpResponse response = client.execute(request);// Creates a reader tied to the responsein = new BufferedReader(new

InputStreamReader(response.getEntity().getContent()));

Page 137: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 137© copyright Janson Industries 2011

Mobile App

▮ The Mobile App (PhoneSale) screens defined with XML▮ For example, this is the receipt screen

// Displays the receipt screensetContentView(R.layout.receipt);TextView itemTV = (TextView) findViewById(R.id.itemTV);// Reads the response and uses the text in the msgitemTV.setText("You've purchased " + qty + " " + itemName

+ "(s) for a total of $" + in.readLine());TextView custTV = (TextView) findViewById(R.id.custTV);// Displays the customer namecustTV.setText(custName);in.close();

} catch (Exception e) {e.printStackTrace();

} finally {if (in != null) {try {in.close();

} catch (IOException e) {e.printStackTrace();

}}

}}

}

Page 138: Chapter 91© copyright Janson Industries 2011 Java Design Concepts ■ JSP’s ■ MVC MVC ■ Beans Beans ■ JSP Tags JSP Tags.

Chapter 9 138© copyright Janson Industries 2011