12 Java Beans Mark Dixon 1 Session Aims
12 – Java Beans Mark Dixon 1
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 Mark Dixon 2
Person. List. jsp Import Package Create Instance Use methods • Class complex • Pages simpler Mark Dixon <%@page import="Main. *" %> <%@page content. Type="text/html" page. Encoding="UTF-8 <%! People p = new People(); %> <% String html = ""; p. Open(); p. Select("SELECT * FROM Person; "); while(p. Next()){ html += p. get("Surname") + " "; } p. Close(); %> <!DOCTYPE html> <head><title>People</title></head> <body> <%=html%> </body> </html> 3
Person. List. jsp (using Bean) Create Bean Use methods • Class complex • Pages simpler Mark Dixon <jsp: use. Bean id="p" scope="session" class="Main. Peo <%@page content. Type="text/html" page. Encoding="UTF-8 <% String html = ""; p. Open(); p. Select("SELECT * FROM Person; "); while(p. Next()){ html += p. get("Surname") + " "; } p. Close(); %> <!DOCTYPE html> <head><title>People</title></head> <body> <%=html%> </body> </html> 4
JSP and Java. Beans • Java. Bean (Bean) = Java class instance • JSP programming style strongly encourages Java. Beans use • special tags built-in for Java. Bean properties • JSP + Bean combination – separates page html look from ‘logic’ – i. e. the presentation from the code Mark Dixon 5
What is a Java. Bean • Java class meeting specific requirements: – Must have a zero-argument constructor: public My. Bean() { … } – All properties private (no public properties) – data accessed via access methods Mark Dixon 6
Beans MUST be in packages BANK ACCOUNT BEAN 0 Parameter constructor Important Exception is for boolean attributes is. Xxxx() Get and set methods MUST conform to get. Xxxx() and set. Xxxx() Can have other methods but method names cannot look like property get / set Mark Dixon 7
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 ! Mark Dixon 8
JAVABEAN PROPERTIES • Read-only properties: String get. Account. ID() • returns the account. ID property • Read/write properties: void set. Balance(double bal) double get. Balance() • Boolean properties: Mark Dixon 9
MORE THAN ONE BEAN • It is important to distinguish between a Java. Bean 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 Mark Dixon 10
BEAN RELATED TAGS • <jsp: use. Bean ……… > • <jsp: set. Property ……… > • <jsp: get. Property ……… > Mark Dixon 11
BEANS WITH JSP • A JSP file which makes use of the Class Bank – Note: file called Bank. jsp Mark Dixon 12
CREATING AN OBJECT Note: use of package name This / is important Important • Creates a bean instance called ‘my. Account’ of type ‘Bank. Account’ • The id attribute is the name of the variable • Similar to the following JSP code: <% Bank. Account my. Account = new Bank. Account(); %> • Or Java: Bank. Account my. Account = new Bank. Account(); Mark Dixon 13
SETTING BEAN PROPERTIES 1 • Sets the value of the my. Account property balance to 500 • Basically the same operation as: <%= my. Account. set. Balance(500) %> • Or in Java as: Bank. Account my. Account Mark Dixon = new 14
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 Mark Dixon 15
SETTING BEAN PROPERTIES 3 • Although this value is text • converted automatically to correct type – In this case a double Mark Dixon 16
READING BEAN PROPERTIES • Inserts the value of my. Account property balance into the web page • Basically the same as: <%= my. Account. get. Balance() %> • Or in Java as: Bank. Account my. Account Bank. Account(); = new double mybalance; mybalance = my. Account. get. Balance(); Mark Dixon 17
JSP BEANS - REVIEW This line creates an object called my. Account of class Bank. Account This line gets the balance This line sets the balance property to 500 Note how the value is displayed on the html page Mark Dixon 18
. html. Page SETTING BEAN PROPERTIES FROM TEXT BOXES Sets the property ‘balance’ to what ever was typed in the textbox. . jsp Page • This the same as: String bal = request. get. Paramter(“openingbalance”); double temp. Bal = Double. parse. Double(bal); myaccount. set. Balance(temp. Bal); Mark Dixon 19
USING TEXTBOXES If the textbox name is the same name as the property Then we do not need a ‘param’ Mark Dixon 20
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 Mark Dixon 21
‘WILDCARDS ’ EXAMPLE Open. Account. html New. Account. jsp Mark Dixon 22
‘WILDCARDS’ EXAMPLE Mark Dixon 23
JAVABEAN SCOPE 1 scope = “page” The default 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” Mark Dixon 24
SESSION BEANS File: Rent. jsp As Bank. jsp and Rent. jsp are scoped at session level, the object my. Account is not created in Rent. jsp Mark Dixon 25
SESSION BEANS File: Bank. jsp The file Bank. jsp File: Rent. jsp Creates the object my. Account, which is then used by Rent. jsp Essentially passing information between JSP pages Mark Dixon 26
CONDITIONAL BEANS • So far we have used the <jsp: use. Bean id =“somebean…. > tag – jsp: use. Bean 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 1 st 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. Mark Dixon 27
EXAMPLE: • Lets assume we have a ‘back’ link on the Pay. Rent. jsp ? ? ? Balance should be 350. 00 Mark Dixon 28
• Problem is that when we return to the Bank. jsp page the set. Property sets the balance to 500 again Mark Dixon 29
SOLUTION: CONDITIONAL BEAN • The <jsp: use. Bean. . . /> • replaced by <jsp: use. Bean. . . > statements </jsp: use. Bean> • The statements (i. e. jsp: set. Property elements) are executed only if a new bean is created, not if an existing bean is found. Modified file: Bank. jsp This is subtle but the effects are profound Mark Dixon 30
EXAMPLE: • Now we have Balance is correct at 350. 00 Mark Dixon 31
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 Mark Dixon 32
Tomcat: Local. Host Mark Dixon 33
Tomcat Directory Structure Mark Dixon 34
Tomcat Folder Structure Context root Starting html page Netbeans Will create this Structure … Web application deployment descriptor (web. xml) Package name of the Hello. Servlet class The Hello. Servlet class Mark Dixon 35
• fgfg Tomcat Folder Structure Default location is in webapps Can have any number of webapplications in webapps But each need WEB -INF and web. xml Mark Dixon 36
Tomcat - Net. Beans • JRE_HOME = C: Program FilesJavajre 6 – Control Panel – System – Advanced – Environment Variables • C: Program FilesApache Software FoundationApache Tomcat 7. 0. 14bin – startup. bat (run from command line) • http: //localhost: 8080/ Mark Dixon 37
REFERENCES - READ AT LEAST ONE OF … nd • Hall, M. Servlets and Java Server Pages 2 Edition – Chapter 14: Using Beans with JSP • Best coverage • Armstrong, E. (2003) The J 2 EE 1. 4 Tutorial – chapter 12: Pages 515 - 525 • http: //java. sun. com/j 2 ee/1. 4/docs/tutorial/doc/index. html 38 Mark Dixon 38
- Slides: 38