Leman Akoglu - PowerPoint PPT Presentation

About This Presentation
Title:

Leman Akoglu

Description:

Login Name: input name='login' type='text' / br ... Password: input name='passwd' type='password' / br ... br b Welcome %= session.getAttribute( 'login' ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 31
Provided by: carnegieme
Learn more at: http://www.cs.cmu.edu
Category:
Tags: akoglu | courses | leman

less

Transcript and Presenter's Notes

Title: Leman Akoglu


1
15-415 Fall 2009 RecitationHomework 9
Building A Web Application Phase-II
  • Leman Akoglu
  • 11/11/2009

2
Phase-I
Phase-II
3
Phase 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

4
Outline
  • 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

5
Recommended Schema for HW9
  • users(login, password, name, email)
  • photos(URL, owner)
  • tags(URL, tagger, taggee, timestamp)
  • likes(user1, user2)

6
Outline
  • 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

7
Typical 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
8
Homework 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
9
Outline
  • 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

10
CMUBook 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
11
Outline
  • 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

12
JSP Basics
  • Three primitives
  • expressions
  • directives
  • declarations

13
JSP 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
14
JSP 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

15
JSP 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)

17
Outline
  • 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

18
How 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
19
Outline
  • 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

20
JSP 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" )

21
JSP 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.

22
Cookie 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
23
Outline
  • 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

24
Exception Handling
try . catch (Exception ex)
ex.printStackTrace() out.println("Login
failed!") out.println("lta hreflogin.jspgtGo
back to login!lt/agt")
25
Outline
  • 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

26
Lets put things together
  • register.jsp

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
27
lt 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
28
Lets put things together
  • login.jsp

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
29
ltbodygt 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
30
Lets put things together
  • user.jsp

lthtmlgt lt_at_ page import"java.sql."gt ltbodygt lt
brgt ltbgtWelcome lt session.getAttribute( "login"
)gtlt/bgt ltbr /gt lt/bodygt lt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com