Servlets and JDBC. Servlets A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:

Slides:



Advertisements
Similar presentations
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
Advertisements

J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
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.
Servlets Stoney Jackson
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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.
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
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.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets and JSP.
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.
SERVLETS.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Introduction Servlets and JSP Celsina Bignoli
CSE 305 Theory of Database Tutorial on Connecting with Sybase from Java program and Developing GUI Jalal Mahmud, TA, CSE 305.
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.
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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
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:
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
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.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example. Slide title 40 pt Slide subtitle 24 pt Text 24 pt Bullets.
Hints for Assignment #8. Initial Screen Hints for the initial page You need a DOCTYPE with the proper namespaces defined You need to import the facebook.
 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.
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.
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.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and Java Server Pages
Introduction To HTML Dr. Magdi AMER. HTML elements.
ClaRA web services V. Gyurjyan Clas12 Software Meeting
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
J2EE/Java EE Apache Tomcat v9 IBM Websphere v8 Redhat JBoss EAP v6 Oracle Weblogic v12 Fusion Web Tier ServletsJSPJSF Business Tier JMXJMSEJB.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
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.
Using Oracle JDBC How to Run JDBC on Your Account Communication Mechanism Using Metadata Building a Database Auto Commit v.s Atomic Transaction.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
In Class Assg 2 - solution
Java Servlets and JSP.
Servlets.
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.
Basic servlet structure
Presentation transcript:

Servlets and JDBC

Servlets

A form

The HTML source Chapter 1 Please enter your name and password then press start Name: Password:

A servlet import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class GetDemo extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("uname"); String password = request.getParameter("userpw");

out.println(" "); out.println(" GetDemo Output "); out.println(" "); out.println("Hello " + username + " "); out.println("Your password was: " + password + " "); out.println(" "); out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }

Servlet Container You need a servlet container for this. E.g. Apache Tomcat. Download the preconfigured Apache Tomcat from the course website. Unzip Will create two directories, "apache-tomcat " and "development" Starting/Stopping Tomcat If you are working on Windows, open file apache-tomcat \bin\startup.bat and modify line: set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_09 to reflect your local path of java jdk. (Do the same for the shutdown.bat) If you are working on the lab machines (Linux) execute first chmod +x apache-tomcat /bin/*.sh

Servlet Container Then start Apache Tomcat by double clicking on apache-tomcat \bin\startup.bat (In Windows) or execute apache-tomcat /bin/startup.sh (in Linux) To stop Tomcat execute the shutdown.bat in Windows, shutdown.sh in Linux.

Servlet Container Your HTML files should go under: apache-tomcat /webapps/ROOT (In Linux) apache-tomcat \webapps\ROOT (In Windows) Then to open an HTML file in the browser, specify e.g. Your Java classes should go under: apache-tomcat /webapps/ROOT/WEB-INF/classes (In Linux) apache-tomcat \webapps\ROOT\WEB-INF\classes (In Windows) Any jar file (such as classes12.jar of Oracle JDBC) should go under: apache-tomcat /webapps/ROOT/WEB-INF/lib (In Linux) apache-tomcat \webapps\ROOT\WEB-INF (In Windows)

Servlet Container Copy/sftp the /opt/oracle/jar/classes12.jar to your development directory. To compile, cd to the 'development' directory and execute the following: javac -d../apache-tomcat /webapps/ROOT/WEB-INF/classes - classpath../apache-tomcat /lib/servlet-api.jar:classes12.jar *.java (In Linux) javac -d..\apache-tomcat \webapps\ROOT\WEB-INF\classes - classpath..\apache-tomcat \lib\servlet-api.jar;classes12.jar *.java (In Windows) These commands will copy your.class files to the..\apache-tomcat \webapps\ROOT\WEB-INF\classes directory.

Connection manager import java.sql.*; import java.util.*; public class ConnectionManager { private static ConnectionManager instance = null; private Stack connections; private ConnectionManager () { connections = new Stack(); try { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); } catch (Exception ex) { System.out.println(ex); } } public static ConnectionManager getInstance() { if (instance == null) instance = new ConnectionManager(); return instance; }

Connection manager public Connection getConnection() { Connection conn = null; if (!connections.empty()) conn = (Connection) connections.pop(); else { //No one left in the stack, create a new one try { conn = DriverManager.getConnection "user", "passw"); } catch (SQLException ex) { System.out.println("SQLException: " + ex); } return conn; } public void returnConnection(Connection conn) { if (conn != null) connections.push(conn); } This is assuming the ssh tunel is used. Otherwise, if running on the lab machines, replace by "user", "passw")

SSH Tunnel In order to be able to connect to ORACLE remotely from your machine at home execute (in your machine): ssh2 -L knoppix.csc.uvic.ca Use some other lab machine. Might be called just ssh in your machine.

Insert Servlet import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Insert extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = request.getParameter("title"); String year = request.getParameter("year"); String length = request.getParameter("length"); String studioName = request.getParameter("studio"); String statementString = "INSERT INTO Movie(title, year, length, studioName) " + "VALUES( '" + title + "'," + year + "," + length + ",'" + studioName + "')";

Connection conn = ConnectionManager.getInstance().getConnection(); try { Statement stmt = conn.createStatement(); stmt.executeUpdate(statementString); stmt.close(); } catch(SQLException e) { out.println(e); } ConnectionManager.getInstance().returnConnection(conn); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }

The HTML source Insert Please enter your name and password then press start Title: Year: Length: Studio:

MovieServlet1 import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class MovieServlet1 extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Connection conn = ConnectionManager.getInstance().getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "SELECT title, year " + "FROM Movie");

out.println(" "); while (rset.next()) { out.println(" "); out.print ( " "+rset.getString("title")+" " + " "+rset.getString("year")+" "); out.println(" "); } stmt.close(); } catch(SQLException e) { out.println(e); } ConnectionManager.getInstance().returnConnection(conn); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }

MovieServlet2 import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class MovieServlet2 extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String year = request.getParameter("year"); Connection conn = ConnectionManager.getInstance().getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "SELECT title, year " + "FROM Movie " + "WHERE year = " + year);

out.println(" "); while (rset.next()) { out.println(" "); out.print ( " "+rset.getString("title")+" " + " "+rset.getString("year")+" "); out.println(" "); } stmt.close(); } catch(SQLException e) { out.println(e); } ConnectionManager.getInstance().returnConnection(conn); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }