JDBC 1 Some introductory database terminology 2 Basic JDBC 3 Servlets 4 JDBC and servlets Gary Alperson helped developed these slides.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
RMI and JDBC Some Database terminology Connecting a remote object to a database From relations to XML Gary Alperson helped developed these slides and the.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Object Oriented Programming Java Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent:
OCT 1 Master of Information System Management Organizational Communications and Distributed Object Technologies Week 2: Sockets and Servlets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Web Technologies1 Java Servlets Part I Server and Servlet Basics Part II Session Tracking and Servlet Collaboration.
Serving HTTP NetworkServer.java EchoServer.java PostForm.html PostForm.html Browser EchoServer Response Using POST GetForm.html GetForm.html Browser EchoServer.
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.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
HTTP HyperText Transfer Protocol Stateless request/response client-server protocol Requests: Method: GET, POST, HEAD, TRACE, OPTIONS, PUT, DELETE.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
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.
CSC 2720 Building Web Applications
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
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:
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java Servlets & Java Server Pages Lecture July 2013.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
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.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Li Tak Sing COMPS311F. A web page that counts the number of times that you have visited the page. You can try the page at:
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
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.
Introduction to Servlets. Introduction Servlet is a language to develop the server side applications, and it is also server side component. It can develop.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Introduction To HTML Dr. Magdi AMER. HTML elements.
Web Programming Assistant Professor Xiaozhong Liu
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
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.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
JDBC & Servlet CSE 4504/6504 Lab.
Servlets Hits Counter 20-Jul-18.
Session Tracking in Servlets
Pre-assessment Questions
Lecture 2: Server-Side Programming: An Introduction to Servlets
Sessions.
Chapter 26 Servlets.
Servlets and Java Server Pages
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Presentation transcript:

JDBC 1 Some introductory database terminology 2 Basic JDBC 3 Servlets 4 JDBC and servlets Gary Alperson helped developed these slides.

Database Terminology Database: A shared collection of logically related data (and a description of this data) designed to meet the information needs of an organization Relation: A table with columns and rows Attribute: A named column of a relation Tuple: A row in a relation Definitions from Database Systems by Connolly, Begg, and Strachan

Sample Table

Attribute

Tuple

SQL Data Definition Language (DDL) –Create tables –Modify tables –Delete (drop) tables Data Manipulation Language (DML) –Insert data –Update data –Select data

Select Statement We will use this data for our examples

From the broker table, select the contents of the last name attribute Query SELECT lname FROM broker; Results SQL is not case sensitive. Key SQL words are capitalized and line breaks are inserted by convention.

From the broker table, select all attributes Query SELECT * FROM broker; Results * Acts as a wildcard

From the broker table, select all attributes where the last name is Smith Query SELECT * FROM broker WHERE lname = ‘Smith’; Results Note that the string is enclosed by single quotes The contents of a string are case sensitive

Use AND or OR to connect multiple where clauses Query SELECT * FROM broker WHERE lname = ‘Smith’ AND fname = ‘John’; Results

Example with two Tables One-to-many relationship Each broker may have many customers Each customer is only affiliated with one broker The b_id joins both tables by identifying the unique broker that each customer is associated with

Cartesian Product When you do a query on multiple tables, SQL begins by creating the Cartesian product, which combines each tuple from one relation from every tuple of the other relation. (Actual SQL implementations are free to compute the resulting table efficiently,i.e., the actual Cartesian product may not be generated at all.)

Query SELECT * FROM customer, broker WHERE broker.b_id = 1; SQL does not realize that the b_id in the customer table is the same as the b_id in the broker table unless you join them in the where clause. Results

Cartesian Product Query SELECT * FROM customer, broker WHERE broker.b_id = 1 AND broker.b_id = customer.b_id; Results

Java’s JDBC Allows access to any ANSI SQL-2 DBMS Does its work in terms of SQL The JDBC has classes that represent: database connections SQL Statements Result sets database metadata Can be connected to ODBC Many drivers exists

Basic Steps in using JDBC 1.Create a driver object. 2. The driver object will inform the Driver Manager that it is available 3. Create a database URL. This needs to point to the database to which you want to connect. 4.Ask the DriverManager for a Connection object. The manager must be told what driver you need and the URL in Get a Statement object from the Connection. 6. Execute a query or an update on the Statement. 7. Handle results. 8. Close the connection.

Create the driver Example: Class.forName(“oracle.jdbc.driver.OracleDriver”).newInstance(); When a new instance of the driver is created it will inform the DriverManager class of its existence.

Create the database URL The exact format depends on the particular driver. Examples: String host = “dbhost.yourcompany.com”; String dbName = “somename”; int port = 1234; String oracleURL = + host + “:” + port + “:” + dbName;

Build a Connection object java.sql.Connection is an interface Within the context of a Connection, SQL statements are executed and results are returned. A Connection object is able to provide information describing the database as a whole through its getMetaData method(). The default Connection automatically commits changes after executing each statement. If auto commit has been disabled, an explicit commit must be done or database changes will not be saved.

Build a Connection object Connection c = DriverManager.getConnection( oracleURL, “mm6”, “seseme”);

Get a Statement from the Connection The Statement interface has two subinterfaces PreparedStatement extends Statement This is an object that represents a precompiled SQL statement. CallableStatement extends PreparedStatement The interface used to execute SQL stored procedures.

Get a Statement from the Connection c.setAutoCommit(false); Statement s = con.createStatement(); s.executeUpdate(command1); s.executeUpdate(command2); e.executeUpdate(command3); // Now we are free to c.commit(); // or.. c.rollback();

The Statement Object may produce a ResultSet ResultSet rs = s.executeQuery(“SELECT * FROM Broker”); while (rs.next()) { // examine each row of the result set String n = rs.getString(columnNumber); double f = rs.getDouble(“SomeColumnName”); } // each get method tries to make a reasonable type conversion // get may be used with integer column numbers starting // at 1 or a column name

The Statement Object may return an int int rowsChanged = s.executeUpdate(“CREATE TABLE” + … ); Executes an SQL INSERT, UPDATE or DELETE statement. In addition, SQL statements that return nothing, such as SQL DDL statements, can be executed. Returns: either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing Throws: SQLExceptionSQLException - if a database access error occurs

An Example The SQL SELECT customer.lname FROM customer, broker WHERE broker.lname = ‘Smith’ AND broker.b_id <> 1 AND broker.b_id = customer.b_id; From both tables select the last names of all customers whose broker’s last name is Smith but whose broker ID is not 1.

Executing a query in Java // Statement aStatement = statement got from connection String last = “Smith”; int nonID = 1; String q = “SELECT customer.lname FROM customer, broker” + “WHERE broker.lname = \’” + last + “\’ AND broker.b_id” + “<>” + nonID + “AND broker.b_id = customer.b_id;”); ResultSet rs = aStatement.executeQuery(q); The slash (\) is the escape character. It precedes the single quote to tell Java to include that quote in the String The String last is outside of the double quotes, because it must be concatonated with the String sent to the database, but it falls within the single quotes so that SQL treats it as a string nonID does not go within single quotes since it is numeric Since the String is an SQL statement, it uses = and <> rather than == and !=

Java Servlets Part I Server and servlet basics Part II Session Tracking and Servlet Collaboration Part III Connecting to database

Part I : Server and Servlet Basics NetworkServer.java and EchoServer.java PostForm.html GetForm.html More HTML form examples

NetworkServer.java // NetworkServer.java Adapted from "Core Servlets // and Java Server Pages" // by Marty Hall import java.net.*; import java.io.*; public class NetworkServer { private int port; private int maxConnections; No Tomcat server. Just this code.

protected void setPort(int port) { this.port = port; } public int getPort() { return port; } protected void setMaxConnections(int max) { maxConnections = max; } public int getMaxConnections() { return maxConnections; } public NetworkServer(int port, int maxConnections) { setPort(port); setMaxConnections(maxConnections); }

// Wait for a connections until maxConnections. // On each connection call handleConnection() passing // the socket. If maxConnections == 0 loop forever public void listen() { int i = 0; try { ServerSocket listener = new ServerSocket(port); Socket server ; while((i++ < maxConnections) || (maxConnections == 0)) { server = listener.accept(); // wait for connection handleConnection(server); } } catch (IOException ioe) { System.out.println("IOException : " + ioe); ioe.printStackTrace(); }

// Open readers and writers to socket. // Display client's host name to console. // Read a line from the client and display it on the console. // Send "Generic network server" to the client. // Override this method. protected void handleConnection(Socket server) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader( server.getInputStream() )); PrintWriter out = new PrintWriter( server.getOutputStream(),true); Flush buffer on println InputStream for reading bytes Readers and Writers to work with characters

System.out.println("Generic network server: got connection from "+ server.getInetAddress().getHostName() + "\n" + "with first line '" + in.readLine() + "'"); out.println("Generic network server"); server.close(); } public static void main(String args[]) { NetworkServer test = new NetworkServer(6502, 5); test.listen(); } To server’s console. To client.

Compile, Run and Visit C:\McCarthy\www\46-928\examples\networking>java NetworkServer Generic network server: got connection from localhost with first line 'GET / HTTP/1.0' Client Server

EchoServer.java /* From Core Servlets, Marty Hall An HTTP Request header example Notes GET /path/file.html HTTP/1.0 The whitespace is required. Accept: text/html Accept header fields Accept: audio/x tell the server MIME types User-agent: MacWeb (Multipurpose Internet Mail Extension) that are handled by the browser. Still no Tomcat HTTP defines dozens of possible headers. Request terminated by two returns

EchoServer.java An HTTP Response header example HTTP OK Server: NCSA/1.4.2 MIME-version: 1.0 Content-type: text/html Content-length: 107 : Blank line MIME type The client must interpret this MIME encoded data. Response code

HTTP General form [ : ] : : : [ : ] a blank line [entity body] The resource identifier field specifies the name of the target resource; it's the URL stripped of the protocol and the server domain name. When using the GET method, this field will also contain a series of name=value pairs separated by ‘&’. When using a POST method, the entity body contains these pairs. The HTTP version identifies the protocol used by the client. */

// Adapted from Core Servlets and JavaServerPages // by Marty Hall, chapter 16 import java.net.*; import java.io.*; import java.util.StringTokenizer; public class EchoServer extends NetworkServer { protected int maxRequestLines = 50; // Post data is brought in // as a single string. protected String serverName = "EchoServer"; public static void main(String a[]) { int port = 6502; new EchoServer(port,0); // loop forever }

public EchoServer(int port, int maxConnections) { super(port,maxConnections); // call base class constructor listen(); // call base class listen() } // listen calls handleConnection() // Overrides base class handleConection and is called by listen() public void handleConnection(Socket server) throws IOException { // Assign readers and writers to the socket BufferedReader in = new BufferedReader( new InputStreamReader( server.getInputStream() )); PrintWriter out = new PrintWriter(server.getOutputStream(),true); // Announce connection to console System.out.println(serverName + " got connection from "+ server.getInetAddress().getHostName() + "\n");

String inputLines[] = new String[maxRequestLines]; int i; for(i = 0; i < maxRequestLines; i++) { inputLines[i] = in.readLine(); if(inputLines[i] == null) break; // client closed connection if(inputLines[i].length() == 0) { // blank line // maybe done or maybe post if(usingPost(inputLines)) { // readPostData reads into a single string // at location i+1 readPostData(inputLines,i,in); // i was not changed in the procedure so // bump it one past the post data string i = i + 2; } break; // we’re done either way }

printHeader(out); for(int j = 0; j < i; j++) { out.println(inputLines[j]); } printTrailer(out); server.close(); } //Request Data // HTTP + HTML // Closing HTML

private void printHeader(PrintWriter out) { out.println( "HTTP/ OK\r\n" + "Server: " + serverName + "\r\n" + "Content-Type: text/html\r\n" + “\r\n” + "<!DOCTYPE HTML PUBLIC " + "\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" + " \n" + " " + serverName + " Results \n" + " \n" + "\n" + " \n" + " " + serverName + " Results \n" + "Here is your request line and request headers\n" + "sent by your browser:\n" + " “ ); // honors whitespace } HTTP Response headers plus HTML.

private void printTrailer(PrintWriter out) { out.println(" \n" + " \n" + " \n"); } private boolean usingPost(String[] inputs) { return (inputs[0].toUpperCase().startsWith("POST")); } // Close HTML // Checks if post

// Read the post data as a single array of char and place it all // in one string. private void readPostData (String inputs[], int i, BufferedReader in) throws IOException { int contentLength = contentLength(inputs); char postData[] = new char[contentLength]; in.read(postData, 0, contentLength); // All of the post data is converted to a single string inputs[++i] = new String(postData,0,contentLength); }

// The header fields may arrive in any order. // Search for and return the CONTENT-LENGTH. private int contentLength(String inputs[]) { String input; for(int i = 0; i < inputs.length; i++) { if(inputs[i].length() == 0) break; input = inputs[i].toUpperCase(); if(input.startsWith("CONTENT-LENGTH")) return (getLength(input)); } return (0); } // Return the integer associated with the second token. private int getLength(String length) { StringTokenizer tok = new StringTokenizer(length); tok.nextToken(); return (Integer.parseInt(tok.nextToken())); }

PostForm.html Post Form Hi, what is your name? What is your age? Visit the port

PostForm.html Browser

EchoServer Response Using POST POST data Size of Name value pairs with spaces as ‘+’ etc.

GetForm.html Get Form Hi, what is your name? What is your age?

GetForm.html Browser

EchoServer Response Using GET GET data

A Form With Checkboxes CheckBoxes Select Pizza Toppings Pepperoni Sausage Extra Cheese Mushrooms

CheckBoxes Browser

CheckBox Response Data from client

RadioBoxes HTML Radio Buttons Please Vote George W. Bush Al Gore Pat Buchanan Ralph Nader

RadioBoxes Browser

EchoServer Response

Reading Form Data With Servlets Under Tomcat // QueryData.java -- Handle the voting form in radio.html import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class QueryData extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { doGet(req, response); } We have less work to do.

public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { String newPresident = req.getParameter("president"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"//W3C//DTD” + “HTML 4.0 "; docType += "Transitional//EN\">\n";

out.println(docType + " \n" + " Presidential Servlet" + " ” + “ \n" + " \n" + " The new president is "+ newPresident + " \n" + " "); }

Radio Buttons Please Vote George W. Bush Al Gore Pat Buchanan Ralph Nader servlet Tomcat’s port

Radio HTML in the browser

The Servlet’s Response

Handling CheckBoxes CheckBoxes Select Pizza Toppings Pepperoni Sausage Extra Cheese Mushrooms servlet

Pizza Toppings

Servlet Response

PizzaData Servlet // PizzaData.java -- Handle the toppings selection from pizza.html import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class PizzaData extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { doGet(req, response); }

public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String finalString = ""; Enumeration paramNames = req.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); finalString += paramName + ":" ; finalString += req.getParameter(paramName) + " "; } Enumerate over the input.

String docType = "<!DOCTYPE HTML PUBLIC \"//W3C//DTD” + “ HTML 4.0 "; docType += "Transitional//EN\">\n"; out.println(docType + " \n" + " Pizza Selections" + " ” + “ \n" + " \n" + " " + finalString + " \n" + " "); }

Part II Session Tracking and Servlet Collaboration First we will use a shared object Then we’ll use the new Session Tracking API

Session Tracking with Servlets HTTP is a stateless protocol. We must have each user introduce themselves in some way. We’ll look at traditional session tracking and then look at the Session Tracking API.

Traditional Session Tracking User Authorization Hidden Form fields URL Rewriting Persistent cookies We’ll look at the first and last.

User Authorization The web server requests the user name and password. The information is available to any servlet that needs it. The browser resends the name and password with each subsequent request. Data about the user and the user’s state can be saved in a shared object.

Shared Objects A convenient way to store data associated with a user. There are likely to be many servlets running. They can collaborate through a shared object. Only one instance of the shared object should exist. It has to be available (in the classpath) of the servlets that needs it. It will be used by several threads and therefore should protect itself against simultaneous access. We’ll look at a shared object and two servlets that use it.

VisitTracker.java // Servlet collaboration can be done through a shared object. // Any servlet has access to this object and it only has one // instance. // It maintains a hash table of names and dates. // Sections of code that must not be executed simultaneously // are called critical sections. Java provides the synchronized // keyword to protect these critical sections. For a synchronized // instance method, Java obtains an exclusive lock on the class // instance. import java.util.*;

public class VisitTracker { private Map nameDatePairs; private static VisitTracker instance = new VisitTracker(); private VisitTracker() { // private constructor nameDatePairs = new HashMap(); } public static VisitTracker getInstance() { return instance; } synchronized public void addVisit(String userName) { nameDatePairs.put(userName, new Date()); }

synchronized public Date lastVisit(String name) { Date d = (Date)nameDatePairs.get(name); return d; }

User Authorization Administered by the web server – Tomcat Edit Tomcat’s deployment descriptor From within the servlet use String name = req.getRemoteUser(); to access the user name. We have to assign user names and passwords. tomcat-users.xml The following will keep track of the date of the last visit.

// UserAuthorizationDemo.java // This servlet reads from Tomcat and finds the name of the // authorized user. It then adds it to a hash table storing // the time of this visit. It makes use of VisitTracker. import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class UserAuthorizationDemo extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/plain"); PrintWriter out = res.getWriter(); String name = req.getRemoteUser(); // ask the server if(name == null) { System.out.println("The system administrator should protect" + " this page."); } else { out.println("This user was authorized by the server:" + name); VisitTracker visit = VisitTracker.getInstance(); Date last = visit.lastVisit(name); if(last == null) out.println("Welcome, you were never here before"); else out.println("Your last visit was on " + last); visit.addVisit(name); }

Cookies A cookie is a bit of information sent by a web server to a browser that can later be read back from that browser. The server can take that bit of information and use it as a key to recover information about prior visits. This information may be in a database or a shared object. Cookies are read from the request object by calling getCookies() on the request object. Cookies are placed in the browser by calling addCookie() on the response object.

Using Cookies // CookieDemo.java // This servlet uses a cookie to determine when the // last visit by this browser occurred. It makes use of // the VisitTracker object. // Cookies normally expire as soon as the browser exits. // We want the cookie to last one year and so we use // setMaxAge(seconds) on the cookie. import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class CookieDemo extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); Cookie[] c = req.getCookies(); // If this person has been here before then we should have // a cookiedemouser field assigned to a unique id. String id = null;

if (c!=null) { // we may have the cookie we are after for (int i=0;i<c.length;i++) { if (c[i].getName().equals("cookiedemouser")) { id = c[i].getValue(); } break; }

if (id == null) { // They have not been here before and need a // cookie. We get a unique string (with respect // to this host)and make sure it is of the 'query string' form. // It uses the clock. Don’t turn the clock back! String uid = new java.rmi.server.UID().toString(); id = java.net.URLEncoder.encode(uid); Cookie oreo = new Cookie("cookiedemouser",id); oreo.setMaxAge(60*60*24*365); res.addCookie(oreo); } VisitTracker visit = VisitTracker.getInstance(); Date last = visit.lastVisit(id); if(last == null) out.println("Welcome, you were never here before"); else out.println("Your last visit was on " + last); visit.addVisit(id); }

The New Session Tracking API Support may vary depending on the server. Implemented with cookies or with URL rewriting if cookies fail (URL rewriting requires help from the servlet). Every user of the site is associated with a javax.servlet.http.HttpSession object The session object can hold any arbitrary set of Java objects. Servlets collaborate by accessing the session object. The following example abstracts away shared object concerns. All valid sessions are grouped together in a HttpSessionContext object

The Session Tracking API // SessionDemo.java // The session object associated with this user/browser is available // to other servlets. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class SessionDemo extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); // Get the current session object. Create one if none exists. HttpSession session = req.getSession(true); // Get the Date associated with this session Date d = (Date)session.getAttribute("dateofvisit"); if(d == null) out.println("Your first time, welcome!"); else out.println("Your last visit was on " + d); session.setAttribute("dateofvisit", new Date()); } }

Part III Connecting to the database Per-Transaction Connections Dedicated Connections Session Connections Notes taken from “Java Programming with Oracle JDBC” By Donald Bales

Per-Transaction Connection Servlet init() { load the driver and inform the DriverManager } doxxx() { get a connection : operate on statements, result sets… : close connection } Each visit gets its own connection.

Per-Transacation Connection // Form Oracle JDBC by Bales import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class TransactionConnectionServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try { // load the driver Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); }

catch (ClassNotFoundException e) { throw new UnavailableException( "TransactionConnection.init() ClassNotFoundException: " + e.getMessage()); } catch (IllegalAccessException e) { throw new UnavailableException( "TransactionConnection.init() IllegalAccessException: " + e.getMessage()); } catch (InstantiationException e) { throw new UnavailableException( "TransactionConnection.init() InstantiationException: " + e.getMessage()); }

public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(" A Per Transaction Connection "); out.println(" "); Connection connection = null; try { // establish a connection connection = DriverManager.getConnection( "scott", "tiger"); } catch (SQLException e) { throw new UnavailableException( "TransactionConnection.init() SQLException: " + e.getMessage()); }

Statement statement = null; ResultSet resultSet = null; String userName = null; try { // test the connection statement = connection.createStatement(); resultSet = statement.executeQuery( "select initcap(user) from sys.dual"); if (resultSet.next()) userName = resultSet.getString(1); } catch (SQLException e) { out.println( "TransactionConnection.doGet() SQLException: " + e.getMessage() + " "); }

finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) { } if (statement != null) try { statement.close(); } catch (SQLException ignore) { } } if (connection != null) { // close the connection try { connection.close(); } catch (SQLException ignore) { } } out.println("Hello " + userName + "! "); out.println("You're using a per transaction connection! "); out.println(" "); } public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); }

A Dedicated Connection Servlet init() { load driver and inform manager open connection } doxxx() { use connection commit transaction } destroy { when servlet container is brought down close the connection }

A Dedicated Connection One connection per servlet Open during life of servlet All users of this servlet use the same connection Oracle’s connection class’s methods are thread safe Under this approach, servlets do not share connections

A Dedicated Connection // From the book Oracle JDBC by Bales import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class DedicatedConnectionServlet extends HttpServlet { Connection connection; long connected;

public void init(ServletConfig config) throws ServletException { super.init(config); try { // load the driver Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); } catch (ClassNotFoundException e) { throw new UnavailableException( "DedicatedConnection.init() ClassNotFoundException: " + e.getMessage()); } catch (IllegalAccessException e) { throw new UnavailableException( "DedicatedConnection.init() IllegalAccessException: " + e.getMessage()); }

catch (InstantiationException e) { throw new UnavailableException( "DedicatedConnection.init() InstantiationException: " + e.getMessage()); } try { // establish a connection connection = DriverManager.getConnection( "scott", "tiger"); connected = System.currentTimeMillis(); } catch (SQLException e) { throw new UnavailableException( "DedicatedConnection.init() SQLException: " + e.getMessage()); }

public void doGet( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(" A Dedicated Connection "); out.println(" "); Statement statement = null; ResultSet resultSet = null; String userName = null;

try { // test the connection statement = connection.createStatement(); resultSet = statement.executeQuery( "select initcap(user) from sys.dual"); if (resultSet.next()) userName = resultSet.getString(1); } catch (SQLException e) { out.println( "DedicatedConnection.doGet() SQLException: " + e.getMessage() + " "); } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) { } if (statement != null) try { statement.close(); } catch (SQLException ignore) { } }

out.println("Hello " + userName + "! "); out.println( "This Servlet's database connection was created on " + new java.util.Date(connected) + " "); out.println(" "); } public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } public void destroy() { // close the connection if (connection != null) try { connection.close(); } catch (SQLException ignore) { } }

A Session Connection Servlet init() { load the driver and inform the driver manager } doxxx() { establish an HTTP Session object that holds the database connection If the session expires we must notify the connection to close }

A Session Connection We need an HTTPSessionBindingListener to close the connection when the session expires. HTTPSession SessionConnection Implements HTTPSessionBindingListener When this session expires the system will call the valueUnBound() method in the HTTPSessionBindingListener.

SessionConnection // From Oracle JDBC by Bales import java.sql.*; import javax.servlet.http.*; public class SessionConnection implements HttpSessionBindingListener { Connection connection; public SessionConnection() { connection = null; }

public SessionConnection(Connection connection) { this.connection = connection; } public Connection getConnection() { return connection; } public void setConnection(Connection connection) { this.connection = connection; }

public void valueBound(HttpSessionBindingEvent event) { if (connection != null) { System.out.println("Binding a valid connection"); } else { System.out.println("Binding a null connection"); } public void valueUnbound(HttpSessionBindingEvent event) { if (connection != null) { System.out.println( "Closing the bound connection as the session expires"); try { connection.close(); } catch (SQLException ignore) { } }

SessionLogin import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionLogin extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try { // load the driver Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); }

catch (ClassNotFoundException e) { throw new UnavailableException( "Login init() ClassNotFoundException: " + e.getMessage()); } catch (IllegalAccessException e) { throw new UnavailableException( "Login init() IllegalAccessException: " + e.getMessage()); } catch (InstantiationException e) { throw new UnavailableException( "Login init() InstantiationException: " + e.getMessage()); }

public void doGet( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(" Login "); out.println(" "); HttpSession session = request.getSession(); SessionConnection sessionConnection = (SessionConnection)session.getAttribute("sessionconnection"); Connection connection = null;

if (sessionConnection != null) { connection = sessionConnection.getConnection(); } if (connection == null) { String userName = request.getParameter("username"); String password = request.getParameter("password"); if (userName == null || password == null) { // prompt the user for her username and password out.println(" "); out.println("Please specify the following to log in: "); out.println("Username: <input type=\"text\" " + "name=\"username\" size=\"30\"> "); out.println("Password: <input type=\"password\" " + "name=\"password\" size=\"30\"> "); out.println(" "); }

else { // create the connection try { connection = DriverManager.getConnection( userName, password); } catch (SQLException e) { out.println("Login doGet() " + e.getMessage()); } if (connection != null) { // store the connection sessionConnection = new SessionConnection(); sessionConnection.setConnection(connection); session.setAttribute("sessionconnection", sessionConnection); response.sendRedirect("SessionLogin"); return; }

else { String logout = request.getParameter("logout"); if (logout == null) { // test the connection Statement statement = null; ResultSet resultSet = null; String userName = null; try { statement = connection.createStatement(); resultSet = statement.executeQuery( "select initcap(user) from sys.dual"); if (resultSet.next()) userName = resultSet.getString(1); } catch (SQLException e) { out.println("Login doGet() SQLException: " + e.getMessage() + " "); }

finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) { } if (statement != null) try { statement.close(); } catch (SQLException ignore) { } } out.println("Hello " + userName + "! "); out.println("Your session ID is " + session.getId() + " "); out.println("It was created on " + new java.util.Date(session.getCreationTime()) + " "); out.println("It was last accessed on " + new java.util.Date(session.getLastAccessedTime()) + " "); out.println(" "); out.println("<input type=\"submit\" name=\"logout\" " + "value=\"Logout\">"); out.println(" "); }

else { // close the connection and remove it from the session try { connection.close(); } catch (SQLException ignore) { } session.removeAttribute("sessionconnection"); out.println("You have been logged out."); } out.println(" "); } public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); }