bin fu [email protected] 03/27/2012 15-415 spring 2012 homework 7 building a web application...
TRANSCRIPT
![Page 1: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/1.jpg)
Bin Fu [email protected]
03/27/2012
15-415 Spring 2012Homework 7
Building A Web Application
School of Computer Science
![Page 2: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/2.jpg)
HW7 OutlineBuilding a simple Web application (“Twitter”)
using JSP2 phasesPhase I : design and documentation
due 4/3hard copy in class
Phase II : implementationdue 4/12 both hard copy in class and via email.
Start early!
![Page 3: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/3.jpg)
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
Database Design Methodology [N. Roussopoulos and R.T. Yeh]
![Page 4: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/4.jpg)
Office Hours for HW73/28 (Wed) 12pm – 3pm3/30 (Fri) 1pm – 2pm4/2 (Mon) 12pm – 3pm 4/3 4/4 (Wed) 12pm – 3pm 4/5 (Thu) 3pm – 6pm 4/6 (Fri) 1pm – 2pm4/9 (Mon) 12pm – 3pm 4/10 (Tue) 3pm – 6pm4/12
Phase-1
Phase-2
Send additional questions to [email protected]
Phase-1 due
Phase-2 due
![Page 5: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/5.jpg)
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
![Page 6: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/6.jpg)
Homework 7A micro-blog web application like TwitterUsers could add tweet, follow other’s tweet,
etc.
Tasks to implement1. Registration (a simple example is already
provided)2. Login/Logout3. Profile Page4. Add Tweet5. Browse Tweet6. “Follow” Management7. Hashtag Search8. Reporting (website statistics)
![Page 7: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/7.jpg)
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
![Page 8: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/8.jpg)
registration form
T1-reg.
user record
Top level information flow diagram (Homework I.1)
System boundary
tasks
internal document (database tables)
external document(web forms)
![Page 9: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/9.jpg)
registration form
More on top level diagram
login form
T2 - login
?
T1-reg.
user record ?
profile page
![Page 10: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/10.jpg)
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
![Page 11: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/11.jpg)
Document + Task formsTask forms and task list
not required for this homework
Document forms and document list
• D1: registration form• D2: login form• D3: profile form• …• Dx: user record• …
internal
external
![Page 12: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/12.jpg)
Document forms (Homework I.2-3)
D1: registration formlogin namepasswordname
D3: profile form• login name?• …List-of:
tweetowner (login name) ?…
Dx: user recordlogin namepasswordname
![Page 13: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/13.jpg)
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
![Page 14: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/14.jpg)
E-R diagram (Homework I.4-6)
user tweet
login emailURL
...
passw
How about follows, tags …?
o Specify cardinalitieso Think about weak/strong entities
own??
![Page 15: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/15.jpg)
Relational schema (Homework I.7-8)
user( login, passw, email … ) tweet( owner, content … ) ?….
SQL DDL statements (Homework I.9)create table users (login char(20), passwd
char (20) NOT NULL (?), … );create table tweet (content char(140),
… );
...
![Page 16: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/16.jpg)
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
![Page 17: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/17.jpg)
Task emulation/pseudo-code (Homework I.10)
Task1: Registration read login, password and emailif ( login does not exist in ‘user’){ insert into user values (login_id, password, email);} else{
print “error: login exists, choose another”}
should be valid SQL queries
![Page 18: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/18.jpg)
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
![Page 19: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/19.jpg)
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
![Page 20: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/20.jpg)
Users
Web app
(JSP, ASP, PHP)
Apache, Tomcat,
Windows IIS
Web Server
Java Virtual Machine
Web app backend
component
Backend Server
Database
Server
Client
Web Application ArchitectureMulti-tier architecture
http e.g. RMI
JDBC
ODBC
![Page 21: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/21.jpg)
Homework 7: Architecture
Tomcat 6.0
Twitter app
JSP, Java
Web Servernewcastle.db.cs.cmu.edu
PostgreSQL
Database Servernewcastle.db.cs.cmu.e
du
hw7 database
Client
Browser
User
http
JDBC
![Page 22: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/22.jpg)
Registration example – register.jsp
Tomcat 6.0
Twitter app
JSP, Java
Web Servernewcastle.db.cs.cmu.edu
PostgreSQL
Database Server
newcastle.db.cs.cmu.edu
hw7 database
Client
Browser
User
http://newcastle.db.cs.cmu.edu:8080/testuser415/register.jsp
register.jsp
html page with input FORM
Submit FORM with login_id, password and email
JDBC exec. queryjava.sqlStatement.executeUpdate()
JDBC insert succeeds
Html page with successful info
1
2
3
4
5
6
![Page 23: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/23.jpg)
Registration example – register.jsp
![Page 24: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/24.jpg)
Registration example – register.jsp(continued)
![Page 25: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/25.jpg)
JSP BasicsJSP is a technology that helps software
developers serve dynamically generated web pages
Similar to PHP and ASP (Microsoft), but using Java: It simply “put Java inside HTML”.
![Page 26: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/26.jpg)
JSP BasicsThree primitives
– expressions– directives– declarations
![Page 27: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/27.jpg)
JSP Basics – expressionsJSP 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>
![Page 28: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/28.jpg)
JSP Basics – directivesJSP "directives" starts with <%@
characters.
"page directive": can import external java (can be user-defined) classes.
<%@ page import="java.util.*, MyPackage.MyClass" %>
“include directive”: can include other jsp/html files.
<%@ include file="hello.jsp" %>
![Page 29: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/29.jpg)
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
<%@ 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 30: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/30.jpg)
JSP Basics - communication w/ server
A "request" in server-side processing refers to the transaction between a browser and the server. request.getParameter(“login”); // Common use: get the
query string values
A "response" is used to affect the response being sent to the browser. response.addCookie(cookie); // See later slide for
Cookies response.sendRedirect(anotherUrl); // Tell browser to
jump to another
URL
![Page 31: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/31.jpg)
Connect JSP to database<% …Connection conn = null;Statement stmt = null;ResultSet r = null;
Class.forName("org.postgresql.Driver");conn = DriverManager.getConnection ("jdbc:postgresql://localhost:40032/hw7?user=www&password=binf415"); stmt = conn.createStatement(); r = stmt.executeQuery(your-SQL-query); if (r.next()) { session.setAttribute("login", r.getString(1));
…%>
register.jsp
![Page 32: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/32.jpg)
JSP Basics – Session managementOn a typical web site, a visitor might visit several pages and perform several interactions. If you are programming the site, it is very helpful to be able to associate some data with each visitor.
For example, after a user logs in, you might want to keep the login name and/or other information of the user to maintain his/her credential.
Two ways to implement in JSP: session, and cookies (optional).
![Page 33: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/33.jpg)
JSP Basics – sessions (method#1)A session is an object associated with a
visitor.If you open different browsers, or use
different IPs, you are essentially use multiple sessions.
Data can be put in the session and retrieved from it, much like a Hashtable.session.setAttribute( "theName", name ); session.getAttribute( "theName" )
Default expiration time: 30 minutes. You don’t need to change it in the homework.
![Page 34: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/34.jpg)
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.
(Optional)
![Page 35: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/35.jpg)
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()%>.<%
![Page 36: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/36.jpg)
JSP Basics – Exception Handling
try {
….
} catch (Exception ex) { ex.printStackTrace();
out.println("Login failed!"); out.println("<a href=login.jsp>Go back to login!</a>");
}
// out: Output stream for page context, i.e., the content to show in the HTML.
![Page 37: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/37.jpg)
Better Software Design? Design Pattern
Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides
Thinking in Patterns (http://www.mindviewinc.com/downloads/TIPatterns-0.9.zip)
Design Pattern for Web App:MVC pattern (Model – View – Controller)Basic idea: break software functionalities and
interfacesTool: struts
(Optional)
![Page 38: Bin Fu binf@andrew.cmu.edu 03/27/2012 15-415 Spring 2012 Homework 7 Building A Web Application School of Computer Science](https://reader036.vdocuments.us/reader036/viewer/2022062803/56649ccb5503460f9499430b/html5/thumbnails/38.jpg)
MVC
(Optional)