School of Computer Science 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II Leman Akoglu 11/11/2009
Download
Report
Transcript School of Computer Science 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II Leman Akoglu 11/11/2009
School of Computer Science
15-415 Fall 2009
Recitation
Homework 9
Building A Web Application
Phase-II
Leman Akoglu
11/11/2009
Phase-I
Phase-II
description
conc. mod.
impl.+test.
req. anal.
top level I.F.D.
schema.
code.
tests
user’s man.
sys. anal.
task + doc forms.
2
task emul.
pseudo-code
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
3
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
4
Recommended Schema for HW9
users(login, password, name, email)
photos(URL, owner)
tags(URL, tagger, taggee, timestamp)
likes(user1, user2)
5
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
6
Typical Web Application Architecture
Web Server
Client
Apache,
Tomcat,
Windows IIS
Web app
Users
Backend Server
Java Virtual
Machine
(JSP, ASP, PHP)
Web app
backend
component
Database
Server
7
Homework 9:
CMUBook architecture
Web Server
newcastle.db.cs.cmu.edu
Client
PostgreSQL
Browser
Tomcat 5.5
Database Server
http
newcastle.db.cs.cmu.edu
User
CMUBook
JSP, Java
8
JDBC
hw9
database
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
9
CMUBook architecture
Registration example –register.jsp
1 http://newcastle
.db.cs.cmu.edu:
8080/lakoglu415
Client
/register.jsp
Browser
newcastle.db.cs.cmu.edu
PostgreSQL
Tomcat 5.5
User
2
html page with
input FORM
CMUBook
3
Submit FORM
with login, name,
password and
email
10
Web Server
JSP, Java
6
Html page with successful info
register.jsp
Database
Server
4
JDBC exec. query newcastle.db.cs.cm
u.edu
java.sqlStatement.
executeUpdate()
hw9
database
JDBC insert
succeeds
5
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
11
JSP Basics
Three primitives
– expressions
– directives
– declarations
12
JSP Basics – expressions
JSP simply puts Java inside HTML pages.
JSP is being turned into a Java file, compiled and loaded
<HTML> <BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY> </HTML>
<%= and %> enclose Java expressions, which are evaluated
at run time
Scriptlets: blocks of Java code (<% and %>)
13
JSP Basics – directives
JSP "directives" starts with <%@ characters.
"page directive":
<%@ page import="java.util.*,java.text.*" %>
“include directive”:
<%@ include file="hello.jsp" %>
14
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.
<%! 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;
}
%>
15
Hello! The time is now <%= getDate() %>
</BODY> </HTML>
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
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
17
How to connect to the database
<%
…
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));
…
18
%>
register.jsp
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
19
JSP Basics – sessions – method#1
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" )
20
(Optional)
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
Cookie example
String 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
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
23
Exception Handling
try {
….
}
catch (Exception ex) {
ex.printStackTrace();
out.println("Login failed!");
out.println("<a href=login.jsp>Go back to login!</a>");
}
24
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
25
Lets put things together…
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>
26
<%
} 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>");
27
} 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>
Lets put things together…
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>
28
<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 {
%>
<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>
29
Lets put things together…
user.jsp
<html>
<%@ page import="java.sql.*"%>
<body>
<br>
<b>Welcome <%= session.getAttribute( "login" )%></b>
<br />
</body>
</html>
30