Transcript Document

Introduction to Server-Side
Web Development
Introduction to Server-Side Web
Development
Introduction to Server-Side Web JavaBeans; basic
concepts and syntax
24th February 2005
Bogdan L. Vrusias
[email protected]
Introduction to Server-Side
Web Development
Contents
• JavaBeans
• Examples
• Sessions
24th February 2005
Bogdan L. Vrusias © 2005
2
Introduction to Server-Side
Web Development
Invoking Java code from JSP
Simple application or
small development team
• Call Java code directly
• Call Java code indirectly
• Use beans
• Use the Model-View-Controller architecture
Complex application or
large development team
24th February 2005
• Use the JSP expression language (EL)
• Use custom tags
Bogdan L. Vrusias © 2005
3
Introduction to Server-Side
Web Development
JavaBeans
• JavaBeans is a portable (platform-independent) component model
written in Java and was developed in collaboration with industry
leaders.
• JavaBeans components are Java classes that can be easily reused and
composed together into applications.
• Any Java class that follows certain design conventions can be a
JavaBeans component.
• JavaServer Pages technology directly supports using JavaBeans
components with JSP language elements.
• JavaBeans will minimize the code on the JSP.
24th February 2005
Bogdan L. Vrusias © 2005
4
Introduction to Server-Side
Web Development
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
24th February 2005
Bogdan L. Vrusias © 2005
5
Introduction to Server-Side
Web Development
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 methods
called setXxx and getXxx (or isXxx for Boolean).
24th February 2005
Bogdan L. Vrusias © 2005
6
Introduction to Server-Side
Web Development
JavaBeans: Basic Tasks
•
In a JSP there are three main JavaBean constructs:
– jsp:useBean
<jsp:useBean id="beanName" class="package.classname"
scope="scope"/>
– jsp:getProperty
<jsp:getProperty name="beanName" property="pName"/>
– Jsp:setProperty
<jsp:setProperty name="beanName" property="pName"
value="pValue" />
<jsp:setProperty name="beanName" property="*" /> (CAREFUL)
• Otherwise
<%= beanName.getPName() %>
<% beanName.setPName() %>
24th February 2005
Bogdan L. Vrusias © 2005
7
Introduction to Server-Side
Web Development
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 form
PropertyClass getProperty() { ... }
– For each writable property, the bean must have a method of the form
setProperty(PropertyClass pc) { ... }
• NOTE: beanName must be the same as that specified in a useBean
element (using the id attribute), and there must be a getPName
method in the JavaBeans component.
24th February 2005
Bogdan L. Vrusias © 2005
8
Introduction to Server-Side
Web Development
JavaBean Example I
StringBean.java
package webtech;
public class StringBean {
private String message = "No message";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
24th February 2005
Bogdan L. Vrusias © 2005
9
Introduction to Server-Side
Web Development
JavaBean Example II
StringBean.jsp
...
<jsp:useBean id="stringBean" class="webtech.StringBean" />
<p>Initial value (getProperty): <I><jsp:getProperty
name="stringBean" property="message" /></I></p>
<p>Initial value (JSP expression): <I><%=
stringBean.getMessage() %></I></p>
<jsp:setProperty name="stringBean" property="message"
value="Best string bean: Fortex" />
<p>Value after setting property with setProperty:
<I><jsp:getProperty name="stringBean" property="message" />
</I></p>
<% stringBean.setMessage("My favorite: Kentucky Wonder"); %>
<p>Value after setting property with scriptlet: <I><%=
stringBean.getMessage() %></I></p>
...
24th February 2005
Bogdan L. Vrusias © 2005
10
Introduction to Server-Side
Web Development
Sharing Beans
• <jsp:useBean … scope="page"/> (Default)
– Bean is not shared and a new bean is created for each request
• <jsp:useBean … scope="request"/>
– Same as "page" scope but, two JSP pages or a JSP page and a servlet will
share the bean when you use jsp:include
• <jsp:useBean … scope="session"/>
– Bean is shared within a session
• <jsp:useBean … scope="application"/>
– Bean is shared by all servlets and JSP pages in a Web application
24th February 2005
Bogdan L. Vrusias © 2005
11
Introduction to Server-Side
Web Development
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.
24th February 2005
Bogdan L. Vrusias © 2005
12
Introduction to Server-Side
Web Development
Sharing Beans: The JavaBean
BakedBean.java
package webtech;
public class BakedBean {
private String level = "half-baked";
private String goesWith = "hot dogs";
public String getLevel() { return(level); }
public void setLevel(String newLevel) {
level = newLevel;
}
public String getGoesWith() {
return(goesWith); }
public void setGoesWith(String dish) {
goesWith = dish;
}
}
24th February 2005
Bogdan L. Vrusias © 2005
13
Introduction to Server-Side
Web Development
Sharing Beans: Page Scope Example
BakedBeanDisplay-page.jsp
…
<H1>Baked Bean Values: page-based Sharing</H1>
<jsp:useBean id="pageBean" class="webtech.BakedBean" />
<jsp:setProperty name="pageBean" property="*" />
<H2>Bean level:
<jsp:getProperty name="pageBean" property="level"
/></H2>
<H2>Dish bean goes with:
<jsp:getProperty name="pageBean" property="goesWith"
/></H2>
…
24th February 2005
Bogdan L. Vrusias © 2005
14
Introduction to Server-Side
Web Development
Sharing Beans: Request Scope Example
BakedBeanDisplay-request.jsp
…
<H1>Baked Bean Values: request-based Sharing</H1>
<jsp:useBean id="requestBean" class="webtech.BakedBean"
scope="request" />
<jsp:setProperty name="requestBean" property="*" />
<H2>Bean level:
<jsp:getProperty name="requestBean" property="level" /></H2>
<H2>Dish bean goes with:
<jsp:getProperty name="requestBean" property="goesWith"
/></H2>
<jsp:include page="BakedBeanDisplay-snippet.jsp" />
…
24th February 2005
Bogdan L. Vrusias © 2005
15
Introduction to Server-Side
Web Development
Sharing Beans: Request Scope Example
BakedBeanDisplay-snippet.jsp
<H1>Repeated Baked Bean Values: request-based Sharing</H1>
<jsp:useBean id="requestBean" class="webtech.BakedBean"
scope="request" />
<H2>Bean level:
<jsp:getProperty name="requestBean" property="level" /></H2>
<H2>Dish bean goes with:
<jsp:getProperty name="requestBean" property="goesWith"
/></H2>
24th February 2005
Bogdan L. Vrusias © 2005
16
Introduction to Server-Side
Web Development
Sharing Beans: Session Scope Example
BakedBeanDisplay-session.jsp
…
<H1>Baked Bean Values: session-based Sharing</H1>
<jsp:useBean id="sessionBean" class="webtech.BakedBean"
scope="session" />
<jsp:setProperty name="sessionBean" property="*" />
<H2>Bean level:
<jsp:getProperty name="sessionBean" property="level" /></H2>
<H2>Dish bean goes with:
<jsp:getProperty name="sessionBean" property="goesWith"
/></H2>
…
24th February 2005
Bogdan L. Vrusias © 2005
17
Introduction to Server-Side
Web Development
Sharing Beans: Application Scope Example
BakedBeanDisplay-application.jsp
…
<H1>Baked Bean Values: application-based Sharing</H1>
<jsp:useBean id="applicationBean" class="webtech.BakedBean"
scope="application" />
<jsp:setProperty name="applicationBean" property="*" />
<H2>Bean level:
<jsp:getProperty name="applicationBean" property="level"
/></H2>
<H2>Dish bean goes with:
<jsp:getProperty name="applicationBean" property="goesWith"
/></H2>
…
24th February 2005
Bogdan L. Vrusias © 2005
18
Introduction to Server-Side
Web Development
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
24th February 2005
Bogdan L. Vrusias © 2005
19
Introduction to Server-Side
Web Development
JSP Sessions I
• 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 client
and the server.
24th February 2005
Bogdan L. Vrusias © 2005
20
Introduction to Server-Side
Web Development
JSP Sessions II
• Session tracking is a technique for maintaining user
information across pages:
– HTTP information (not used much… privacy issues)
– Hidden fields (very popular… but again privacy issues)
<input type=“hidden” name=“myKey”
value=“myValue” >
– Extended Path information and URL-rewriting ( privacy issues)
<a href=“/myPage.jsp?login=james&item=book_1”>
Next</a>
– 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")
24th February 2005
Bogdan L. Vrusias © 2005
21
Introduction to Server-Side
Web Development
Closing
• Questions???
• Remarks???
• Comments!!!
• Evaluation!
24th February 2005
Bogdan L. Vrusias © 2005
22