<%@page contentType="text/html" pageEncoding="UTF-8"%> <%!">
12 java beans
This presentation is the property of its rightful owner.
Sponsored Links
1 / 38

12 – Java Beans PowerPoint PPT Presentation


  • 62 Views
  • Uploaded on
  • Presentation posted in: General

12 – Java Beans. Session Aims & Objectives. Aims To cover the use of Java Beans Objectives, by end of this week’s sessions, you should be able to: Create and use a Java Bean. PersonList.jsp. <[email protected] import="Main.*" %> <[email protected] contentType="text/html" pageEncoding="UTF-8"%> <%!

Download Presentation

12 – Java Beans

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


12 java beans

12 – Java Beans


Session aims objectives

Session Aims & Objectives

  • Aims

    • To cover the use of Java Beans

  • Objectives,by end of this week’s sessions, you should be able to:

    • Create and use a Java Bean


Personlist jsp

PersonList.jsp

<[email protected] import="Main.*" %>

<[email protected] contentType="text/html" pageEncoding="UTF-8"%>

<%!

People p = new People();

%>

<%

String html = "";

p.Open();

p.Select("SELECT * FROM Person;");

while(p.Next()){

html += p.get("Surname") + "<br />";

}

p.Close();

%>

<!DOCTYPE html>

<html>

<head><title>People</title></head>

<body>

<%=html%>

</body>

</html>

  • Class complex

  • Pages simpler

Import Package

Create Instance

Use methods


Personlist jsp using bean

PersonList.jsp (using Bean)

  • Class complex

  • Pages simpler

Create Bean

<jsp:useBean id="p" scope="session" class="Main.People" />

<[email protected] contentType="text/html" pageEncoding="UTF-8"%>

<%

String html = "";

p.Open();

p.Select("SELECT * FROM Person;");

while(p.Next()){

html += p.get("Surname") + "<br />";

}

p.Close();

%>

<!DOCTYPE html>

<html>

<head><title>People</title></head>

<body>

<%=html%>

</body>

</html>

Use methods


Jsp and javabeans

JavaBean (Bean) = Java class instance

JSP programming style strongly encourages JavaBeans use

special tags built-in for JavaBean properties

JSP + Bean combination

separates page html look from ‘logic’

i.e. the presentation from the code

JSP and JavaBeans


What is a javabean

Java class meeting specific requirements:

Must have a zero-argument constructor:

public MyBean() { …

}

All propertiesprivate (no public properties)

data accessed via access methods

What is a JavaBean


Bank account bean

BANK ACCOUNT BEAN

Get and set methods

MUST conform to getXxxx() and setXxxx()

Beans MUST be in packages

0 Parameter constructor Important

Exception is for boolean attributes isXxxx()

Can have other methods but method names cannot look like property get / set


Refining the terminology

An attribute is a variable which belongs to an class/object

For objects also known as instance variables

For classes also known as class variables

Remember final static int COLOUR_ONE

Math.PI is a class variable

A property is an attribute which has getter and setter methods

And that’s it !

REFINING THE TERMINOLOGY


Javabean properties

Read-only properties:

String getAccountID()

returns the accountID property

Read/write properties:

void setBalance(double bal)

double getBalance()

Boolean properties:

boolean isActive()

void setActive(boolean act)

JAVABEAN PROPERTIES


More than one bean

It is important to distinguish between a JavaBean as used in a:

GUI development tool

This is a visual component

i.e. will subclass Panel, Button etc.

Note there is a visual Bean design tool at:

http://java.sun.com/products/javabeans/beanbuilder/index.jsp

Server-Side application

We are only dealing with the latter

MORE THAN ONE BEAN


Bean related tags

<jsp: useBean ……… >

<jsp: setProperty ……… >

<jsp: getProperty ……… >

BEAN RELATED TAGS


Beans with jsp

BEANS WITH JSP

A JSP file which makes use of the Class Bank

Note: file called Bank.jsp


Creating an object

CREATING AN OBJECT

Creates a bean instance called ‘myAccount’ of type ‘BankAccount’

The id attribute is the name of the variable

Similar to the following JSP code:

<% BankAccountmyAccount = new BankAccount(); %>

Or Java:

BankAccountmyAccount = new BankAccount();

Note: use of package name

Important

This / is important


Setting bean properties 1

SETTING BEAN PROPERTIES 1

Sets the value of the myAccountpropertybalance to 500

Basically the same operation as:

<%= myAccount.setBalance(500) %>

Or in Java as:

BankAccountmyAccount = newBankAccount();

mybalance = myAccount.setBalance(500);


Setting bean properties 2

SETTING BEAN PROPERTIES 2

Also can have a dynamic property which uses an expression tag

This example is just setting the balance to some random value between 0 and 100


Setting bean properties 3

SETTING BEAN PROPERTIES 3

Although this value is text

converted automatically to correct type

In this case a double


Reading bean properties

READING BEAN PROPERTIES

Inserts the value of myAccountpropertybalance into the web page

Basically the same as:

<%= myAccount.getBalance() %>

Or in Java as:

BankAccountmyAccount = newBankAccount();

double mybalance;

mybalance = myAccount.getBalance();


Jsp beans review

JSP BEANS - REVIEW

Note how the value is displayed on the html page

This line creates an object called myAccount of class BankAccount

This line sets the balance property to 500

This line gets the balance


Setting bean properties from text boxes

SETTING BEAN PROPERTIES FROM TEXT BOXES

This the same as:

String bal = request.getParamter(“openingbalance”);

double tempBal = Double.parseDouble(bal);

myaccount.setBalance(tempBal);

.htmlPage

Sets the property ‘balance’ to what ever was typed in the textbox.

.jsp Page


Using textboxes

USING TEXTBOXES

If the textbox name is the same name as the property

Then we do not need a ‘param’


Setting bean properties wildcards

SETTING BEAN PROPERTIES … ‘WILDCARDS’

Using wildcards to set properties:

  • Sets the value of all ‘somebean’ properties to JSP parameters with the same name

    • If the parameters do not exist, the value of the bean properties do not change


Wildcards example

‘WILDCARDS’ EXAMPLE

OpenAccount.html

NewAccount.jsp


Wildcards example1

‘WILDCARDS’ EXAMPLE


Javabean scope 1

scope= “page”

scope= “request”

These beans will not last after the request is completed

The difference between these 2 scopes is very small

Beans such as this do not allow you to share data between servlets and JSPs

scope= “application”

scope= “session”

These beans will last between requests, thus allowing sharing of data between requests

Again, the differences between these two requests are mostly cosmetic

JAVABEAN SCOPE 1

The default scope


Session beans

SESSION BEANS

As Bank.jsp and Rent.jsp are scoped at session level, the object myAccount is not created in Rent.jsp

File: Rent.jsp


Session beans1

SESSION BEANS

File: Bank.jsp

The file Bank.jsp

Creates the object myAccount, which is then used by Rent.jsp

Essentially passing information between JSP pages

File: Rent.jsp


Conditional beans

CONDITIONAL BEANS

So far we have used the <jsp: useBean id =“somebean…. > tag

jsp:useBean results in new bean being created only if no bean with same id and scope can be found

If a bean with same id and scope is found, then that bean is used.

This means that any property we initially set will be again be set each time we visit the page

This is ok when we visit the a page for the 1st time as we want to set the properties of the bean which will be used across several pages.

But what if we wanted to set initial bean properties for a bean which is shared by multiple pages.

Since we don’t know which page will be accessed first, we don’t know which page should contain the initialization code.


Example

EXAMPLE:

Lets assume we have a ‘back’ link on the PayRent.jsp

??? Balance should be 350.00 


12 java beans

Problem is that when we return to the Bank.jsp page the setProperty sets the balance to 500 again


Solution conditional bean

SOLUTION: CONDITIONAL BEAN

The <jsp:useBean ... />

replaced by

<jsp:useBean ...> statements </jsp:useBean>

The statements (i.e. jsp:setProperty elements) are executed only if a new bean is created, not if an existing bean is found.

This is subtle but the effects are profound

Modified file: Bank.jsp


Example1

EXAMPLE:

Now we have

Balance is correct at 350.00 


Apache tomcat

Apache – http server (html pages)

Tomcat – runs JSP + Servlets

servlet container (interpreter/compiler)

Can run:

Standalone

Handles simple page requests

Handles servlet requests

Apache plugin

Apache handles HTML pages, CGI, PHP etc

Tomcat handles servlets

Apache Tomcat


Tomcat localhost

Tomcat: LocalHost


Tomcat directory structure

Tomcat Directory Structure


Tomcat folder structure

Tomcat Folder Structure

Context root

Starting html page

Netbeans

Will create this

Structure …

Web application deployment descriptor (web.xml)

Package name of the HelloServlet class

The HelloServlet class


Tomcat folder structure1

fgfg

Tomcat Folder Structure

But each need WEB-INF and web.xml

Default location is in webapps

Can have any

number of

webapplications

in webapps


Tomcat netbeans

Tomcat - NetBeans

  • JRE_HOME = C:\Program Files\Java\jre6

    • Control Panel

    • System

    • Advanced

    • Environment Variables

  • C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.14\bin

    • startup.bat (run from command line)

  • http://localhost:8080/


References read at least one of

Hall, M. Servlets and Java Server Pages 2nd Edition

Chapter 14: Using Beans with JSP

Best coverage

Armstrong, E. (2003) The J2EE 1.4 Tutorial

chapter 12: Pages 515 - 525

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html

REFERENCES - READ AT LEAST ONE OF …

38


  • Login