Download - Week6 Intro Server Side II
-
8/11/2019 Week6 Intro Server Side II
1/23
Server-Side Web Development
Server-Side Web Development
JavaBeans; basic concepts and syntax
23rdFebruary 2005
Bogdan L. Vrusias
-
8/11/2019 Week6 Intro Server Side II
2/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 2
Contents
JavaBean definition and syntax
Sharing JavaBean components
JavaBeans Examples
Session Tracking
-
8/11/2019 Week6 Intro Server Side II
3/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 3
Invoking Java code from JSP
Call Java code directly
Call Java code indirectly
Use beans
Use the Model-View-Controller architecture
Use the JSP expression language (EL)
Use custom tags
Simple application or
small development team
Complex application or
large development team
-
8/11/2019 Week6 Intro Server Side II
4/23
-
8/11/2019 Week6 Intro Server Side II
5/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 5
JavaBeans: Advantages
No Java syntax in the JSP
Stronger separation between content and presentation
Good for separating Web and Java developers
Simple object sharing
Due to the JSP bean constructs
Convenient correspondence between request parameters
and object properties
Simple process of reading request parameters
-
8/11/2019 Week6 Intro Server Side II
6/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 6
JavaBeans: Basics
A bean class must have a zero-argument (default)
constructor.
A bean should have no public instance variables (fields).
Persistent values should be accessed through methodscalled setXxxand getXxx(or isXxxfor Boolean).
-
8/11/2019 Week6 Intro Server Side II
7/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 7
JavaBeans: Basic Tasks
In a JSP there are three main JavaBean constructs:
jsp:useBean
jsp:getProperty
Jsp:setProperty
(CAREFUL)
Otherwise
-
8/11/2019 Week6 Intro Server Side II
8/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 8
JavaBeans: Basic Tasks
Properties of a JavaBean class must simply be accessible using public
methods that conform to certain conventions:
For each readable property, the bean must have a method of the formPropertyClass getProperty() { ... }
For each writable property, the bean must have a method of the form
setProperty(PropertyClass pc) { ... }
NOTE 1: beanNamemust be the same as the one specified in a
useBeanelement (using the idattribute), and there must be a
getPNamemethod in the JavaBeans component. NOTE2:The official version of a Java Bean class extends the Object
class and implements the Serializableclass. But for simplicity
reasons we will not be doing that.
-
8/11/2019 Week6 Intro Server Side II
9/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 9
JavaBean Example I
StringBean.java
package webtech.w6;
public class StringBean {
private String message = "No message";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
-
8/11/2019 Week6 Intro Server Side II
10/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 10
JavaBean Example II
StringBean.jsp
...
Initial value (getProperty):
Initial value (JSP expression):
Value after setting property with scriptlet:
...
-
8/11/2019 Week6 Intro Server Side II
11/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 11
Sharing Beans
(Default)
Bean is not shared and a new bean is created for each request
Same as "page" scope but, two JSP pages or a JSP page and a servlet willshare the bean when you use jsp:include
Bean is shared within a session
Bean is shared by all servlets and JSP pages in a Web application
-
8/11/2019 Week6 Intro Server Side II
12/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 12
Sharing Beans: Example
This example demonstrates the use of JavaBeans and how the beans are
shared.
There are four possibilities:
Page
Request
Session
Application
For this example these is one JavaBean that stores two parameters, and five
JSP pages to demonstrate the use of the bean.
To test the bean sharing we will try to pass parameters by directly entering the
parameters within the URL:
scope_page.jsp?sport=cricket&rating=boring
Parameter 1 Parameter 2
-
8/11/2019 Week6 Intro Server Side II
13/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 13
Sharing Beans: The JavaBean
msgBean.javapackage webtech.w6;
public class msgBean {
private String sport = "football";
private String rating = "fun";
public String getRating() {return rating;
}
public void setRating(String newRating) {
rating = newRating;
}
public String getSport() {return sport;
}
public void setSport(String newSport) {
sport = newSport;
}
}
-
8/11/2019 Week6 Intro Server Side II
14/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 14
Sharing Beans: Page Scope Example
scope_page.jsp
The sport:
Has rating:
-
8/11/2019 Week6 Intro Server Side II
15/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 15
Sharing Beans: Request Scope Example
scope_request.jsp
The sport:
Has rating:
-
8/11/2019 Week6 Intro Server Side II
16/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 16
Sharing Beans: Request Scope Example
snippet.jsp
The sport:
Has rating:
-
8/11/2019 Week6 Intro Server Side II
17/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 17
Sharing Beans: Session Scope Example
scope_session.jsp
The sport:
Has rating:
-
8/11/2019 Week6 Intro Server Side II
18/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 18
Sharing Beans: Application Scope Example
scope_application.jsp
The sport:
Has rating:
-
8/11/2019 Week6 Intro Server Side II
19/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 19
Installing JavaBeans
Each JavaBean should be located within your web
application under:WEB-INF/classes
Or, if the bean is within a package (recommended), then:WEB-INF/classes/subdirectoryMatchingPackageName
Or, if the bean is within a .JAR, then place the .JAR in:
WEB-INF/lib
-
8/11/2019 Week6 Intro Server Side II
20/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 20
JSP Session Tracking
A session can be defined as a series of related interactions
between a single client and the server, which take place
over a period of time.
A session object can be used for storing and retrieving
information.
Every time the client accesses the resources on the server,
the client provides the session ID that was assigned by the
server.
A session has a one-to-one association between a clientand the server.
-
8/11/2019 Week6 Intro Server Side II
21/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 21
JSP Session Tracking
Session tracking is a technique for maintaining user
information across pages:
HTTP information (not used much privacy issues)
Hidden fields (popular but again, privacy issues)
Extended Path information and URL-rewriting ( privacy issues)
Next
S Sid b l
-
8/11/2019 Week6 Intro Server Side II
22/23
Server-Side Web Development
23rdFebruary 2006 Bogdan L. Vrusias 2006 22
JSP Session Tracking
Cookies (data can be encrypted)
Cookie uid = new Cookie("uid", "234ff543333c");
response.addCookie(uid);
Session (data can be encrypted)
session.putValue("user_id", user_id);
session.getValue("user_id")
JavaBeans using the session scope
S Sid W b D l
-
8/11/2019 Week6 Intro Server Side II
23/23
Server-Side Web Development
23rd February 2006 Bogdan L Vrusias 2006 23
Closing
Questions???
Remarks???
Comments!!!
Evaluation!