CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
More on Processing User Input BCIS 3680 Enterprise Programming.
Advertisements

© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
Expression Language Lec Umair Javed©2006 Generating Dynamic Contents Technologies available  Servlets  JSP  JavaBeans  Custom Tags  Expression.
 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.
DT211/3 Internet Application Development
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.
JSP – Java Server Pages Part 2 Representation and Management of Data on the Internet.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
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.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
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:
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.
EXPRESSION LANGUAGE Msc : Lê Gia Minh. EL Language : Cơ Bản  Cho phép JSP developers truy cập các đối tượng Java thông qua tag. Được dùng hiển thị nội.
JSTL: The JavaServer Pages Standard Tag Library Mark A. Kolb Security Broadband, Austin, TX
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
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.
JSP Expression Language (EL) 25-Nov-15. JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
EL and JSTL. JSTL Sources
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.
Intermediate JSP Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java.
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.
JSP Action Elements Lec - 37.
The Expression Language Syntax
CS520 Web Programming Servlet and JSP Review
Java Server Pages By: Tejashri Udavant..
CS520 Web Programming Servlet and JSP Review
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
Invoking Java Code from JSP
Introduction to Java Bean
CS320 Web and Internet Programming Expression Language (EL)
CS320 Web and Internet Programming Java Beans
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)
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)
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles

Problems with Scripting Elements Mixing presentation and processing hard to debug hard to 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 public class User { String firstname; String lastname; public getFirstname() { return firstname; } public getLastname() { return lastname; } public getName() { return firstname + “ “ + lastname; } } Properties firstname lastname name

About Bean Properties Property naming conventions 1 st letter is always in lower case 1 st 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: ?? Write-only: ?? Read/write: ?? 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; } }

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 JSP Bean EL

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

Example: BGColor.jsp Use a bean BGBean to control the background color of a JSP page

Understand and cs320.bean.BGBean bg = new cs320.bean.BGBean(); bg.getR(); The bean class used in must have a Constructor that takes no argument.

Set Bean Properties

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 ServletContext Session scope HttpSession Request scope HttpServletRequest Page scope (in JSP scriptlet) pageContext

Need for EL Using to access bean properties is tedious EL

What is EL? Expression Language (EL) Since the JSP 2.0 Specification A more concise way to access bean properties and write JSP expressions  vs. Java’s answer to scripting languages Syntax: ${ expression }

Example: BGColor.jsp Revisited Use EL to access the bean properties ${ bean_name.property_name }

Expression Literals Operators Variables

EL Literals true, false 23, 0x10, , 1.1e13,... “double-quoted”, ‘single-quoted’ null No char type

EL Operators Arithmetic +, -, *, /, % div, mod Logical &&, ||, ! and, or, not Relational ==, !=,, = eq, ne, lt, gt, le, ge Conditional ? : empty check whether a value is null or empty Other [],., ()

EL Evaluation and Auto Type Conversion ${2+4/2} ${2+3/2} ${“2”+3/2} ${“2”+3 div 2} ${“a” + 3 div 2} ${null == ‘test’} ${null eq ‘null’} ${empty “”} ${empty param.a} ${empty null} ${empty “null”} ${“abc” lt ‘b’} ${“cs320” > “cs203”}

EL Variables You cannot declare new variables using EL (after all, it’s called “expression” language). However, you can access beans, implicit objects, and previously defined scoped variables.

Implicit Objects in EL pageContext servletContext session request response param, paramValues header,headerValues cookie initParam pageScope requestScope sessionScope applicationScope

Example: RequestInfo.jsp Display some information about the request Client address … Cookies and parameters Use of implicit objects Find the Java class type for the object Look for getters in the API  E.g. ${pageContext.request.remoteAddr} Access elements in a collection  cookie and param

Limitation of EL Only expressions, no statements, especially no control-flow statements JSTL