Examples of Using Servlets and JSP Representation and Management of Data on the Internet.

Slides:



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

Java Server Pages (JSP)
 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.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
Servlets and JDBC. Servlets A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
Ch.16 JDBC (Java DataBase Connectivity) PIKE Lab. 석사 2 학기 이 은 정.
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
JSP Java Server Pages Reference:
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.
Introduction to Servlet & JSP
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.
Java Servlets and JSP.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML.
1 Databases & Web-based Applications JDBC & Java Servlets A. Benabdelkader ©UvA, 2002/2003.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
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.
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.
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.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
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.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
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.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
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.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Introduction to Servlets
J2EE (Enterprise Programing)
PERTEMUAN 3.
Servlet Fudamentals.
JDBC & Servlet CSE 4504/6504 Lab.
CS320 Web and Internet Programming Database Access with JDBC
Course Outcomes of Advanced Java Programming AJP (17625, C603)
HW#4 Making Simple BBS Using JDBC
Java Servlets and JSP.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Introduction to Java Servlets
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Examples of Using Servlets and JSP Representation and Management of Data on the Internet

Working with Xaln Combining Servlets and XSL

Working With XSL Add to the CLASSPATH: –setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apa che/xerces/xerces.jar –setenv CLASSPATH ${CLASSPATH}:/usr/local/java/apa che/xalan/xalan.jar You can download from the Web newer versions of Xalan and Xerces You can download from the Web newer versions of Xalan and Xerces

Java Hello Java 19$ XML XML for Beginners 12$ XSL XSL in a Nut Shell 34$ JSP Introduction to JSP 17$ JavaScript A Path to JavaScript 33$ doc.xml

<xsl:stylesheet version="1.0" xmlns:xsl=" A dbi Example Books List   doc.xsl

TransformServlet

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.xalan.xslt.*; public class TransformServlet extends HttpServlet { // Respond to HTTP GET requests from browsers. public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set content type for HTML. response.setContentType("text/html; charset=UTF-8"); // Output goes to the response PrintWriter. PrintWriter out = response.getWriter();

try { XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); // Get the XML input document and the stylesheet, // both in the servlet engine document directory. XSLTInputSource xmlSource = new XSLTInputSource(new java.net.URL (" ~yarok/doc.xml").openStream()); XSLTInputSource xslSource = new XSLTInputSource(new java.net.URL (" ~yarok/doc.xsl").openStream()); // Generate the result. XSLTResultTarget target = new XSLTResultTarget(out); // Creating the output processor.process(xmlSource, xslSource, target); }

// If an Exception occurs, //return the error to the client. catch (Exception e) { out.write(e.getMessage()); e.printStackTrace(out); } // Close the PrintWriter. out.close(); }

Creating Results to a File A More Simple Example (not a servlet) –Gets input from files where file names given as parameters –Write output to a file whose name given as a parameter

import org.apache.xalan.xslt.XSLTProcessorFactory; import org.apache.xalan.xslt.XSLTInputSource; import org.apache.xalan.xslt.XSLTResultTarget; import org.apache.xalan.xslt.XSLTProcessor; /** * Simple sample code to show how * to run the XSL processor * from the API. */ public class TransformExample { public static void main(String[] args) throws java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException {

// Have the XSLTProcessorFactory // obtain a interface to a // new XSLTProcessor object. XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); processor.process(new XSLTInputSource(args[0]), new XSLTInputSource(args[1]), new XSLTResultTarget(args[2])); }

Using Beans in JSP

The Bean Object The bean object is under the directory /lib/dbi The bean belongs to the package “dbi”

package dbi; import java.sql.*; import java.io.*; /** * Example of using Java Beans in JSP pages */ public class DBBean { String login = null; String url = "jdbc:oracle:thin:"+ login + "/" + login + String driver = "oracle.jdbc.driver.OracleDriver"; private Connection connection;

public DBBean() { super(); } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; }

public boolean connect() throws ClassNotFoundException,SQLException { Class.forName(driver); connection = DriverManager.getConnection(url); return true; } public void close() throws SQLException { connection.close(); }

public ResultSet poseQuery(String sql) throws SQLException { Statement s = connection.createStatement(); ResultSet rs = s.executeQuery(sql); return rs; } public int applyUpdate(String sql) throws SQLException { Statement s = connection.createStatement(); int r = s.executeUpdate(sql); return r; } }

The JSP Pages There are three JSP pages The first page opens the connection The third page closes the connection All three pages using the same bean

First Page

Example of using beans in a JSP – Connect <jsp:useBean id="db" scope="session" class="dbi.DBBean" /> <jsp:setProperty name="db" property="login" param="login" /> <%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Created the Table (in the database of the user )

<% db.connect(); try { db.applyUpdate("DROP TABLE test"); } catch (Exception e) {} try { i = db.applyUpdate("CREATE TABLE test (name varchar2(10), id number(6))"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException ("Error in the query", e); } rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>

Created the Tables <a href="dbinsert.jsp?login= ">Next

Second Page

Example of using beans in a JSP – Insert <jsp:useBean id="db" scope="session" class="dbi.DBBean" /> <jsp:setProperty name="db" property="login" param="login" /> <%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Inserting Two Rows (in the database of the user )

<% try{ i = db.applyUpdate( "INSERT INTO test VALUES ('aaa', 123)"); i = db.applyUpdate( "INSERT INTO test VALUES ('bbb', 456)"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException ("Error in the query", e); } rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>

After Insertion <a href="dbinsertmore.jsp?login= ">Next

Third Page

Example of using beans in a JSP – Insert <jsp:useBean id="db" scope="session" class="dbi.DBBean" /> <jsp:setProperty name="db" property="login" param="login" /> <%! ResultSet rs = null ; ResultSetMetaData rsmd = null ; int numColumns ; int i; %>

Inserting Two More Rows (in the database of the user )

<% try{ i = db.applyUpdate( "INSERT INTO test VALUES (‘ccc', 789)"); i = db.applyUpdate( "INSERT INTO test VALUES (‘ddd', 911)"); rs = db.poseQuery("SELECT * FROM test"); } catch(SQLException e) { throw new ServletException ("Error in the query", e); } rsmd = rs.getMetaData(); numColumns = rsmd.getColumnCount(); %>

<% for (int column=1; column <= numColumns; column++) { %> <% } %> <% while(rs.next()) { %> <% } %>

<% db.close(); %> Done