DT228/3 Web Development Java Beans. Intro A major problem with JSP is tendency to mix java code with HTML  -  web designer write the HTML and programmer.

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

Apache Struts Technology
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
DT211/3 Internet Application Development JSP: Processing User input.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DT228/3 Web Development multi page applications/ sharing data.
DT211/3 Internet Application Development
DT228/3 Web Development JSP: Directives and Scripting elements.
ASP.NET Programming with C# and SQL Server First Edition
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3. Using Beans: Basic Tasks 4. Sharing Beans 5. Practice.
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.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
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,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
A Simple Guide to Using SPSS ( Statistical Package for the Social Sciences) for Windows.
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.
Java Server Pages (JSP)
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
Implementing and Using the SIRWEB Interface Setup of the CGI script and web procfile Connecting to your database using HTML Retrieving data using the CGI.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
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.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 14 Using JavaBeans Components in JSP Documents.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
JSP Based on
JSP: Actions elements and JSTL
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 (JSP)
CS320 Web and Internet Programming Java Beans
CS3220 Web and Internet Programming Expression Language (EL)
CS3220 Web and Internet Programming Expression Language (EL)
Scripted Page Web Application Development (Java Server Pages)
CS 240 – Advanced Programming Concepts
Presentation transcript:

DT228/3 Web Development Java Beans

Intro A major problem with JSP is tendency to mix java code with HTML  -  web designer write the HTML and programmer writes the java –maintenance issue if mixed -  JSP pages not easy to re-use if both presentation code and business processing code (java) is together –cannot “re-use” business logic

Intro Java beans provide another mechanism to hide java code from the JSP page Java beans can be easily reused – can create libraries of reusable components for plugging into JSP Java beans follow certain rules and guidelines in order to enable their use as components - design patterns

Java beans Can be used in any java program or Java Server Page Easy to write (programmer) Easy to use (non programmer) Can buy java beans to perform specific functions

Java beans can be used to.. Reuse graphical component – buttons etc (original intention…) Validate user input Pass data from one part of an application to another (e.g. person’s details) – i.e. a container for data Perform common database functions.. And more.

Java beans Each bean is written in java and compiled as a java class Must have a zero argument constructor Either explicitly include the constructor OR don’t have any constructor Beans have properties Properties can be read only or Properties can be read/write

E.g. a java bean called Userbean.java might have properties such as name, address, age.. Java bean properties can be set and retrieved with setter and getter methods (called accessor methods) E.g. a java bean with a property colour To read or get the value property colour, use a get method called getColour() To update or set the value property colour, use a get method called setColour() Java bean properties

Properties NOTE: Properties can be read only, read/write: Readable properties only have a getter method (i.e. you can’t “set” or update them) Read/write properties have a setter and getter method

Design patterns for java beans Need to have a way of using bean without knowing a lot about the actual code inside the bean Use Design patterns to enable developers to call the bean and use its properties in a standard way.

Design patterns for java beans Zero argument constructor setPropertyName() methods – consisting of “set” plus property name with each word capitalised getPropertyName() methods – consisting of “get” plus property name with each word capitalised isPropertyName( ) method – returns a boolean value Property (e.g. firstName) always start with a lower case letter

Simple bean example package com; public class SimpleBean implements java.io.Serializable { /* Properties.. Always private*/ private String username = null; private int luckynumber = 0; /* Empty Constructor */ public SimpleBean() {} /* Getter and Setter Methods */ public String getUsername() { return username; } Allows an object to be saved to disk for re- use later Properties are private Access Methods use the property names

Simple bean (cont.) public void setUsername(String s) { username = s; } public int getLuckynumber() { return luckynumber; } public void setLuckynumber(int i) { luckynumber = i; } Access Methods use the property names

JSP Actions for a bean JSP container support several JSP actions to use beans within a JSP page identifies the bean to use sets bean properties retrieves bean properties

To use a java bean in a JSP page Need to create and compile the bean class Place the class file in WEB-INF/classes directory Declare the java bean in the JSP page etc This creates an instance of the bean id attribute = name by which the bean will be identified in the JSP page Class attribute = specifies which bean class to use Scope attribute = controls where bean will be kept

<jsp:useBean id="object-name" scope="page | request | session | application" type="type-of-object" class="fully-qualified-classname" beanName="fully-qualified-beanName" /> JSP Actions: full syntax for reference <jsp:setProperty name="id-of-the-JavaBean" property="name-of-property" param="name-of-request-parameter-to-use” value="new-value-of-this-property" /> <jsp:getProperty name="name-of-the-object" property="name-of-property" />

Scope of a java bean A Java bean is only created when it is needed. Like all objects, has a concept of scope: Page scope (default) – new bean for every page view (bean placed in the PageContext object) Request scope – new bean created for every new client request to the page (bean is placed in the ServletRequest object) Session scope – new bean on first request for new user session (bean is placed in the HttpSession object) Application scope – new bean first request to the application (bean is placed in the webapplication’s ServletContext)

JSP that uses a java bean The following information was saved: User Name: <jsp:getProperty name="simpleBean" property="username" /> Lucky number: <jsp:getProperty name="simpleBean" property="luckynumber" /> Package name

Example: Capturing user info using a bean A form that takes name, birth date, address, gender, lucky number, favourite foods (radio box..) – and displays what the user has entered when the “submit” button is pressed… Java bean (e.g. Userinfo.java) is used to 1) capture and hold user info to pass around the application. and… 2) validate user info….

Example: User info Bean properties Table 5-2: Properties for UserInfoBean Property NameJava TypeAccessDescription userName String read/writeThe user's full name birthDate String read/write The user's birth date in the format yyyy-mm-dd (e.g., ) Addr String read/writeThe user's address in the format gender String read/write The user's sex ( male or female ) luckyNumber String read/writeThe user's lucky number (between 1 and 100) valid boolean read true if the current values of all properties are valid, false otherwise

Example: Capturing User Info

Example: Capturing user info with a Bean - Sample form User Info Entry Form Name: Birth Date: (Use format yyyy-mm-dd) Various Input fields on form..

Example: Capturing user info with a Bean: sample form Address: (Use format Sex: Male Female Lucky number: (A number between 1 and 100) etc.. Various Input fields on form..

<jsp:useBean id="userInfo" class="com.ora.jsp.beans.userinfo.UserInfoBean"> The following information was saved: User Name: <jsp:getProperty name="userInfo" property="userName" /> Birth Date: <jsp:getProperty name="userInfo" property="birthDate" /> Address: <jsp:getProperty name="userInfo" property=" Addr" /> Sex: <jsp:getProperty name="userInfo" property="sex" /> Lucky number: <jsp:getProperty name="userInfo" property="luckyNumber" /> The user input is valid: <jsp:getProperty name="userInfo" property="valid" /> Example: Capturing user info with a Bean Create a bean Assign the properties in the bean to the values in the form.. Quick way using * Output the values of the bean properties – using bean ID and property name input_bean.jsp: stores the data into a bean and displays the bean properties

Example: Output of JSP is:

Example: Bean breakdown Userinfo bean will contain getter and setter methods for each of the five attribute (name, date of birth, etc. To include validation – can simple add methods into the bean to do validation of each of the attributes –This removes complexity from the JSP page –Gives re-usable functionality via the java bean (e.g. address difficult to validate in a JSP page without using scriptlets) –Maintainance easy - If Validation rule changes – only change once in the java bean –

To deploy a bean in Tomcat Write the java code for the bean (***.java) Create a WEB-INF/classes directory in your web application directory structure. Bean code and classes go in here if packages not being used. If using packages (see overleaf), create your package directory structure within the WEB-INF/classes directory.. E.g. WEB- INF/classes/com/webdev/beans Place.java and.class files in to the relevant directory.. And then use the bean in JSP using etc If using packages, remember to put in package declaration in bean.java file (e.g. package com.beans;) – this adds your bean into the package.

Note on packages Packages help to organise how files are stored within an application (imagine if all java classes were not divided into packages…) Sun recommends using a directory structure for packages that is the reverse of the domain name for the web application.. Ensures your package structure is unique. But you can use whatever you want. E.g. developing a web application to be used for Would make sense to have a directory structure for your beans of WEB-INF/class/ie/dit/beans to store the beans in a package.