 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
An introduction to Java Servlet Programming
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
1 Pertemuan 11 Aplikasi Web dengan Servlet dan JSP Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
1 Databases & Web-based Applications JDBC & Java Servlets A. Benabdelkader ©UvA, 2002/2003.
 2005 Pearson Education, Inc. All rights reserved Servlets.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 30 - Servlets: Bonus for Java Developers Outline 30.1 Introduction 30.2 Servlet Overview and.
CSC 2720 Building Web Applications
Java Servlets and Java Server Pages Carol Wolf Computer Science.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
MDCFUG Is Java in Your Future? Tyler Williams Principal dataTerrace
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
Java Servlets & Java Server Pages Lecture July 2013.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
Java Stream Introduction 11.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class
ClaRA web services V. Gyurjyan Clas12 Software Meeting
April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,
J2EE/Java EE Apache Tomcat v9 IBM Websphere v8 Redhat JBoss EAP v6 Oracle Weblogic v12 Fusion Web Tier ServletsJSPJSF Business Tier JMXJMSEJB.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Servlets Outline 1 Introduction 2 Servlet Overview and Architecture 2.1 Interface Servlet and the Servlet Life Cycle 2.2 HttpServlet Class 2.3 HttpServletRequest.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Programming with Java Lecture 6 Elements of a Java Servlet
Introduction to Servlets
Servlets Outline Introduction Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class HttpServletRequest.
Servlet Fudamentals.
JDBC & Servlet CSE 4504/6504 Lab.
Servlets Hits Counter 20-Jul-18.
Chapter 24: Servlets Outline Introduction Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle
COP 4610L: Applications in the Enterprise Spring 2005
Chapter 30 - Servlets: Bonus for Java Developers
COP 4610L: Applications in the Enterprise Spring 2005
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Basic servlet structure
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business logic –Database access Web servers often represent the middle tier Three-tier distributed application example –SurveyServlet –Survey.html –Cloudscape database

 2002 Prentice Hall. All rights reserved. Outline Fig Multi- tier Web-based survey using XHTML, servlets and JDBC. Lines Line 21 Lines Lines // Fig. 9.27: SurveyServlet.java 2 // A Web-based survey that uses JDBC from a servlet. 3 package com.deitel.advjhtp1.servlets; 4 5 import java.io.*; 6 import java.text.*; 7 import java.sql.*; 8 import javax.servlet.*; 9 import javax.servlet.http.*; public class SurveyServlet extends HttpServlet { 12 private Connection connection; 13 private PreparedStatement updateVotes, totalVotes, results; // set up database connection and prepare SQL statements 16 public void init( ServletConfig config ) 17 throws ServletException 18 { 19 // attempt database connection and create PreparedStatements 20 try { 21 Class.forName( "COM.cloudscape.core.RmiJdbcDriver" ); 22 connection = DriverManager.getConnection( 23 "jdbc:rmi:jdbc:cloudscape:animalsurvey" ); // PreparedStatement to add one to vote total for a 26 // specific animal 27 updateVotes = 28 connection.prepareStatement( 29 "UPDATE surveyresults SET votes = votes + 1 " + 30 "WHERE id = ?" 31 ); 32 Servlets are initialized by overriding method init. Loads the database driver. Attempt to open a connection to the animalsurvey database. Create PreparedStatement updateVotes object.

 2002 Prentice Hall. All rights reserved. Outline Fig Multi- tier Web-based survey using XHTML, servlets and JDBC. Lines // PreparedStatement to sum the votes 34 totalVotes = 35 connection.prepareStatement( 36 "SELECT sum( votes ) FROM surveyresults" 37 ); // PreparedStatement to obtain surveyoption table's data 40 results = 41 connection.prepareStatement( 42 "SELECT surveyoption, votes, id " + 43 "FROM surveyresults ORDER BY id" 44 ); 45 } // for any exception throw an UnavailableException to 48 // indicate that the servlet is not currently available 49 catch ( Exception exception ) { 50 exception.printStackTrace(); 51 throw new UnavailableException(exception.getMessage()); 52 } } // end of init method // process survey response 57 protected void doPost( HttpServletRequest request, 58 HttpServletResponse response ) 59 throws ServletException, IOException 60 { 61 // set up response to client 62 response.setContentType( "text/html" ); 63 PrintWriter out = response.getWriter(); 64 DecimalFormat twoDigits = new DecimalFormat( "0.00" ); 65 Create PreparedStatement totalVotes and results objects.

 2002 Prentice Hall. All rights reserved. Outline Fig Multi- tier Web-based survey using XHTML, servlets and JDBC. Lines Lines Lines Lines // start XHTML document 67 out.println( " " ); out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 70 "XHTML 1.0 Strict//EN\" \" "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" ); out.println( 74 " " ); // head section of document 77 out.println( " " ); // read current survey response 80 int value = 81 Integer.parseInt( request.getParameter( "animal" ) ); // attempt to process a vote and display current results 84 try { // update total for current surevy response 87 updateVotes.setInt( 1, value ); 88 updateVotes.executeUpdate(); // get total of all survey responses 91 ResultSet totalRS = totalVotes.executeQuery(); 92 totalRS.next(); 93 int total = totalRS.getInt( 1 ); // get results 96 ResultSet resultsRS = results.executeQuery(); 97 out.println( " Thank you! " ); 98 out.println( " " ); 99 Obtain the survey response Set the first parameter of PreparedStatement updateVotes to the survey response and update the database. Execute PreparedStatement totalVotes to retrieve the total number of votes received. Execute PreparedStatement results and process the ResultSet to create the survey summary for the client.

 2002 Prentice Hall. All rights reserved. Outline Fig Multi- tier Web-based survey using XHTML, servlets and JDBC. 100 out.println( " " ); 101 out.println( " Thank you for participating." ); 102 out.println( " Results: " ); // process results 105 int votes; while ( resultsRS.next() ) { 108 out.print( resultsRS.getString( 1 ) ); 109 out.print( ": " ); 110 votes = resultsRS.getInt( 2 ); 111 out.print( twoDigits.format( 112 ( double ) votes / total * 100 ) ); 113 out.print( "% responses: " ); 114 out.println( votes ); 115 } resultsRS.close(); out.print( "Total responses: " ); 120 out.print( total ); // end XHTML document 123 out.println( " " ); 124 out.close(); 125 } // if database exception occurs, return error page 128 catch ( SQLException sqlException ) { 129 sqlException.printStackTrace(); 130 out.println( " Error " ); 131 out.println( " " ); 132 out.println( " Database error occurred. " ); 133 out.println( "Try again later. " ); 134 out.close();

 2002 Prentice Hall. All rights reserved. Outline Fig Multi- tier Web-based survey using XHTML, servlets and JDBC. Lines } } // end of doPost method // close SQL statements and database when servlet terminates 140 public void destroy() 141 { 142 // attempt to close statements and database connection 143 try { 144 updateVotes.close(); 145 totalVotes.close(); 146 results.close(); 147 connection.close(); 148 } // handle database exceptions by returning error to client 151 catch( SQLException sqlException ) { 152 sqlException.printStackTrace(); 153 } 154 } // end of destroy method 155 } Method destroy closes each PreparedStatement and database connection.

 2002 Prentice Hall. All rights reserved. Outline Fig Survey.html document that allows users to submit survey responses to SurveyServlet. 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Survey What is your favorite pet? <input type = "radio" name = "animal" 19 value = "1" />Dog 20 <input type = "radio" name = "animal" 21 value = "2" />Cat 22 <input type = "radio" name = "animal" 23 value = "3" />Bird 24 <input type = "radio" name = "animal" 25 value = "4" />Snake 26 <input type = "radio" name = "animal" 27 value = "5" checked = "checked" />None

 2002 Prentice Hall. All rights reserved. Outline Fig Survey.html document that allows users to submit survey responses to SurveyServlet.

 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet (Cont.)