Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,

Slides:



Advertisements
Similar presentations
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Advertisements

Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
Tomcat Configuration A Very, Very, Very Brief Overview.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Java web development Servlet & Java server pages.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
11. Java-Based Web: JSP, JSF. 2 Motto: Rule 1: Our client is always right Rule 2: If you think our client is wrong, see Rule 1. - Anonymous.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
CSCI 6962: Server-side Design and Programming Course Introduction and Overview.
Component-Based Software Engineering Internet Applications Paul Krause.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 5_1 George Koutsogiannakis/ Summer
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
CSCI 6962: Server-side Design and Programming Java Server Faces Components and Tags.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
J2EE Overview Web Programming CSCI J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
1 Java Server Pages Allows the embedding of Java commands in a page of HTML. Popular for UI heavy solutions. These commands are then interpreted by a JSP.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
Chapter 14 Using JavaBeans Components in JSP Documents.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Lecture JAVA SERVLETS & JAVA SERVER PAGES François Major Université de Montréal
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
JSP java server pages.
Java Server Pages Can web pages be created specially for each user?
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
JavaBeans and JSP CS-422.
Introduction to Java Bean
Introduction to Java Bean
Java Server Pages B.Ramamurthy.
Scope and State Handling in Java Server Pages
COP 4610L: Applications in the Enterprise Spring 2005
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

Mark Dixon 1 12 – Java Beans

Mark Dixon 2 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 3 PersonList.jsp Class complex Pages simpler <%! People p = new People(); %> <% String html = ""; p.Open (); p.Select ("SELECT * FROM Person;"); while( p.Next ()){ html += p.get ("Surname") + " "; } p.Close (); %> People Import Package Use methods Create Instance

Mark Dixon 4 PersonList.jsp (using Bean) Class complex Pages simpler <% String html = ""; p.Open (); p.Select ("SELECT * FROM Person;"); while( p.Next ()){ html += p.get ("Surname") + " "; } p.Close (); %> People Create Bean Use methods

Mark Dixon 5 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

Mark Dixon 6 Java class meeting specific requirements: –Must have a zero-argument constructor: public MyBean() { … } –All properties private ( no public properties) –data accessed via access methods What is a JavaBean

Mark Dixon 7 BANK ACCOUNT BEAN 0 Parameter constructor Important Exception is for boolean attributes isXxxx() Beans MUST be in packages Get and set methods MUST conform to getXxxx() and setXxxx() Can have other methods but method names cannot look like property get / set

Mark Dixon 8 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

Mark Dixon 9 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

Mark Dixon 10 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: p –Server-Side application We are only dealing with the latter MORE THAN ONE BEAN

Mark Dixon 11 BEAN RELATED TAGS

Mark Dixon 12 BEANS WITH JSP A JSP file which makes use of the Class Bank –Note: file called Bank.jsp

Mark Dixon 13 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: Or Java: BankAccount myAccount = new BankAccount(); Note: use of package name Important This / is important

Mark Dixon 14 SETTING BEAN PROPERTIES 1 Sets the value of the myAccount property balance to 500 Basically the same operation as: Or in Java as: BankAccount myAccount = new BankAccount(); mybalance = myAccount.setBalance(500);

Mark Dixon 15 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 16 SETTING BEAN PROPERTIES 3 Although this value is text converted automatically to correct type –In this case a double

Mark Dixon 17 READING BEAN PROPERTIES Inserts the value of myAccount property balance into the web page Basically the same as: Or in Java as: BankAccount myAccount = new BankAccount(); double mybalance; mybalance = myAccount.getBalance();

Mark Dixon 18 JSP BEANS - REVIEW This line creates an object called myAccount of class BankAccount This line sets the balance property to 500 This line gets the balance Note how the value is displayed on the html page

Mark Dixon 19 SETTING BEAN PROPERTIES FROM TEXT BOXES This the same as: String bal = request.getParamter(“openingbalance”); double tempBal = Double.parseDouble(bal); myaccount.setBalance(tempBal); Sets the property ‘balance’ to what ever was typed in the textbox..jsp Page.htmlPage

Mark Dixon 20 USING TEXTBOXES If the textbox name is the same name as the property Then we do not need a ‘param’

Mark Dixon 21 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 22 OpenAccount.html ‘WILDCARDS ’ EXAMPLE NewAccount.jsp

Mark Dixon 23 ‘WILDCARDS’ EXAMPLE

Mark Dixon 24 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

Mark Dixon 25 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

Mark Dixon 26 SESSION BEANS File: Rent.jsp File: Bank.jsp The file Bank.jsp Creates the object myAccount, which is then used by Rent.jsp Essentially passing information between JSP pages

Mark Dixon 27 CONDITIONAL BEANS So far we have used the 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 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 28 EXAMPLE: Lets assume we have a ‘back’ link on the PayRent.jsp ??? Balance should be 

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

Mark Dixon 30 SOLUTION: CONDITIONAL BEAN The replaced by statements 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

Mark Dixon 31 EXAMPLE: Now we have Balance is correct at

Mark Dixon 32 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

Mark Dixon 33 Tomcat: LocalHost

Mark Dixon 34 Tomcat Directory Structure

Mark Dixon 35 Tomcat Folder Structure Context root Starting html page Web application deployment descriptor (web.xml) Package name of the HelloServlet class The HelloServlet class Netbeans Will create this Structure …

Mark Dixon 36 fgfg Default location is in webapps Can have any number of webapplications in webapps But each need WEB-INF and web.xml Tomcat Folder Structure

Mark Dixon 37 Tomcat - NetBeans JRE_HOME = C:\Program Files\Java\jre6 –Control Panel –System –Advanced –Environment Variables C:\Program Files\Apache Software Foundation\Apache Tomcat \bin –startup.bat (run from command line)

Mark Dixon 38 Hall, M. Servlets and Java Server Pages 2 nd Edition –Chapter 14: Using Beans with JSP Best coverage Armstrong, E. (2003) The J2EE 1.4 Tutorial –chapter 12: Pages REFERENCES - READ AT LEAST ONE OF …