IST350 / CSC485 Exam II - Review Sheet

mobdescriptiveΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 9 μήνες)

149 εμφανίσεις


1

IST350

/ CSC485

Exam
II

-

Review Sheet


Design Patterns
(Chapters 7, 8)


What are design patterns? How to describe a design pattern? (7.1)


Explain the
following design patterns:

-

The
Composite pattern as used in Java AWT API,
for
Component and
Container
cl
asses
.

(8.3.2)

-

The Singleton pattern as described in 7.1.1.

-

The Template Method pattern as used in the ActionServlet design in out project.
(Design documents and 7.2.2)


Java Database Connectivity (JDBC)
(
12.3;
Labs 8 and 9; and
[Liang]
Chapter
1
2)


Describ
e
following JDBC interfaces:

-

Connection

-

Statement

-

PreparedStatement

-

Result


How do you create a JDBC connection to an ODBC service or to an Oracle server? What
is the syntax for a JDBC URL?


How do you create an instance of Statement? How do you execute
a statement? Does
every execution of the statement return a ResultSet?


When do you use a PreparedStatement instead of a Statement? Describe the differences
between using the two interfaces to execute a select query using examples.


How do you retrieve va
lues in a ResultSet?


JavaServer Pages
(
[Liang]
Chapter
15
)


How is a
JSP page processed
?


What are a JSP expression, a JSP scriptlet, a JSP declaration? How do you write these
constructs in JSP?


How do you write a statement to forward requests to another

JSP? How do you send
request
s

to a Servlet?


Explain how to pass information to a JSP page by using a JavaBeans component?


How
do

you track client information throughout the session?


2

Servlets

([Liang]Chapter 13)


Is a servlet a standalone program? Expl
ain how servlets work in the Sun ONE
Studio
environment
. Why is it necessary to create a Web module in IDE?


How do you retrieve information passed in from a HTML request?
Or s
et values that you
need to send to the next JSP page?

How do you set an objec
t to the client’s session?


Explain how does the project design reflect the Model
-
View
-
Controller (MVC) pattern?
Use examples to support your point.


A specific
a
ction
s
ervlet, such as LoginAction, just needs to implement an execute
method. How does it w
ork that way?


JavaBeans and
Refl
ection

(
Lab 5, Java API, and Project Documents
)


Describe
the concept of Java reflection
.

Explain how it works in sample programs such
the one used in Lab 5.


Explain the design of the
l
oadPrevPage me
thod of the ActionServ
let class. What are the
requirements for the
form bean
s and the JSP pages?


Put
ting

Everything Together



Design and develop a set of component, including a JSP page, a form bean, and
an

action
servlet, for a given scenario, as described in the project do
cuments.



3

Hints


JDBC

read online java api (http://java.sun.com/j2se/1.4.2/docs/api/) and jdbc tutorials
(http://java.sun.com/docs/books/tutorial/jdbc/index.html) when needed.


I
nterfaces

Read 12.2 for an overview. The paragraph starting with
"The inter
faces define a
framework ..."

gives a general description. 12.4 through 12.6 discussed the four
interfaces being asked individually.


D
rivers and
C
onnection

12.3 describes the steps needed to write Java code talking to a db, from registering a
driver, s
etting up the connection, to creating and executing statements, to process results
returned. Also use sample code written for Lab 9.


To connect to an Oracle db, use the thin client drvier (
oracle:thin
) for Oracle 8 and
above. Read 12.5.1 for details.


Statements

Statements (including prepared ones) are created by using an established connection. See
examples from lab 9 or the following web page for details

http://java.sun
.com/docs/books/tutorial/jdbc/basics/tables.html



Use
executeQuery

for select queries;
executeUpdate

for insert, delete or update
statements; and execute (for unknown statements or stored procedures) Check online API
to see the three (overloaded) methods

have different return types. Only
executeQuery

returns a
ResultSet
.


Prepared statements

Used to improve performance when making the same query statement repeatedly.

Explanations available on
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html



Result set

Basically, use
next

to move to the next "row" and
getXXX

methods to get a "field"



4

JSP

See handout and JSP online tutorials (if needed) at

http://java.sun.com/j2ee/tutorial/1_3
-
fcs/doc/jspintro.html



How is JSP processed?

Read 15.3


Scripting constructs

15.5


Send requests to JSPs/servlets

See example 15.3 in 15.7. Basically, set
the target page/servlet in the action attribute; use
the predefined variable request to retrieve parameters by names, such as

request.getParameter("number")

You may also use the following syntax to forward request

<jsp:forward page="..">


Use
J
ava
B
eans

Des
cribed in 15.8. Use the following syntax to instantiate a bean and then use it
throughout the page is scriptlets:

<jsp:usebean id=".." scope=".." class="..">


Tracking session

Use the predefined variable: session, which is an HttpSession object. Use
sess
ion.getAttribute("<attr
-
name>")

to return an object that has been set to the
corresponding
attr
-
name

in a previous web component (JSP or Servlet)



5

Servlets

See handout and online tutorials (if needed) at

http://java.sun.com/j2ee/tutorial/1_3
-
fcs/doc/Servlets.html
.


Servlet environment

A servlet is not a standalone program. It needs to run in a web server that supports the
servlet technology, such as the Tomcat server. A
WEB
-
INF

folder that contains an XML
file
web.xml

needs to be put under an appropriate directory in the web server’s file
structure.
The
web.xml

file defines the names of servlets that will be used in a web
-
based application, as well as class files and URL mappin
gs for these servlets. This file
will be used to register the servlets to the web server when the server starts up. Shutting
down and then restarting up the web server is required after new servlets are added or
changes are made to existing servlets.


The
Sun ONE Studio

(or
NetBeans
) IDE has a Tomcat server embedded in it. It can
help programmers to create and administrate files that are necessary by creating a
Web
Module
. Interfaces are provided to compile and execute Servlet code, and
to
restart the

Tomcat server when requested.


Http
Servlet
Request

and
Http
Servlet
Response

We’ve learned to use the following APIs to process client requests:


ServletRequest

java.lang.Object

getAttribute
(java.lang.String

name)



Returns the value of the named attribute as
an
Object
, or
null

if no attribute of the given name
exists.

java.lang.String

getParameter
(java.lang.String

name)



Returns the value of a request parameter as
a
String
, or
null

if the parameter does not exist.

void

setAttribute
(java.lang.String

name,
java.lang.Object

o)



Stores an attribute in this request.

HttpServletRequest

extends Servl
etRequest


HttpSession

getSession
()



Ret
urns the current session associated with
this request, or if the request does not have a
session, creates one.

HttpServletResponse

void

sendRedirect
(java.lang.String

location)



Sends a temporary redirect response to the client using the
specified redirect location URL.

HttpSession

java.lang.Object

getAttribute
(java.lang.String

name)



Returns the object bound with the specified
name in this session, or
null

if no object is bound
under the name.


6


void

setAttribute
(java.lang.String

name,
java.lang.Object

value)



Binds an object to this session, using the
name specified.


MVC as us
ed in project design

The MVC pattern has been used in the design at two different levels. The overall
architecture has three
tier
s: each of which is responsible for presentation (view), business
logic (controller), and
data persistence (model). The overa
ll design is flexible enough to
prevent changes to one tier from rippling over to another tier. Within the web
-
based
presentation tier, the MVC pattern is used again: a form bean class (model), a JSP page
(view), and a servlet (controller) are used togeth
er to separate three different types of
responsibilities. Therefore, a HTML page programmer can change the layout in the JSP
page independently, so can a Java programmer change the site navigation flow defined in
a servlet.


Example:
Login.jsp
,
LoginForm.
java
, and
LoginAction.java
.


Action servlets

All servlets in the application extend the
ActionServlet
, which has a template method
with the following logic defined:


loadPrevPage(request);

nextPage = execute(prevPage);

request.getSession().setAttribute("fo
rmObj", nextPage);

response.sendRedirect(getFormName(nextPage));


The method first retrieves information embedded in the form bean from a previous JSP
page through the request object. The next step is to invoke the execute method to
determined the right n
ext JSP to go to and to generate the corresponding form bean object
(returned as
nextPage
). It then set the new form bean to the client’s session and
dispatch to the right JSP page.


The method is called a template method since it defines when need to
be done and in
what order. The
ActionServlet

cl
ass is defined as abstract, so is the
execute

method.
The specific action servlet classes need to implement the execute method to define
exactly how to validate input data from the previous JSP page and how
to choose the next
page accordingly.



7

Reflection

and Introspection

A Java bean class is a class that meets all the following requirements:



Must be
public



Has a public default constructor defined locally or a superclass has one



Must implement the
Serializ
able

interface

By convention, a bean class should provide getters and setters for its instance variables
(also known as properties). It may also have events with public registration methods that
enable it to add and remove listeners.



The
java.beans.Intr
ospector

class provides a standard way for tools to learn
about the properties, events, and methods supported by a target Java Bean.


protected void loadPrevPage(HttpServletRequest request) {


prevPage =


(CtbsForm)
request.getSession().getAttribute("
formObj")
;




BeanInfo pageInfo = null;


//get the page attributes by class reflection


try {


pageInfo =

//#2




//#1


Introspector.getBeanInfo(
prevPage.
getClass());


}catch (IntrospectionException ise) {}


PropertyDescriptor[] pds =


pageInfo
.getPropertyDescriptors()
;


//#
3


try {



for (int i=0; i<pds.length; i++) {


Method m = pds[i]
.getWriteMethod()
;

//#
4


Object[] args =


{request.getParameter(pds[i].getName())};


m.invoke(prevPage, args);




//#
5


}


} catch (Exception iae) {}

}


#1
-
The
getClass

method
. Instances of the class
Class

represent classes and interfaces in
a running Java application. These instances cannot be created using a constructor. They
can be returned by invoking the
getC
lass

method on corresponding objects.


#2
-
The
get
BeanInfo

method
. Introspect on a Java Bean and learn about all its features,
including properties, exposed methods, and events. All these info will be encapsulated in
the
BeanInfo

object. The one argumen
t version returns all features defined locally or
inherited from all superclasses. A two
-
argument version is available to allow a
stopClass

to be specified.


#3
-
The
getPropertyDescriptors

method
. This method returns an array of
java.beans

.
PropertyDescrip
tors

describing the editable properties supported by this bean.


#4
-
The
get
Write
Method

method
.
Gets the method that should be used to write the
property value. The return value is in

the
java.lang.reflect.
M
ethod

type.


#5
-
The
invoke

method
. Invokes the un
derlying method represented by this
Method

object, on the specified object (e.g.
prevPage
) with the specified parameters (e.g.
args
).