An XML-Based JDBC Connector Servlet Framework Geng Tang 03/05/2001.

Slides:



Advertisements
Similar presentations
An XML Based JDBC Connector Servlet Framework By Narasimhan Rengaswamy.
Advertisements

J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
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
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.
27-Jun-15 Directories and DDs. 2 Web apps A web application is basically a web site that: “Knows who you are”--it doesn’t just give you static pages,
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.
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.
Server Side Programming Web Information Systems 2012.
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.
Java Server Programming Jeff Schmitt Towson University October 15, 1998.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
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 support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
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. Servlets When we run small Java programs within a browser these are referred to as Applets... And when we run small Java programs within.
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Servlet Filters import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { /* J2EE v1.3 Filter.
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Apr 3, 2013 JSP Java Server Pages. 2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet.
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlet.
Java Server Pages.
Java Servlets and JSP.
Servlets.
Introduction to Java Servlets
Basic servlet structure
Presentation transcript:

An XML-Based JDBC Connector Servlet Framework Geng Tang 03/05/2001

What is JCS ? zJDBC Connector Servlet

Com.jresources.servlets Package zClass JDBCConnectorServlet zpackage com.jresources.servlets; zimport javax.servlet.*; zimport javax.servlet.http.*; zimport java.io.*; zimport com.jresources.jcs.*; zimport java.util.Enumeration; zimport java.util.Properties; zpublic class JDBCConnectorServlet extends HttpServlet { zprivate java.util.Properties initProps = new Properties(); zpublic void init(ServletConfig config) throws ServletException { zsuper.init(config); zEnumeration e = config.getInitParameterNames(); zwhile(e.hasMoreElements()) { zString currParam = (String)e.nextElement(); zinitProps.put(currParam, config.getInitParameter(currParam)); z}

zpublic void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { z jcsEngine mainMod = new jcsEngine(); ztry {String qryDoc; zif(request.getPathInfo()==null) { zqryDoc = getServletConfig().getServletContext().getRealPath(request.getServletPath()); z}else { zqryDoc = request.getPathTranslated(); z} zmainMod.processRequest(request, this.initProps,qryDoc); zif(mainMod.getResponseType()==jcsEngine.RESPONSE_REDIRECT) { zresponse.sendRedirect(mainMod.getResponse()); z} else { zPrintWriter out = response.getWriter(); zresponse.setContentType(mainMod.getMIMEType()); zout.println(mainMod.getResponse()); z} x}catch(Exception e) { x String errorMsg = "An error occurred while executing the query " + – request.getServletPath() + " with the JCS servlet: " + e.toString(); z this.getServletContext().log(errorMsg, e); z if(mainMod.getErrorPage()=="") { z throw new ServletException(errorMsg, e); z }else { z response.sendRedirect(mainMod.getErrorPage()); z } z public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {doGet(request, response); z }

Method in jcsEngine z public void processRequest(HttpServletRequest request, String qryDoc) throws Exception z { zthis.processRequest(request, null, qryDoc); z} z/** z * Executes the request with custom properties z */ zpublic void processRequest(HttpServletRequest request, Properties customProps, String qryDoc) throws Exception z { z//our results zStringBuffer strResults = new StringBuffer(""); z//open the script zString qryContent = FunctionLib.OpenFile(qryDoc); zqryContent = this.insertFormVariables(qryContent, request); z//check for custom properties...if exist, then apply zif(customProps!=null) { zqryContent = this.insertJCSVariables(qryContent, customProps); z} z//process SQLEncode function calls zString[] workingQuery = new String[1]; zworkingQuery[0] = qryContent; zthis.processSQLEncode(workingQuery, new StringBuffer()); zqryContent = workingQuery[0]; z//parse the query script zjcsQuery objQuery = new jcsQuery(qryContent); z//TODO: reimplement this code as a finite state machine to make it cleaner and more extensible z//set the errorpage for error handling zerrorPage = objQuery.getErrorPage(); z//create a string array to hold the template text zString[] workingTemplate = new String[1];

z//open the template file zif(objQuery.getTemplateDoc()!="") { zworkingTemplate[0] = objQuery.getTemplateDoc(); z//process any server-side includes it might have zthis.processSSI(workingTemplate, new StringBuffer(), qryDoc, 0); z} z//instantiate and register the JDBC driver zClass.forName(objQuery.getJDBCDriver()); z//connect to the database and create a statement zConnection conn = DriverManager.getConnection(objQuery.getJDBCURL(), objQuery.getUID(), objQuery.getPWD()); zStatement s = conn.createStatement(); z//if it's a "write" query, execute it and redirect to the redirectURL z//if there is no redirect url, add the template to the results stringbuffer zif(objQuery.IsTransaction()) { zs.executeUpdate(objQuery.getSQL()); z//System.out.println(objQuery.getRedirectURL()); zif(objQuery.getRedirectURL()!="") { zconn.close(); //clean up the db connection zthis.response = objQuery.getRedirectURL(); zthis.responseType = jcsEngine.RESPONSE_REDIRECT; zreturn; z}else { zstrResults.append(workingTemplate[0]); z}

z//otherwise, further parse the template for the jcs:resultset tag... z//execute the query, loop thru the rs, apply the template's display zelse{ zif(objQuery.getMaxRows() > -1){ zs.setMaxRows(objQuery.getMaxRows()); z} zResultSet rs = s.executeQuery(objQuery.getSQL()); z//if there is no template, then send the default z//XML document zif(objQuery.getTemplateDoc().equals("")) { zXMLSerializableResultset xrs = new XMLSerializableResultset(rs); zthis.response = xrs.getXML(); zthis.MIMEType = "text/xml"; zthis.responseType = jcsEngine.RESPONSE_WRITE; zrs.close(); zconn.close(); zreturn;

z}else { zjcsTemplateParser template = new jcsTemplateParser(); ztemplate.setTemplate(workingTemplate[0]); zstrResults.append(template.getHeader()); zResultSetMetaData rsMetadata = rs.getMetaData(); zint intFields = rsMetadata.getColumnCount(); zString recordOutput; zString currFieldValue; zString AdjFieldValue; zint recordCount = 0; zwhile(rs.next()) { zrecordOutput = template.getBody(); zfor(int i =1; i <= intFields; i++) { zcurrFieldValue = FunctionLib.EncodeSpecialCharacters(rs.getString(i)); zAdjFieldValue = objQuery.UseHTMLLineBreak()?FunctionLib.HTMLLineBreak(currFieldValue):currFieldValue; zrecordOutput = FunctionLib.Replace(recordOutput, "#" + rsMetadata.getColumnName(i) + "#", AdjFieldValue); z} zstrResults.append(recordOutput); zrecordCount++; z} zif(recordCount == 0) { zstrResults.append(objQuery.getEmptyResultsetOutput()); z} zstrResults.append(template.getFooter()); z} zrs.close();

z} z//end sql check zconn.close(); zthis.MIMEType = objQuery.getMIMEType(); zthis.responseType = jcsEngine.RESPONSE_WRITE; z//make sure we fill out any form or cgi variables in the template... zString tmpResponse = insertFormVariables(strResults.toString(),request); z//check for custom properties...if exist, then apply zif(customProps!=null) { ztmpResponse = this.insertJCSVariables(tmpResponse, customProps); z} zthis.response=tmpResponse; zreturn; z }

Some important files zjcs_taglib.tld zweb.xml

jcs_taglib.tld z z<!DOCTYPE taglib z PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" z" z z 1.0 z 1.1 z jcs z zA custom JSP tag implementation of the JResources.com DB Connector servlet z z query_object z com.jresources.jcs.jcsCustomTag z com.jresources.jcs.jcsCustomTagOutput z A custom JSP tag implementation of the JResources.com DB Connector servlet z z url z true z z outputvariable z true z

Web.xml z z<!DOCTYPE web-app z PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" z " z z JResources.com Database Connector Servlet z zThe JResources.com Database Connector Servlet is a Java zservlet that provides an XML-based interface to JDBC data zsources. With the JResources.com DB connector servlet, zweb developers now have an easy way to query and modify zJDBC databases without writing a line of Java code. z z jdbc_url z jdbc:odbc:guestbook z z The default URL for the guestbook app's database z

(continued) z z output z z This is an example of using JCS from a JSP with custom z properties read from the web application's context parameters. z In Tomcat, these are set up in the WEB-INF/web.xml file z zSample output for the guestbook app z z JCS z z com.jresources.servlets.JDBCConnectorServlet z z defaultDriver z z sun.jdbc.odbc.JdbcOdbcDriver z

(continued) z z JCS z z *.jcs z z z z /WEB-INF/jcs_taglib.tld z

Guestbook application zCreate a SELECT query zAdding and modifying data zGenerating XML output from query zUsing JCS from within a JSP zUsing XSLT with JCS

guestbook.jcs z z jdbc:odbc:guestbook z select * from guestbook order by date z z<![CDATA[ z ztemplates\header.inc z z Guestbook Entries (as of #system.date#) z z Visitor z z Date z z #fname# #lname# z z #date# z

(Continued) z ztemplates\footer.inc z z]]> z z<![CDATA[ z z There are no entries in the guestbook. z z]]> z z HTML z

guestbook_xml_default.jcs z z jdbc:odbc:guestbook z select * from guestbook order by date z

guestbook_xml.jcs z z text/xml z jdbc:odbc:guestbook z select * from guestbook order by date z z<![CDATA[ z z#comments# z z #date# z #host# z z]]> z

guestbook.jsp z z<% ztry{ zjcs.processRequest(request, getServletConfig().getServletContext().getRealPath("/samples/guestbook/guestbook.jcs")); z} zcatch(Exception e){} zif(jcs.getResponseType()==com.jresources.jcs.jcsEngine.RESPONSE_REDIRECT) { zresponse.sendRedirect(jcs.getResponse()); z}else { z%> z z<% z} z%>

guestbook_tag.jsp z

guestbook_custom.jsp z z<% zjava.util.Properties prop = new java.util.Properties(); zprop.put("jdbc_url", "jdbc:odbc:guestbook"); zprop.put("output", "This is an example of using JCS from a JSP with custom properties passed from the JSP script to the JCS engine"); ztry{ zjcs.processRequest(request, prop, getServletConfig().getServletContext().getRealPath("/samples/guestbook/jsp.guestbook.jcs")); z}catch(Exception e){} zif(jcs.getResponseType()==com.jresources.jcs.jcsEngine.RESPONSE_REDIRECT) { zresponse.sendRedirect(jcs.getResponse()); z}else { z%> z z<% z} z%>

Jsp.guestbook.jcs z z #jcs.jdbc_url# z select * from guestbook order by date z z<![CDATA[ z ztemplates/header.inc z z Guestbook Entries (as of #system.date#) z z Visitor z z Date z

z #fname# #lname# z z #date# z z #jcs.output# z ztemplates/footer.inc z z]]> z z HTML z

guestbook_xsl.jsp z z<% z/* zThis sample uses Xalan 1.1. Download it from Ensure that xalan.jar and xerces.jar are in Tomcat's classpath. z*/ ztry{ z z XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); z processor.process(new XSLTInputSource(new java.io.StringReader(guestbook.toString())), new XSLTInputSource("file:///" + getServletConfig().getServletContext().getRealPath("/samples/guestbook/guestbook.xsl")), new XSLTResultTarget(out)); z}catch(Exception e) { z%> zAn error occurred processing your operation... z<% z} z%>

guestbook_tag_custom.jsp z

guestbook_submit.jcs z z jdbc:odbc:guestbook z select * from guestbook order by date z z<![CDATA[ z ztemplates\header.inc z z Sign the Guestbook: z z Your first name: z Your last name: z Your comments: z <input type="reset" value="Reset" z name="B2"> z

(continued) z z Other Guestbook Entries (as of #system.date#) z z Visitor z z Date z z #fname# #lname# z z #date# z ztemplates\footer.inc z z]]> z z HTML z

guestbook_add.jcs z z jdbc:odbc:guestbook z zINSERT INTO guestbook ("ID", "fname", "lname", "comments", "host", "date") VALUES ('#system.UID#',' #form.fname# ',' #form.lname# ', ' #form.comments# ', '#CGI.REMOTE_ADDR#', Now()) z z guestbook.jcs z error_page.htm z

guestbook_delete.jcs z z jdbc:odbc:guestbook z zdelete from guestbook where ID = '#form.ID#' z z guestbook.jcs z error_page.htm z

guestbook_view_entry.jcs z z jdbc:odbc:guestbook z select * from guestbook where ID = '#form.ID#' z z<![CDATA[ z ztemplates\header.inc z z #fname# #lname#'s Guestbook Entry z Comments: #comments# z Date entered: #date# z Originating Host: #host# z Delete this entry z ztemplates\footer.inc z z]]> z z HTML z

Summary zReusable zaccess the record in relational database via JDBC without Java zrapid creation of XML data server zportable

Questions ?