Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II...

30
Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science

Transcript of Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II...

Page 1: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Leman Akoglu11/11/2009

15-415 Fall 2009 Recitation

Homework 9

Building A Web Application Phase-II

School of Computer Science

Page 2: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

description

req. anal.

top level I.F.D.

sys. anal.

conc. mod. impl.+test.

task + doc forms.

schema.code.

task emul.

tests

user’s man.

pseudo-code

Phase-I Phase-II

2

Page 3: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Phase IIYou will develop

JSP pages that handle user interactionsProcessing logic written in Java classManipulating data in database, connect from JSP to

database using JDBC

3

Page 4: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

4

Page 5: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Recommended Schema for HW9

users(login, password, name, email)photos(URL, owner)tags(URL, tagger, taggee, timestamp)likes(user1, user2)

5

Page 6: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

6

Page 7: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Users

Web app

(JSP, ASP, PHP)

Apache, Tomcat,

Windows IIS

Web Server

Java Virtual Machine

Web app backend

component

Backend Server

Database

Server

Client

Typical Web Application Architecture

http

JDBC

ODBC

7

Page 8: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Homework 9:CMUBook architecture

Tomcat 5.5

CMUBook

JSP, Java

Web Server

newcastle.db.cs.cmu.ed

uPostgreSQL

Database Servernewcastle.db.cs.cmu.e

du

hw9 database

Client

Browser

User

http

JDBC

8

Page 9: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

9

Page 10: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

CMUBook architectureRegistration example –register.jsp

Tomcat 5.5

CMUBook

JSP, Java

Web Servernewcastle.db.cs.cmu.edu

PostgreSQL

Database Server

newcastle.db.cs.cmu.edu

hw9 database

Client

Browser

User

http://newcastle.db.cs.cmu.edu:8080/lakoglu415/register.jsp

register.jsp

html page with input FORM

Submit FORM with login, name, password and email

JDBC exec. queryjava.sqlStatement.executeUpdate()

JDBC insert succeeds

Html page with successful info

1

2

3

4

5

610

Page 11: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

11

Page 12: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP BasicsThree primitives

– expressions– directives– declarations

12

Page 13: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP Basics – expressionsJSP simply puts Java inside HTML pages.JSP is being turned into a Java file, compiled

and loaded

<%= and %> enclose Java expressions, which are evaluated at run time

Scriptlets: blocks of Java code (<% and %>)

<HTML> <BODY> Hello!  The time is now <%= new java.util.Date() %> </BODY> </HTML>

13

Page 14: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP Basics – directivesJSP "directives" starts with <%@

characters.

"page directive": <%@ page import="java.util.*,java.text.*" %>“include directive”:<%@ include file="hello.jsp" %>

14

Page 15: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP Basics – declarationsThe 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.

<%! and %> sequences enclose your declarations

15

<%@ page import="java.util.*" %> <HTML> <BODY> <%!     Date theDate = new Date();     Date getDate()     {         System.out.println( "In getDate() method" );         return theDate;     } %>

Hello!  The time is now <%= getDate() %> </BODY> </HTML>

Page 16: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

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);

16

Page 17: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

17

Page 18: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

How to connect to the database

18

<% …Connection conn = null;Statement stmt = null;ResultSet r = null;

Class.forName("org.postgresql.Driver");conn = DriverManager.getConnection ("jdbc:postgresql://localhost:40123/hw9?user=www&password=lakoglu415"); stmt = conn.createStatement(); r = stmt.executeQuery(your-SQL-query); if (r.next()) { session.setAttribute("login", r.getString(1));

…%>

register.jsp

Page 19: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

19

Page 20: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP Basics – sessions – method#1Http 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" )

20

Page 21: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

JSP Basics – cookies – method#2

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.

21

(Optional)

Page 22: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Cookie exampleString username=request.getParameter("username");Cookie cookie = new Cookie ("username",username);cookie.setMaxAge(365 * 24 * 60 * 60);response.addCookie(cookie);

<%String cookieName = "username";Cookie cookies [] = request.getCookies ();Cookie myCookie = null;if (cookies != null){

for (int i = 0; i < cookies.length; i++) {if (cookies [i].getName().equals (cookieName)){myCookie = cookies[i];break;}

}}%> <p>Welcome: <%=myCookie.getValue()%>.<%

22

Page 23: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

23

Page 24: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Exception Handling

24

try {

….

} catch (Exception ex) { ex.printStackTrace();

out.println("Login failed!"); out.println("<a href=login.jsp>Go back to login!</a>");

}

Page 25: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

OutlineRecommended Schema for HW9

A Typical Web Application ArchitectureCMUBook architectureJSP mini demo register.jsp

JSP BasicsHow to connect to the databaseSession and cookie management login.jspException handling

Demo

25

Page 26: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Lets put things together…

26

register.jsp<html><%@ page import="java.sql.*"%><% 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 (submit==null) {%><body><h1>CMUBook Registration</h1><form method="post" action="register.jsp"> Login Name: <input name="login" type="text" /> <br /> Full Name: <input name="fullname" type="text" /> <br /> Password: <input name="passwd" type="password" /> <br /> Email: <input name="email" type="text" /> <br /> <input name="submit" type="submit" value="Submit" /></form></body>

Page 27: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

27

<% } else {%><body><% Connection conn = null; Statement stmt = null; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql://localhost:40123/hw9?user=www&password=lakoglu415"); stmt = conn.createStatement(); int r = stmt.executeUpdate("INSERT INTO users(login, fullname, passwd, email) VALUES ('" + login + "','" + fullname + "','" + passwd + "','" + email + "')"); if (r==1) { out.println("Registration successful!"); out.println("<a href=login.jsp>Log In</a>");

} 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(); }%></body><% }%></html>

Page 28: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Lets put things together…

28

login.jsp<html><%@ page import="java.sql.*"%><% String login = request.getParameter("login"); String passwd = request.getParameter("passwd"); String submit = request.getParameter("submit");

if (submit==null) {%>

<body><h1>CMUBook Login Page</h1>

<form method="post" action="login.jsp"> Login ID: <input name="login" type="text" /> <br /> Password: <input name="passwd" type="password" /> <br /> <input name="submit" type="submit" value="Submit" /></form></body><% } else {%>

Page 29: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

29

<body><% Connection conn = null; Statement stmt = null; ResultSet r = null; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql://localhost:40123/hw9?user=www&password=lakoglu415"); 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("<a href=login.jsp>Log In</a>"); out.println("<a href=register.jsp>Register</a>");

} } 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(); }%></body><% }%></html>

Page 30: Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.

Lets put things together…

30

user.jsp

<html><%@ page import="java.sql.*"%>

<body><br><b>Welcome <%= session.getAttribute( "login" )%></b><br /></body></html>