Title: Leman Akoglu
115-415 Fall 2009 RecitationHomework 9
Building A Web Application Phase-II
2Phase-I
Phase-II
3Phase II
- You will develop
- JSP pages that handle user interactions
- Processing logic written in Java class
- Manipulating data in database, connect from JSP
to database using JDBC
4Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
5Recommended Schema for HW9
- users(login, password, name, email)
- photos(URL, owner)
- tags(URL, tagger, taggee, timestamp)
- likes(user1, user2)
6Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
7Typical Web Application Architecture
Web Server
Apache, Tomcat, Windows IIS
Client
Backend Server
http
Java Virtual Machine
Web app (JSP, ASP, PHP)
Users
Web app backend component
JDBC ODBC
Database Server
8Homework 9CMUBook architecture
Web Server newcastle.db.cs.cmu.edu
Client Browser
PostgreSQL Database Server newcastle.db.cs.cmu.edu
Tomcat 5.5
http
User
hw9 database
JDBC
CMUBook JSP, Java
9Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
10CMUBook architectureRegistration example
register.jsp
http//newcastle.db.cs.cmu.edu8080/lakoglu415/reg
ister.jsp
1
Web Server newcastle.db.cs.cmu.edu
Client Browser
PostgreSQL Database Server newcastle.db.cs.cmu.edu
Tomcat 5.5
4
2
JDBC exec. query java.sqlStatement.executeUpdate()
html page with input FORM
User
hw9 database
CMUBook JSP, Java
3
Submit FORM with login, name, password and email
register.jsp
5
JDBC insert succeeds
6
Html page with successful info
11Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
12JSP Basics
- Three primitives
- expressions
- directives
- declarations
13JSP Basics expressions
- JSP simply puts Java inside HTML pages.
- JSP is being turned into a Java file, compiled
and loaded - lt and gt enclose Java expressions, which are
evaluated at run time - Scriptlets blocks of Java code (lt and gt)
ltHTMLgt ltBODYgt Hello! The time is now lt new
java.util.Date() gt lt/BODYgt lt/HTMLgt
14JSP Basics directives
- JSP "directives" starts with lt_at_ characters.
- "page directive"
- lt_at_ page import"java.util.,java.text." gt
- include directive
- lt_at_ include file"hello.jsp" gt
15JSP Basics declarations
- The JSP code turns into a class definition. All
the scriptlets are placed in a single method of
this class. - Can add variable and method declarations to this
class. These variables and methods can later be
called from your scriptlets and expressions. - lt! and gt sequences enclose your declarations
lt_at_ page import"java.util." gt ltHTMLgt ltBODYgt
lt! Date theDate new Date()
Date getDate()
System.out.println( "In getDate() method" )
return theDate
gt Hello! The time is now lt
getDate() gt lt/BODYgt lt/HTMLgt
16 JSP Basics - communication w/ server
- A "request" in server-side processing refers to
the transaction between a browser and the server. - request.getRemoteHost()
- request.getParameter(login)
- A "response" is used to affect the response being
sent to the browser. - response.addCookie(cookie)
- response.sendRedirect(anotherUrl)
17Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
18How to connect to the database
register.jsp
lt Connection conn null Statement stmt
null ResultSet r null Class.forName("org.post
gresql.Driver") conn DriverManager.getConnectio
n ("jdbcpostgresql//localhost40123/hw9? userw
wwpasswordlakoglu415") stmt
conn.createStatement() r
stmt.executeQuery(your-SQL-query) if
(r.next()) session.setAttribute("login",
r.getString(1)) gt
19Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
20JSP Basics sessions method1
- Http protocol is a stateless protocol, that means
that it can't persist the data. - A session is an object associated with a visitor.
- Data can be put in the session and retrieved from
it, much like a Hashtable. - session.setAttribute( "theName", name )
- session.getAttribute( "theName" )
21JSP Basics cookies method2
(Optional)
- Cookies are commonly used for session management.
- short pieces of data sent by web servers to the
client browser - saved to clients hard disk in the form of a small
text file - helps the web servers to identify web users, by
this way server tracks the user.
22Cookie example
String usernamerequest.getParameter("username")
Cookie cookie new Cookie ("username",username)
cookie.setMaxAge(365 24 60
60)response.addCookie(cookie)
ltString cookieName "username"Cookie cookies
request.getCookies ()Cookie myCookie
nullif (cookies ! null) for (int i 0 i lt
cookies.length i) if (cookies
i.getName().equals (cookieName)) myCookie
cookiesi break gt ltpgtWelcome
ltmyCookie.getValue()gt.lt
23Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
24Exception Handling
try . catch (Exception ex)
ex.printStackTrace() out.println("Login
failed!") out.println("lta hreflogin.jspgtGo
back to login!lt/agt")
25Outline
- Recommended Schema for HW9
- A Typical Web Application Architecture
- CMUBook architecture
- JSP mini demo ? register.jsp
- JSP Basics
- How to connect to the database
- Session and cookie management ? login.jsp
- Exception handling
- Demo
26Lets put things together
lthtmlgt lt_at_ page import"java.sql."gt lt String
fullname request.getParameter("fullname")
String email request.getParameter("email")
String login request.getParameter("login")
String passwd request.getParameter("passwd")
String submit request.getParameter("submit")
if (submitnull) gt ltbodygt lth1gt CMUBook
Registration lt/h1gt ltform method"post"
action"register.jsp"gt Login Name ltinput
name"login" type"text" /gt ltbr /gt Full Name
ltinput name"fullname" type"text" /gt ltbr /gt
Password ltinput name"passwd" type"password" /gt
ltbr /gt Email ltinput name"email" type"text"
/gt ltbr /gt ltinput name"submit" type"submit"
value"Submit" /gt lt/formgt lt/bodygt
27lt else gt ltbodygt lt Connection conn
null Statement stmt null try
Class.forName("org.postgresql.Driver") conn
DriverManager.getConnection("jdbcpostgresql//
localhost40123/hw9?userwwwpasswordlakoglu415")
stmt conn.createStatement() int r
stmt.executeUpdate("INSERT INTO
users(login, fullname, passwd, email) VALUES ('"
login "','" fullname "','" passwd
"','" email "')") if (r1)
out.println("Registration successful!")
out.println("lta hreflogin.jspgtLog Inlt/agt")
else out.println("Registration
failed!") catch (Exception ex)
ex.printStackTrace() finally //this is
important. You should free up resources. Always.
In a finally block. stmt.close()
conn.close() gt lt/bodygt lt gt lt/htmlgt
28Lets put things together
lthtmlgt lt_at_ page import"java.sql."gt lt String
login request.getParameter("login") String
passwd request.getParameter("passwd") String
submit request.getParameter("submit") if
(submitnull) gt ltbodygt lth1gt CMUBook Login
Page lt/h1gt ltform method"post"
action"login.jsp"gt Login ID ltinput
name"login" type"text" /gt ltbr /gt Password
ltinput name"passwd" type"password" /gt ltbr /gt
ltinput name"submit" type"submit" value"Submit"
/gt lt/formgt lt/bodygt lt else gt
29ltbodygt lt Connection conn null Statement
stmt null ResultSet r null try
Class.forName("org.postgresql.Driver") conn
DriverManager.getConnection("jdbcpostgresql//l
ocalhost40123/hw9?userwwwpasswordlakoglu415")
stmt conn.createStatement() r
stmt.executeQuery("SELECT FROM users WHERE
login'" login "' and passwd'" passwd
"'") if (r.next())
session.setAttribute("login", r.getString(1))
response.sendRedirect("user.jsp") else
out.println("Login failed!!")
out.println("lta hreflogin.jspgtLog Inlt/agt")
out.println("lta hrefregister.jspgtRegisterlt/agt")
catch (Exception ex)
out.println("Login failed!") finally
//this is important. You should free up
resources. Always. In a finally block.
stmt.close() conn.close()
gt lt/bodygt lt gt lt/htmlgt
30Lets put things together
lthtmlgt lt_at_ page import"java.sql."gt ltbodygt lt
brgt ltbgtWelcome lt session.getAttribute( "login"
)gtlt/bgt ltbr /gt lt/bodygt lt/htmlgt