Вы находитесь на странице: 1из 27

Indy Java Users Group

March 27, 2001


Sallie Mae

6:00p.m

Mission Statement
Promote the use of the Java language and components across all levels of interest in the greater Indianapolis area, by serving as a resource for knowledge, experience and career opportunities.

Agenda
Pizza Welcome!! Announcements Joe Caron Quality Tip Performance Tip 6:00-6:15 6:15-6:30 6:30-7:30 7:30-7:45 7:45-8:00

Announcements
Next Meeting - April 24, 2002 Mike Burba Sun ONE Java Jam - Kurt Kirkham Feedback Results eGroups.yahoo.com
http://groups.yahoo.com/group/indyJug http://groups.yahoo.com/group/indyjug_opensource

BOT Contest Deadline March 31


http://developer.digi-net.com

Announcements
CRN Annual Survey 1) Call 1-800-939-6206 2) Ask for Amy Johnson 3) Request your gift Sweatshirt Cap T-Shirt Visit www.metafacts.com Visit www.crn.com

Mark Steenbarger Indy Jug Joe Caron KingArthurFlour.com Mike Slattery SearchSoft.net Frank Morton Base2Inc.com

Presentation Lineup

Joe Caron Indianapolis Java Users Group March 27, 2002

JDBC & Servlets/JSP

Database access through the web


s s s s s

Sample application overview JDBC Fundamentals Servlet/JSP Fundamentals Implementation & sample code Pitfalls, Surprises, Work-arounds

Sample Application
s s s

Message Board Membership Based Pass membership info to catalogue site for quick check-out Accumulate bakers points redeemable against online orders Member recipe box

s s s s s

JSP for presentation Servlets & other java for app logic MySQL as the RDBMS Tomcat as Servlet/JSP container Linux (Red Hat 6.2)

Sample Application

s s s s s

www.bakingcircle.com Staging Server Entity Relationship Diagram Application Diagram Presentation Diagram

Sample Application

JDBC Fundamentals
s s

Standard means of accessing any RDBMS for which there is a JDBC driver Drivers which implement Suns JDBC standard capture a DB connection as Java Object Change DBMS without changing any application code (except driver) Sun JDBC API overview

JDBC Fundamentals
s

Key objects
Connection x Statement x PreparedStatement x ResultSet / Rowset x ResultSet MetaData x DB MetaData
x

JDBC Fundamentals
s s s s s s s

Sun JDBC overview Basic Example - MySQL Basic Example - ORACLE Statement vs. PreparedStatement Stand-Alone Example API Documentation JDBC Optional: DataSource

Servlet/JSP Fundamentals
s

Servlet Container (Tomcat, for example), working in conjunction with Web server (Apache or IIS) Integrated in J2EE compliant server (BEA, Iplanet, Orion) Many options regarding both the products and how to set them up

Servlet/JSP Fundamentals
Common Set-Up
Servlet Engine
Redire ct based on conf file

Web Server

.jsp & /servlet/

.html & others

Common directory structure

Client

Servlet/JSP Fundamentals
s

s s

Servlets like any other Java class, except: Must extend HttpServlet Must contain a doPost or doGet method Come equipped to handle the laborious points of http Http requests and responses are made into Java objects Best alternative to traditional CGI

Servlet/JSP Fundamentals
s

Anatomy of a Servlet
Optional init method, executes the first time a servlet is invoked x doGet/doPost methods execute when request is made x method signature accepts http request and response as parameters x Optional destroy method x www.servlets.com Samples
x

Servlet/JSP Fundamentals
s

One instance of Servlet for each Servlet name Same instance serves all requests to that name Instance members persist across all requests to that name. Local /block variables in doPost & doGet are unique to each request

Servlet/JSP Fundamentals
s s s s s

Instance members vs local variables Major request and response methods More major request and response me Making use of servlet context Keeping sessions: the bane of all Web

JSPs: Servlets in disguise...


s s

Better alternative for presentation. Insert special brackets into HTML page to mark off Java code. A JSP is compiled into a Servlet the first time it is called. Crash course in JSP syntax

JSP as presentation model


App logic

Servlet
Performs the hard work

Helper objects Persistent bean objects DB Connections, etc

Client request
Presentation
JSP Unsuccessful results JSP Successful results

Simple calls to bean properties

No servlet need if bean

Subsequent client request

JSP alternatives
s

s s

JSP can still result in unwieldy code creeping into HTML Most HTML editors dont like it JSP:Taglibs and other templating programs may help. Much depends on how HTML documents are produced and what business logic you are presenting

In Depth Examples
s s s s s s

Encapsulation of SQL calls List results and view thread detail Post reply and add to favorites Set all bean properties w/ request Edit users Query central

Pitfalls, surprises, and work-arounds


s

HTTP works via requests and responses. Client caching of responses poses a problem. Guard against evil use of the back button Dont assume everyones client will respond properly to the defaults Guard against easy spoofs of any GET request which alters data.

Вам также может понравиться