CS320 Web and Internet Programming Java Beans

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

JSP and Servelets.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
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.
COMP201 Java Programming Part III: Advanced Features Topic 16: JavaServer Pages (JSP) Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
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.
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.
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.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
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,
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
Java Server Pages (JSP)
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Java Server Pages Introduction. Servlet Drawbacks Web page designer will need to know servlets to design the page. Servlet will have to be compiled for.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
Web Technologies Java Beans & JSP By Praveen Kumar G.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
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.
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
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.
JSP Java Server Pages. Hello, !
CS320 Web and Internet Programming Web Application and MVC Chengyu Sun California State University, Los Angeles.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
JSP Action Elements Lec - 37.
Andrew(amwallis) Classes!
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
JavaBeans and JSP CS-422.
Chengyu Sun California State University, Los Angeles
Introduction to Java Bean
Anatomy of a class Part I
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Introduction to Java Server Pages
Introduction to Java Bean
CS320 Web and Internet Programming Expression Language (EL)
Java Server Pages (JSP)
CS320 Web and Internet Programming MVC Architecture
CS3220 Web and Internet Programming Expression Language (EL)
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Expression Language (EL)
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Scripted Page Web Application Development (Java Server Pages)
Web Technologies Java Beans & JSP
Chengyu Sun California State University, Los Angeles
Anatomy of a class Part I
Presentation transcript:

CS320 Web and Internet Programming Java Beans Chengyu Sun California State University, Los Angeles

Problems with Scripting Elements Mixing presentation and processing hard to read, debug, or maintain No clean and easy way to reuse code Solution – separate out Java code Servlets Java Beans Custom tag libraries

Java Beans A regular Java object, typically used for modeling data, e.g. GuestBookEntry A.K.A. POJO (Plain Old Java Object)

Bean Properties The properties of a bean are defined by getters and setters Properties != Class variables Properties public class User { String firstname; String lastname; public getFirstname() { return firstname; } public getLastname() { return lastname; } public getName() { return firstname + “ “ + lastname; } } firstname lastname name

About Bean Properties Property naming conventions Property types 1st letter is always in lower case 1st letter must be capitalized in getter (accessor) and/or setter (mutator) Property types read-only property: only getter write-only property: only setter read/write property: both

Bean Property Example What properties does Foobar have? Read-only: ?? public class Foobar { private int a, b, c, d; private boolean e; public Foobar() { a = b = c = d = 0; } public int getA() { return a; } public void setA( int a ) { this.a = a; } public int getB() { return b; } public void setC( int c ) { this.c = c; } public int getAB() { return a+b; } public boolean isE() { return e; } public void setE( boolean e ) { this.e = e; } } What properties does Foobar have? Read-only: ?? Write-only: ?? Read/write: ??

Common Problems with Bean Property … public class Foobar { private int a, b, c, d; public Foobar() { a = b = c = d = 0; } public int getA() { return a; } public void setA( String s ) { this.a = Integer.parseInt(s); } public int getB( int x ) { return b+x; } public void setC( int c, int x ) { this.c = c+x; } public void setD( String s ) { this.d = Integer.parseInt(d); } } How many properties does Foobar have??

… Common Problems with Bean Property A getter must have no argument A setter must have exactly one argument The type of a property must be consistent in both the getter and the setter

Bean and JSP Bean JSP <jsp:getProperty> <jsp:useBean> EL <jsp:setProperty> <c:set>

Bean Tags and Attributes jsp:useBean class id scope page (default) request session application jsp:getProperty name property jsp:setProperty value param

Example: BGColor.jsp Use a bean BGBean to control the background color of a JSP page <jsp:useBean id=“bg" class=“cs320.bean.BGBean" /> <jsp:getProperty name=“bg” property=“r” />

Understand <jsp:useBean> and <jsp:getProperty> <jsp:useBean id=“bg" class=“cs320.bean.BGBean" /> <jsp:getProperty name=“bg" property=“r" /> cs320.bean.BGBean bg = new cs320.bean.BGBean(); bg.getR(); The bean class used in <jsp:useBean> must have a constructor that takes no argument.

Set Bean Properties <jsp:setProperty name=“bg” property=“r” value=“255” /> <jsp:setProperty name=“bg” property=“r” param=“r” /> <jsp:setProperty name=“bg” property=“r” /> <jsp:setProperty name=“bg” property=“*” />

Understand Scopes Application scope – data is valid throughout the life cycle of the web application Session scope – data is valid throughout the session redirect, multiple separate requests Request scope – data is valid throughout the processing of the request forward Page scope – data is valid within current page

Scopes in Servlet Application scope Session scope Request scope ServletContext Session scope HttpSession Request scope HttpServletRequest Page scope (in JSP scriptlet) pageContext