CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California...
-
Upload
bridget-little -
Category
Documents
-
view
227 -
download
3
Transcript of CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California...
![Page 1: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/1.jpg)
CS320 Web and Internet ProgrammingJava Beans and Expression Language (EL)
Chengyu SunCalifornia State University, Los Angeles
![Page 2: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/2.jpg)
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
![Page 3: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/3.jpg)
Java Beans
A regular Java object, typically used for modeling data, e.g. GuestBookEntry
A.K.A. POJO (Plain Old Java Object)
![Page 4: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/4.jpg)
Bean Properties
The properties of a bean are defined by getters and settersProperties != 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
![Page 5: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/5.jpg)
About Bean Properties
Property naming conventions 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
![Page 6: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/6.jpg)
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; }}
![Page 7: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/7.jpg)
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??
![Page 8: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/8.jpg)
… Common Problems with Bean Property
A getter must have no argumentA setter must have exactly one argumentThe type of a property must be consistent in both the getter and the setter
![Page 9: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/9.jpg)
Bean and JSP
JSP
<jsp:useBean>
Bean
<jsp:getProperty>
EL
<jsp:setProperty>
<c:set>
![Page 10: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/10.jpg)
Bean Tags and Attributes
jsp:useBean class id scope
page (default) request session application
jsp:getProperty name property
jsp:setProperty name property value param
![Page 11: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/11.jpg)
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” />
![Page 12: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/12.jpg)
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 aConstructor that takes no argument.
![Page 13: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/13.jpg)
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=“*” />
![Page 14: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/14.jpg)
Understand Scopes
Application scope – data is valid throughout the life cycle of the web applicationSession 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
![Page 15: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/15.jpg)
Scopes in Servlet
Application scope ServletContext
Session scope HttpSession
Request scope HttpServletRequest
Page scope (in JSP scriptlet) pageContext
![Page 16: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/16.jpg)
Need for EL
Using <jsp:getProperty> to access bean properties is tedious
EL
![Page 17: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/17.jpg)
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. <jsp:getProperty> vs. <%= expression %>
Java’s answer to scripting languages
Syntax: ${ expression }
![Page 18: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/18.jpg)
Example: BGColor.jsp Revisited
Use EL to access the bean properties
${ bean_name.property_name }
![Page 19: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/19.jpg)
Expression
LiteralsOperatorsVariables
![Page 20: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/20.jpg)
EL Literals
true, false23, 0x10, ...7.5, 1.1e13, ...“double-quoted”, ‘single-quoted’null
No char type
![Page 21: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/21.jpg)
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 [], ., ()
![Page 22: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/22.jpg)
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”}
![Page 23: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/23.jpg)
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.
![Page 24: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/24.jpg)
Implicit Objects in EL
pageContext servletContext session request response
param, paramValuesheader,headerValuescookieinitParam
pageScoperequestScopesessionScopeapplicationScope
![Page 25: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/25.jpg)
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
![Page 26: CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfe850/html5/thumbnails/26.jpg)
Limitation of EL
Only expressions, no statements, especially no control-flow statements
JSTL