41
An XML Based JDBC Connector Servlet Framework By Narasimhan Rengaswamy

An XML Based JDBC Connector Servlet Framework

  • Upload
    dorie

  • View
    58

  • Download
    0

Embed Size (px)

DESCRIPTION

An XML Based JDBC Connector Servlet Framework. By Narasimhan Rengaswamy. What is a JDBC Connector Servlet(JCS)?. A Java servlet that provides an XML based scripting interface to JDBC data sources - PowerPoint PPT Presentation

Citation preview

Page 1: An XML Based JDBC Connector Servlet Framework

An XML Based JDBC Connector Servlet Framework

By Narasimhan Rengaswamy

Page 2: An XML Based JDBC Connector Servlet Framework

What is a JDBC Connector Servlet(JCS)?

A Java servlet that provides an XML based scripting interface to JDBC data sources

Allows JSP and servlet developers to achieve a separation between server side presentation logic and their data access(SQL) logic

Page 3: An XML Based JDBC Connector Servlet Framework

Brief Idea about JDBCAn API used to write database clients(from applets to servlets, EJBs), connect to a relational database, execute SQL statements and process the results extractedDatabase connectivity is not just connecting to database and executing statements but also optimizing network resources using connection pooling and distributed transactions(e.g.WebLogic Enterprise CORBA Java and WebLogic Enterprise EJB applications)

Page 4: An XML Based JDBC Connector Servlet Framework

Servlet

A Java servlet contains the application code that executes on the server in response to an event. A servlet receives events from the web application page, performs the appropriate actions, and then subsequently outputs data dynamically to the application's presentation layout model by invoking a Java Server Page (JSP)

Page 5: An XML Based JDBC Connector Servlet Framework

Servlets(Contd.)

Are Http request handlers which provide full access to Java APIs and endow web programmers with a powerful tool for creating dynamic Web pagesA servlet is a Java component that can be plugged into a Java-enabled web server to provide custom services

Page 6: An XML Based JDBC Connector Servlet Framework

A little about XML

XML, the Extensible Markup Language, is the universal syntax for describing and structuring data, independent from application logic. XML is a method for putting structured data in a text file

Page 7: An XML Based JDBC Connector Servlet Framework

Case Study

Two Sections

1. Implementing the JDBC Connector Servlet

2. Using the JDBC Connector Servlet

Page 8: An XML Based JDBC Connector Servlet Framework

Implementing the JDBC Connector Servlet

Determine Framework’s Functional RequirementsDesign a High Level ArchitectureDesign the tool’s scripting FrameworkDesign and develop the Java classes that implement the scripting frameworkDesign and develop other artifacts to deploy framework to J2EE containers

Page 9: An XML Based JDBC Connector Servlet Framework

(Contd…)

Package the framework’s binaries for deployment to servlet engines and application servers that support J2EE

Deploy JCS to a J2EE web container

Page 10: An XML Based JDBC Connector Servlet Framework

Framework’s Functional Requirements

JCS should provide mechanism to Query a Database and return the result back to the browser.The output can be a XML document.Allow to view in a WAP device in a WML formatJCS must be able to execute INSERT,UPDATE and DELETE statements

Page 11: An XML Based JDBC Connector Servlet Framework

(Contd…)

Must allow HTML form,CGI and custom variables in JCS query files Allow HTTP GET or POST between the HTML form and a JCS QuerySupport SSI(Server Side Includes)

e.g. Library files

Page 12: An XML Based JDBC Connector Servlet Framework

(Contd…)Must accommodate default content to send to the browser e.g. if the select statements does not return the resultSupport Exception and redirecting of pages when error occursShould allow JCS queries within JSP applications by providing a bean interface and custom tagMust provide tags to generate unique Ids(e.g. primary key)

Page 13: An XML Based JDBC Connector Servlet Framework

The JCS Architecture HTTP

Appropriate Connector

Web Clients

Web Server(Apache,IIS)

Servlet Engine(Tomcat, various J2EE App Servers)

JDBC Connector Servlet

<jcs.query/>

jcs Engine

JSP Custom Tag

Java Server Pages

Relational Databases

Page 14: An XML Based JDBC Connector Servlet Framework

(Contd…)Step 1:Remote Browser requests URL for the XML

based queries.Step 2:Servlet Engine dispatch it to JDBC

Connector ServletStep 3:JDBC Connector Servlet Instantiates JCS

engine and parse the query and get back the result

Page 15: An XML Based JDBC Connector Servlet Framework

JCS Scripting Framework

JCS provides a scripting framework to write web-based JDBC queries JDBC queries are XML documents with special tags containing database connection parameters, SQL code and outputDesign of this XML schema is done by first creating a jcs namespace

Page 16: An XML Based JDBC Connector Servlet Framework

Snapshot of a JCS Query<?xml version=“1.0”?><jcs:query xmlns:jcs=“http://www.jresources.com/xml/jcs/1.0”> <jcs:mime_type>

<!—Your output’s MIME Type--></jcs:mime_type>

<jcs:jdbc_url> <!—The URL of the JDBC data source--></jcs:jdbc_url><jcs:jdbc_uid> <!—Your userid--></jcs:jdbc_uid><jcs:jdbc_pwd> <!—Your password--></jcs:jdbc_pwd><jcs:jdbc_driver> <!—Your JDBC driver--></jcs:jdbc_driver>

Page 17: An XML Based JDBC Connector Servlet Framework

(contd…)<jcs:sql>

<!--Your SQL statement.Here’s an example:--> SELECT * FROM guestbook WHERE ID = “#FORM.id#”</jcs:sql>

<jcs:error_page> <!—The page to redirect to in the event an exception is thrown --></jcs:error_page>

<jcs:redirect_url> <!—The page to redirect to after successfully executing an INSERT, update, or DELETE statement --></jcs:redirect_url>

<jcs:maxrows> <!—The maximum number of rows to be returned in the query--></jcs:maxrows>

Page 18: An XML Based JDBC Connector Servlet Framework

(Contd…)<jcs:template>

<!-- This is your output template--><! [CDATA[

<jcs:include><!= include file reference goes here -->

</jcs:include><!– HTML markup goes here --><jcs:resultset>

<!– HTML markup goes here -->Use #FIELD_NAME# to insert the contents of a particular

field <!– HTML markup goes here -->

</jcs:resultset><!– HTML markup goes here -->

] ]></jcs:template>

Page 19: An XML Based JDBC Connector Servlet Framework

JCS Query Tags

Database connectivity parameter tagsThe SQL tagOutput definition tagsSpecial function tagsTemplate-specific tags

Page 20: An XML Based JDBC Connector Servlet Framework

Database Connectivity Tags

Contains parameters to connect to what database, which JDBC driver to use and support authenticationTag names jcs:jdbc_driverjcs:jdbc_urljcs:jdbc_uidjcs:jdbc_pwd

Page 21: An XML Based JDBC Connector Servlet Framework

The SQL Tag

Contains <jcs:sql> tag

Page 22: An XML Based JDBC Connector Servlet Framework

Output Definition Tags

Jcs:mime_typeJcs:redirect_urlJcs:templateJcs:empty_resultset_outputJcs:error_pageJcs:maxrows

Page 23: An XML Based JDBC Connector Servlet Framework

Special function tags

Jcs:encode_sqlDoubles up single quote characters to replace them with two consecutive single quote charactersJcs:line_break_characterto handle carriage return and line feed characters in data retrieved from database(currently supports HTML only but future enhancement would be an XHTML)

Page 24: An XML Based JDBC Connector Servlet Framework

Template specific tags

<jcs:result_set>

loops through the rows of data returned from the query

<jcs:include>creates a server-side include reference within a JCS output template

Page 25: An XML Based JDBC Connector Servlet Framework

Variable Types JCS supports variable types to access

dynamic data.Variable names are case sensitiveForm VariablesResultset Field VariablesCGI VariablesSystem VariablesCustom Variables

Variables take the form #form.variablename#

Page 26: An XML Based JDBC Connector Servlet Framework

Form VariablesVariables passed by client to the server

via HTTP GET and POST methodSample JCS query form handler named

guestbook_add.jcs

<?xml version=“1.0”?><jcs:query xmlns:jcs=“http://www.jresources.com/xml/jcs/1.0”> <jcs:jdbc_url>jdbc:odbc:guestbook</jcs:jdbc_url>

<jcs:sql>INSERT INTO guestbook (“ID”, “fname”, “lname” ,

“comments”, “host”, “date”) VALUES

(‘#SYSTEM.UID#’,’#form.fname#’, ’#form.lname#’ , ’#form.lname#’ , ’#form.comments#’ ,’CGI.REMOTE_ADDR#’,NOW())

</jcs:sql>

Page 27: An XML Based JDBC Connector Servlet Framework

Resultset Field VariablesIt takes the form #fieldname#Used in output templates to reference values of a given record’s fields

Sample code<jcs:include>templates\header.inc</jcs:include><jcs:resultset>

<p><b>#fname# #lname#’s Guestbook Entry</b></p><p><b>Comments: </b>#comments#</p>

<p><b>Date entered: </b>#date#</p>

<p><b>Originating Host: </b>#host#</p><br><p><a href=“guestbook_delete.jcs?ID=“#ID#”>Delete this entry</a></p>

</jcs:resultset><jcs:include>templates\footer.inc</jcs:include>

Page 28: An XML Based JDBC Connector Servlet Framework

CGI VariablesUses form variables in #cgi.variablename# formatVariable names

#CGI.REMOTE_ADDR##CGI.SERVER_PROTOCOL##CGI.QUERY_STRING##CGI.REMOTE_USER##CGI.SERVER_NAME##CGI.SERVER_PORT##CGI.REMOTE_HOST#

Page 29: An XML Based JDBC Connector Servlet Framework

System Variables

#system.date#

Servers current date

#system.UID#

Generates a unique id

Page 30: An XML Based JDBC Connector Servlet Framework

Java Classes that implement JCS

Grouped into threeClasses providing functionality to reuse in other applications1) FunctionLib class from com.jresources.util package2) XMLSerializableResultset class from com.jresources.jdbc package

Page 31: An XML Based JDBC Connector Servlet Framework

Core JCS functionality classes

jcsRuntimeException classjcsQuery classjcsTemplateParser classjcsEngine class

from com.jresources.jcs package

Page 32: An XML Based JDBC Connector Servlet Framework

Classes providing server-sidepresentation services

jcsCustomTag classjcsCustomTagOutput classJDBCConnectorServlet class

Page 33: An XML Based JDBC Connector Servlet Framework

Functionality of FunctionLib class

Unique Id generationXML parsing and file I/O

Methods used aregenerateUID()OpenFile()getCDATASection() to parse jcs:template and jcs:empty_resultset_output tagsHTMLLineBreak() replaces a carriage return character with an HTML <BR> tag

Page 34: An XML Based JDBC Connector Servlet Framework

XMLSerializableResultSet class

Serializes a JDBC resultset as a XML document

Method used isgetXML()

Page 35: An XML Based JDBC Connector Servlet Framework

Core JCS functionality classes

jcsRuntimeException class Method:

jcsRuntimeException()

Page 36: An XML Based JDBC Connector Servlet Framework

(Contd…)jcsQuery class

How it works:1) Constants DEFAULT_JDBC_DRIVER (set to ODBC-JDBC bridge) and DEFAULT_MIME_TYPE (set to text/html) are declared2) Instantiate the class using default constructor setJCSQueryContent()

to parse the JCS query and initialize the instance variablesCODE:package com.jresources.jcs;import com.resources.util.FunctionLib;public class jcsQuery {//constantspublic static final String DEFAULT_JDBC_DRIVER =

“sun.jdbc.odbc.JdbcOdbcDriver” ;public static final String DEFAULT_MIME_TYPE = “ text/html ” ;

Page 37: An XML Based JDBC Connector Servlet Framework

(Contd…)//the JCS Query raw XMLprivate String jcsQueryContent;

//”Read-only” instance variablesprivate String strMIMEType = this.DEFAULT_MIME_TYPE;private String JDBCDriver = this.DEFAULT_JDBC_DRIVER;private String JDBCURL;private String UID;private String PWD;private String SQL;private String qrydoc;private String templateDoc;private String redirectURL;private String errorPage;private String emptyResultsetOutput;private int maxRows = -1 ;private boolean htmlLineBreakFlag = false;

Page 38: An XML Based JDBC Connector Servlet Framework

jcsTemplateParser Class

Has two constructors

parseTemplate()setFilename()setTagname()setTemplate()

Page 39: An XML Based JDBC Connector Servlet Framework

Class JCSEngine

Uses processRequest()processSSI()executeUpdate()executeQuery()

Page 40: An XML Based JDBC Connector Servlet Framework

Class jcsCustomTag

Provides functionality for custom JSP tagAllows pages written in JSP 1.1. to

execute JCS queriesTag has two attributes

urloutputvariable

Page 41: An XML Based JDBC Connector Servlet Framework

Class jcsCustomTagOutput

Provides an interface to the host JSP page to access the contents of output variable

Method:getVariableInfo()