Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Leman Akoglu 11/11/2009 15-415 Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science."— Presentation transcript:

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

2 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-IPhase-II 2

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 3

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 4

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

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 6

7 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

8 Homework 9: CMUBook architecture Tomcat 5.5 CMUBook JSP, Java Web Server newcastle.db.cs.cmu.edu PostgreSQL Database Server newcastle.db.cs.cmu.edu hw9 database Client Browser User http JDBC 8

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 9

10 CMUBook architecture Registration example –register.jsp Tomcat 5.5 CMUBook JSP, Java Web Server newcastle.db.cs.cmu.edu PostgreSQL Database Server newcastle.db.cs.cm u.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. query java.sqlStatement. executeUpdate() JDBC insert succeeds Html page with successful info 1 2 3 4 5 6 10

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 11

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

13 JSP Basics – expressions JSP simply puts Java inside HTML pages. JSP is being turned into a Java file, compiled and loaded enclose Java expressions, which are evaluated at run time Scriptlets: blocks of Java code ( ) Hello! The time is now 13

14 JSP Basics – directives JSP "directives" starts with <%@ characters. "page directive": “include directive”: 14

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. sequences enclose your declarations 15 <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now

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

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 17

18 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

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 19

20 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

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

22 Cookie example String username=request.getParameter("username"); Cookie cookie = new Cookie ("username",username); cookie.setMaxAge(365 * 24 * 60 * 60); response.addCookie(cookie); Welcome:. <% 22

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 23

24 Exception Handling 24 try { …. } catch (Exception ex) { ex.printStackTrace(); out.println("Login failed!"); out.println(" Go back to login! "); }

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 25

26 Lets put things together… 26 register.jsp <% 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) { %> CMUBook Registration Login Name: Full Name: Password: Email:

27 27 <% } else { %> <% 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(" Log In "); } 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(); } %> <% } %>

28 Lets put things together… 28 login.jsp <% String login = request.getParameter("login"); String passwd = request.getParameter("passwd"); String submit = request.getParameter("submit"); if (submit==null) { %> CMUBook Login Page Login ID: Password: <% } else { %>

29 29 <% 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(" Log In "); out.println(" Register "); } } 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(); } %> <% } %>

30 Lets put things together… 30 user.jsp Welcome


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

Similar presentations


Ads by Google