Tutorial c-treeace Web Service Using Java



Similar documents
User's Guide FairCom Performance Monitor

Contents. 2. cttctx Performance Test Utility Server Side Plug-In Index All Rights Reserved.

User's Guide c-treeace Status Log Analyzer

c-treeace Server Monitor

Implementing the Shop with EJB

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Using the DataDirect Connect for JDBC Drivers with the Sun Java System Application Server

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

Oracle Business Intelligence Publisher. 1 Oracle Business Intelligence Publisher Certification. Certification Information 10g Release 3 (

Principles and Techniques of DBMS 5 Servlet

c-treeace V9.0 UpdateGuide

Creating Java EE Applications and Servlets with IntelliJ IDEA

Supplement IV.E: Tutorial for Tomcat. For Introduction to Java Programming By Y. Daniel Liang

Data Sheet VISUAL COBOL WHAT S NEW? COBOL JVM. Java Application Servers. Web Tools Platform PERFORMANCE. Web Services and JSP Tutorials

Using the DataDirect Connect for JDBC Drivers with WebLogic 8.1

Oracle WebLogic Server

Web Application Programmer's Guide

By Wick Gankanda Updated: August 8, 2012

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Oracle SQL Developer Migration

Penetration from application down to OS

Application Servers - BEA WebLogic. Installing the Application Server

Crystal Reports Installation Guide

Simba XMLA Provider for Oracle OLAP 2.0. Linux Administration Guide. Simba Technologies Inc. April 23, 2013

Crystal Reports XI Release 2 - Service Pack 6

TIBCO ActiveMatrix BusinessWorks Plug-in for TIBCO Managed File Transfer Software Installation

Geoportal Server Installation Guide for GlassFish Contents

Kony MobileFabric. Sync Windows Installation Manual - WebSphere. On-Premises. Release 6.5. Document Relevance and Accuracy

IBM TRIRIGA Anywhere Version 10 Release 4. Installing a development environment

PROGRESS DATADIRECT QA AND PERFORMANCE TESTING EXTENSIVE TESTING ENSURES DATA CONNECTIVITY THAT WORKS

2. Follow the installation directions and install the server on ccc

DOCUMENTATION SHADOWPROTECT - MICROSOFT WINDOWS SYSTEM BACKUP AND RESTORE OPERATIONS

PowerSchool Student Information System

Witango Application Server 6. Installation Guide for OS X

Implementing SSO between the Enterprise Portal and the EPM Add-In

IBM Systems Director Navigator for i5/os New Web console for i5, Fast, Easy, Ready

Set Up Hortonworks Hadoop with SQL Anywhere

Java Servlet Tutorial. Java Servlet Tutorial

WebSphere and Message Driven Beans

Getting Started with Web Applications

Teradata Query Scheduler. User Guide

Crystal Reports XI Release 2 for Windows Service Pack 3

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

Web Container Components Servlet JSP Tag Libraries

HP Business Service Management

TIBCO ActiveMatrix BusinessWorks Process Monitor Server. Installation

HP Business Service Management

Feith Rules Engine Version 8.1 Install Guide

Configure a SOAScheduler for a composite in SOA Suite 11g. By Robert Baumgartner, Senior Solution Architect ORACLE

Siebel Installation Guide for Microsoft Windows. Siebel Innovation Pack 2013 Version 8.1/8.2, Rev. A April 2014

ACM Crossroads Student Magazine The ACM's First Electronic Publication

J2EE-Application Server

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

PARALLELS SERVER BARE METAL 5.0 README

Supported Platforms. HP Vertica Analytic Database. Software Version: 7.1.x

IBM Tivoli Monitoring for Databases

Fuse ESB Enterprise Installation Guide

Siebel Installation Guide for UNIX. Siebel Innovation Pack 2013 Version 8.1/8.2, Rev. A April 2014

Using. DataTrust Secure Online Backup. To Protect Your. Hyper-V Virtual Environment. 1 P a g e

FileMaker 12. ODBC and JDBC Guide

Supported Platforms. HP Vertica Analytic Database. Software Version: 7.0.x

Oracle Fusion Middleware. 1 Oracle Team Productivity Center Server System Requirements. 2 Installing the Oracle Team Productivity Center Server

Dell Fabric Manager Installation Guide 1.0.0

HP Application Lifecycle Management

FileMaker 11. ODBC and JDBC Guide

Operating Instructions Software (Fax Driver)

INTRODUCTION TO WEB TECHNOLOGY

Oracle Product Data Quality

Oracle Fusion Middleware

Parallels Virtualization SDK ReadMe CONTENTS:

IBM Lotus Enterprise Integrator (LEI) for Domino. Version August 17, 2010

GlassFish Server Open Source Edition 3.1 Quick Start Guide

Users Guide. Ribo 3.0

An introduction to web programming with Java

How To Connect A Java To A Microsoft Database To An Ibm.Com Database On A Microsq Server On A Blackberry (Windows) Computer (Windows 2000) On A Powerpoint (Windows 5) On An Ubio.Com

CA Workload Automation Agent for Databases

Data Access Guide. BusinessObjects 11. Windows and UNIX

Kony MobileFabric. Sync Server Tomcat Installation Manual. On-Premises

FileMaker 8. Installing FileMaker 8 ODBC and JDBC Client Drivers

HP Service Manager Compatibility Matrix

NetIQ Sentinel Quick Start Guide

An Oracle White Paper February Oracle Data Integrator 12c Architecture Overview

An Oracle White Paper November Oracle Business Intelligence Standard Edition One 11g

DOCUMENTATION SYSTEM STATE BACKUP & RESTORE OPERATIONS

Parallels Desktop for Mac

TIBCO Enterprise Administrator Release Notes

Glassfish, JAVA EE, Servlets, JSP, EJB

HP Vertica Integration with SAP Business Objects: Tips and Techniques. HP Vertica Analytic Database

Using the DataDirect Connect for JDBC Drivers with Tomcat

Installing and Configuring Adobe LiveCycle 9.5 Connector for Microsoft SharePoint

24x7 Scheduler Multi-platform Edition 5.2

GETTING STARTED GUIDE

To use PowerChute Web Device Manager, the Smart-UPS must connect to your network through one of the following types of servers or workstations:

Copyright Notice. Information in this document is subject to change without notice.

Oracle WebCenter Sites. Installing on Oracle WebLogic Application Server 11g Release 1 (11.1.1)

HP SiteScope. HP Vertica Solution Template Best Practices. For the Windows, Solaris, and Linux operating systems. Software Version: 11.

Release Bulletin EAServer for HP-UX Itanium and IBM AIX

Microsoft Windows Apple Mac OS X

How to Install MS SQL Server Express

Transcription:

Tutorial c-treeace Web Service Using Java

Tutorial c-treeace Web Service Using Java

Copyright 1992-2012 FairCom Corporation All rights reserved. No part of this publication may be stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise without the prior written permission of FairCom Corporation. Printed in the United States of America. Information in this document is subject to change without notice. Trademarks c-treeace, c-tree, c-tree Plus, r-tree, the circular disk logo, and FairCom are registered trademarks of the FairCom Corporation. c-treeace for COBOL, c-treeace SQL, c-treeace SQL ODBC, c-treeace SQL ODBC SDK, c-treevcl/clx, c-tree ODBC Driver, c-tree Crystal Reports Driver, c-treedb, and c-treephp are trademarks of FairCom Corporation. The following are third-party trademarks: AMD and AMD Opteron are trademarks of Advanced Micro Devices, Inc. Macintosh, Mac OS, and Xcode are trademarks of Apple Inc., registered in the U.S. and other countries. Embarcadero, the Embarcadero Technologies logos and all other Embarcadero Technologies product or service names are trademarks, service marks, and/or registered trademarks of Embarcadero Technologies, Inc. and are protected by the laws of the United States and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company.. DBstore is a trademark of Dharma Systems, Inc. HP and HP-UX are registered trademarks of the Hewlett-Packard Company. AIX, IBM, OS/2, OS/2 WARP, Power 6 and POWER7 are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. Intel, Itanium, Pentium and Xeon are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries. LynuxWorks, LynxOS and BlueCat are registered trademarks of LynuxWorks, Inc. Microsoft, the.net logo, MS-DOS, Windows, Windows Mobile, Windows NT, Windows Server, Windows Vista, Visual Basic, and Visual Studio, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Novell, NetWare, and SUSE are registered trademarks of Novell, Inc., in the United States and other countries. Oracle and Java are registered trademarks of Oracle and/or its affiliates. QNX and Neutrino are registered trademarks of QNX Software Systems Ltd. in certain jurisdictions. Red Hat and the Shadow Man logo are registered trademarks of Red Hat, Inc. in the United States and other countries, used with permission. SCO and SCO Open Server are trademarks or registered trademarks of The SCO Group, Inc. in the U.S.A. and other countries. SGI and IRIX are registered trademarks of Silicon Graphics, Inc., in the United States and/or other countries worldwide. UNIX and UnixWare are registered trademarks of The Open Group in the United States and other countries. Linux is a trademark of Linus Torvalds in the United States, other countries, or both. Python and PyCon are trademarks or registered trademarks of the Python Software Foundation. All other trademarks, trade names, company names, product names, and registered trademarks are the property of their respective holders. FairCom welcomes your comments on this document and the software it describes. Send comments to: Documentation Comments FairCom Corporation 6300 W. Sugar Creek Drive Columbia, MO 65203 Portions 1987-2012 Dharma Systems, Inc. All rights reserved. This software or web site utilizes or contains material that is 1994-2007 DUNDAS DATA VISUALIZATION, INC. and its licensors, all rights reserved. 9/28/2012

CONTENTS Creating a c-treeace Web Service for JEE...1 1.1 Configure GlassFish... 3 1.2 Create a Project in NetBeans... 4 1.3 Set Up the DataSource... 6 1.4 Create the Web Service... 9 1.5 Create the Servlet... 13 1.6 Edit the Web Page... 18 1.7 Try it out... 19 1.8 Additional Resources... 20 w w w. f a i r c o m. c o m All Rights Reserved iii

Creating a c-treeace Web Service for JEE Chapter 1 This tutorial will explore the process of creating a Web Service that makes a c-treeace database available to JEE (Java Enterprise Edition) applications. JEE allows developers to combine modules built with EJB (Enterprise Java Beans) to create powerful enterprise applications. We will use NetBeans to create the Web Service and GlassFish to deploy it. Developers working in a Windows environment could instead use ADO.NET to create a Web Service. Your choice of.net or Java largely depends on the environment you prefer; c-treeace provides support for both. The Web Service will allow the database to be accessed by other applications; in this case, a web page. You can use these principles to make your own database available as a Web Service to JEE applications of your choosing. The Web Service in this tutorial will provide two operations: SearchByCustNum: return a single customer record from a customer file (custmast) based on the customer ID SelectAll: return all the customer records in the customer file Typical operations you may want a Web Service to perform might include the ability to add, edit, or delete records. You could perform complex queries specific to your application. Or you might invoke stored procedures containing your business logic. In this tutorial, we will interact with the Web Service using a web page that contains two Submit buttons labeled Read Customer (one button for each of the two operations): Prerequisites For this tutorial, it will help to have a basic knowledge of Web Services and the c-treeace JDBC driver (you will need c-treeace V9.x for this tutorial). And, of course, you will need to have NetBeans and GlassFish set up and ready to go. You will also need a Java Developers Kit (JDK) and Java Runtime Environment (JRE). This example will use the c-treeace sample custmast database. If you have run any of the standard c-treeace tutorials, you will have a custmast database and it will contain a few records. If you do not have a custmast database, you will need to create one. Either run one of the c-treeace tutorials or use the c-treeace SQL Explorer tool to create one. The image in the Try it out... (page 19) section at the end of the tutorial shows typical sample data; feel free to make up your own. w w w. f a i r c o m. c o m All Rights Reserved 1

Chapter 1 Creating a c-treeace Web Service for JEE In this tutorial you will: Configure GlassFish (page 3) - Before you begin, you will need to configure GlassFish. Create a Project in NetBeans (page 4) - You will begin by creating a project in NetBeans. Set Up the DataSource (page 6) - Next, you will set up a DataSource in GlassFish using c-treeace SQL JDBC. Create the Web Service (page 9) - Then you will create the Web Service: First you will create an "empty" Web Service, which will be a file called custmast.java. Next you will create the two operations: SearchByCustNum and SelectAll. Then you will build and deploy the Web Service. Create the Servlet (page 13) - You will create a Servlet to connect the web page to the Web Service. The Servlet will call the appropriate operation in the Web Service (either SearchByCustNum or SelectAll). Edit the Web Page (page 18) - Finally, you will edit the initial web page, index.jsp, to create two Submit buttons labeled Read Customer: one will call SearchByCustNum; the other will call SelectAll. Try it out... (page 19) You will find Additional Resources (page 20) at the end of this document. F a i r C o m C o r p o r a t i o n 2 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 1.1 Configure GlassFish Before we begin, we need to configure GlassFish for c treeace. You will need to edit domain.xml, located in: <GLASS_FISH_INSTALL_DIR>\glassfish\domains\domain1\config Change these settings: Change PermSize to: -XX:PermSize=128m Change MaxPermSize to: -XX:MaxPermSize=512m Change Memory Usage to: -Xmx1024m w w w. f a i r c o m. c o m All Rights Reserved 3

Chapter 1 Creating a c-treeace Web Service for JEE 1.2 Create a Project in NetBeans We will begin by creating the project so we can configure the JDBC DataSource: 1. Create a new Web Application project (click File > New Project then Java Web and Web Application). We will call the project WebService1. 2. Click Next to configure the Server and Java EE information. We will use the GlassFish Server and Java EE 6 Web. 3. No frameworks will be used in this tutorial. F a i r C o m C o r p o r a t i o n 4 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 4. Click Finish to create the project. w w w. f a i r c o m. c o m All Rights Reserved 5

Chapter 1 Creating a c-treeace Web Service for JEE 1.3 Set Up the DataSource Next, we will set up a JDBC DataSource in GlassFish: 1. Using Windows Explorer or the command prompt, copy the ctreejdbc.jar file (shipped with c-treeace) from the <CTREE_INSTALL_DIR>\lib\sql.jdbc directory to <GLASS_FISH_INSTALL_DIR>\lib 2. Start the GlassFish server. 3. In NetBeans, click File > New File and select GlassFish > JDBC Resource. 4. Choose Create New JDBC Connection Pool and enter the JNDI Name: jdbc/ctreesql F a i r C o m C o r p o r a t i o n 6 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 5. You will not enter anything in Additional Properties, so click Next on the subsequent screen. 6. Enter the JDBC Connection Pool Name, ctreesqlpool, and select Other for the New Configuration using Database option. w w w. f a i r c o m. c o m All Rights Reserved 7

Chapter 1 Creating a c-treeace Web Service for JEE 7. Enter the DataSource Classname: ctree.jdbcx.ctreeconnectionpooldatasource. For the Resource Type, select javax.sql.connectionpooldatasource. Add the following properties: User = ADMIN Password = ADMIN portnumber = 6597 databasename = ctreesql servername = localhost 8. Click Finish to create the connection resources. F a i r C o m C o r p o r a t i o n 8 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 1.4 Create the Web Service Now it is time to create the Web Service. Begin Creating the Web Service First we will create an "empty" Web Service (this will be a file called custmast.java): 1. Right-click the project and choose New > Web Service. 2. Enter the Web Service Name (we will call it Custmast). In the Package box type Tutorial, and check Implement Web Service as Stateless Session Bean. 3. Click Finish to create the Web Service. Add Operations Having created our Web Service, we must add the operations it will perform. For this tutorial, we will create two operations: SearchByCustNum and SelectAll: 1. Expand the Web Services group. Right-click on the new Custmast Web Service and select Add Operation. 2. Enter an operation Name (call it SearchByCustNum). SearchByCustNum will need a single parameter (call it custnum). Click Add to add the parameter. w w w. f a i r c o m. c o m All Rights Reserved 9

Chapter 1 Creating a c-treeace Web Service for JEE 3. Repeat the steps above to create a second operation in your web service called SelectAll. SelectAll does not need a parameter. The new operations are implemented by adding Java code in the method s body. You will need to edit the Java in the Custmast.java file to make it appear as shown below: Custmast.java package Tutorial; import java.sql.connection; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.logging.level; import java.util.logging.logger; import javax.jws.webservice; import javax.jws.webmethod; import javax.jws.webparam; import javax.ejb.stateless; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception; F a i r C o m C o r p o r a t i o n 10 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java import javax.sql.datasource; The Custmast provides a basic web service for retrieving data from the Custmast table. @author FairCom Corporation / @WebService(serviceName = "Custmast") @Stateless() public class Custmast { Searches for a Custmast record based on the "customer number" information (Primary Key) @param custnum Customer number to searched. @see SelectAll / @WebMethod(operationName = "SearchByCustNum") public String SearchByCustNum(@WebParam(name = "custnum") String custnum) { String resultvalue = ""; // Retrieve the list of custmast records from the database connection try { Statement statement = getstatement(); ResultSet resultset = statement.executequery("select from custmast where cm_custnumb = " + custnum); // Build the output string with the list of records read while (resultset.next()) resultvalue = resultvalue + getrecordtext(resultset) + "<br />"; catch (SQLException ex) { resultvalue = "Couldn't retrieve the custmast - "; resultvalue.concat(ex.tostring()); Logger.getLogger(Custmast.class.getName()).log(Level.SEVERE, null, ex); return resultvalue; return resultvalue; Retrieve all the Custmast records and build an string with this data @see SearchByCustNum / @WebMethod(operationName = "SelectAll") public String SelectAll() { String resultvalue = new String(); // Retrieve the list of custmast records from the database connection try { Statement statement = getstatement(); ResultSet resultset = statement.executequery("select from custmast"); // Build the output string with the list of records read while (resultset.next()) { resultvalue = resultvalue + getrecordtext(resultset) + "<br /> "; catch (SQLException ex) { resultvalue = "Couldn't retrieve the list of custmasts - "; resultvalue.concat(ex.tostring()); Logger.getLogger(Custmast.class.getName()).log(Level.SEVERE, null, ex); return resultvalue; w w w. f a i r c o m. c o m All Rights Reserved 11

Chapter 1 Creating a c-treeace Web Service for JEE return resultvalue; Creates an Statement object for the configured JDBC Resource in the application server / private Statement getstatement() throws SQLException { // Retrieve the JDBC Resource from the application server Context ctx; DataSource ds; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup("jdbc/ctreesql"); catch (NamingException ex) { Logger.getLogger(Custmast.class.getName()).log(Level.SEVERE, null, ex); return null; // Retrieve the SQL Connection from the resource and create a Statement Connection con = ds.getconnection(); return con.createstatement(); Builds a string with a Custmast record information / private String getrecordtext(resultset resultset) throws SQLException { String resultvalue = resultset.getstring(1).trim() + ", "; resultvalue = resultvalue + resultset.getstring(2).trim() + ", "; resultvalue = resultvalue + resultset.getstring(3).trim() + ", "; resultvalue = resultvalue + resultset.getstring(4).trim() + ", "; resultvalue = resultvalue + resultset.getstring(5).trim() + ", "; resultvalue = resultvalue + resultset.getstring(6).trim() + ", "; resultvalue = resultvalue + resultset.getstring(7).trim(); return resultvalue; Finish the Web Service The final step in creating the Web Service is to build and deploy it. We do that by right-clicking the Web Service project (WebService1) and selecting Deploy. F a i r C o m C o r p o r a t i o n 12 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 1.5 Create the Servlet In the JEE environment, we use a Servlet to connect the.jsp page to the Web Service. Think of a Servlet as a little like a CGI script or an.asp page that is executed when you click Submit. The Servlet calls the appropriate operation in the Web Service, either SearchByCustNum or SelectAll. 1. Right-click the Web Service project and select New > Web Service Client. 2. For Project, browse to the Custmast Web Service project. In the Package field type the name of the package (call it clientservlet) and check Generate Dispatch code. 3. Right-click the WebService1 project and select New > Servlet module. 4. Enter the Class Name (e.g., ReadCustomerServlet) and Package (e.g., clientservlet). 5. Accept the default values and click Finish. w w w. f a i r c o m. c o m All Rights Reserved 13

Chapter 1 Creating a c-treeace Web Service for JEE 6. Edit the Servlet module ReadCustomerServlet.java to implement the searchbycustnum Web Service call. The resultant source file will appear as shown below: ReadCustomerServlet.java package clientservlet; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.xml.ws.webserviceref; import javax.xml.transform.source; import javax.xml.ws.dispatch; @author FairCom Corporation / @WebServlet(name = "ReadCustomerServlet", urlpatterns = {"/ReadCustomerServlet") public class ReadCustomerServlet extends HttpServlet { @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Custmast/Custmast.wsdl") private Custmast_Service service; Processes requests for both HTTP <code>get</code> and <code>post</code> methods. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / protected void processrequest(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { String CustNumb = request.getparameter("custnumb"); String result = null; try { // Call Web Service Operation Dispatch<Source> sourcedispatch = null; result = service.getcustmastport().searchbycustnum(custnumb); catch (Exception ex) { // TODO handle custom exceptions here response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); try { out.println("<html>"); out.println("<head>"); out.println("<title>servlet ReadCustomerServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>c-tree Web Service Response:</h1>"); out.println("<h3>customer:</h3>" + result); out.println("<a href=\"javascript:window.history.go(-1)\">back</a>"); out.println("</body>"); out.println("</html>"); finally { out.close(); // <editor-fold defaultstate="collapsed" desc="httpservlet methods. Click on the + sign on the left to edit the code."> F a i r C o m C o r p o r a t i o n 14 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java Handles the HTTP <code>get</code> method. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / @Override protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { processrequest(request, response); Handles the HTTP <code>post</code> method. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / @Override protected void dopost(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { processrequest(request, response); Returns a short description of the servlet. @return a String containing servlet description / @Override public String getservletinfo() { return "Short description"; // </editor-fold> Create the second Servlet: 1. Repeat steps 3-5 to create a second Servlet called ReadCustomersServlet. w w w. f a i r c o m. c o m All Rights Reserved 15

Chapter 1 Creating a c-treeace Web Service for JEE 2. Edit the Servlet module ReadCustomersServlet.java to implement the selectall web service call. The resultant source file will appear as shown below: ReadCustomersServlet.java package clientservlet; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.xml.ws.webserviceref; import javax.xml.transform.source; import javax.xml.ws.dispatch; @author FairCom Corporation / @WebServlet(name = "ReadCustomersServlet", urlpatterns = {"/ReadCustomersServlet") public class ReadCustomersServlet extends HttpServlet { @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Custmast/Custmast.wsdl") private Custmast_Service service; Processes requests for both HTTP <code>get</code> and <code>post</code> methods. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / protected void processrequest(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { String result = null; try { // Call Web Service Operation Dispatch<Source> sourcedispatch = null; result = service.getcustmastport().selectall(); catch (Exception ex) { // TODO handle custom exceptions here response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); try { out.println("<html>"); out.println("<head>"); out.println("<title>servlet ReadCustomerServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>c-tree Web Service Response:</h1>"); out.println("<h3>customers:</h3>" + result); out.println("<a href=\"javascript:window.history.go(-1)\">back</a>"); out.println("</body>"); out.println("</html>"); finally { out.close(); F a i r C o m C o r p o r a t i o n 16 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java // <editor-fold defaultstate="collapsed" desc="httpservlet methods. Click on the + sign on the left to edit the code."> Handles the HTTP <code>get</code> method. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / @Override protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { processrequest(request, response); Handles the HTTP <code>post</code> method. @param request servlet request @param response servlet response @throws ServletException if a servlet-specific error occurs @throws IOException if an I/O error occurs / @Override protected void dopost(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { processrequest(request, response); Returns a short description of the servlet. @return a String containing servlet description / @Override public String getservletinfo() { return "Short description"; // </editor-fold> w w w. f a i r c o m. c o m All Rights Reserved 17

Chapter 1 Creating a c-treeace Web Service for JEE 1.6 Edit the Web Page Finally, we will need to edit the initial web page, index.jsp. We will create two Submit buttons labeled Read Customer: one will call SearchByCustNum; the other will call SelectAll. 1. Expand the Web Pages group. 2. Edit index.jsp so the <Body> of the HTML contains the following: index.jsp <body> <h1>c-tree Web Service Tutorial</h1> <p> <form name="test" method="post" action="readcustomerservlet"> <p>enter the customer ID you want to check: <input type="text" name="custnumb" size="4" ID="CustNumb"> <input type="submit" value="read Customer" name="readcustomerbutton"> </p> </form> <form name="test2" method="post" action="readcustomersservlet"> <p>or click here for checking all the customers: <input type="submit" value="read Customer" name="readcustomersbutton"> </p> </form> </body> F a i r C o m C o r p o r a t i o n 18 Copyright 2012 FairCom Corporation

Tutorial c-treeace Web Service Using Java 1.7 Try it out... We will test our Web Service using the web page we just made. The address will be: http://localhost:8080/webservice1 Enter a customer ID and click Read Customer to see information about a single customer. or Click the lower Read Customer button to show all customers. We should see the customer record(s) from the custmast table. In this example, the page shows all of the records: That s how easy it is to expand the power of your c-treeace database by making it available to other JEE applications as a Web Service. Stay tuned for more exciting tips and tricks from FairCom. w w w. f a i r c o m. c o m All Rights Reserved 19

Chapter 1 Creating a c-treeace Web Service for JEE 1.8 Additional Resources We encourage you to explore the additional resources listed here: Additional documentation and the other tutorials may be found at www.faircom.com (http://www.faircom.com). NetBeans can be downloaded from www.netbeans.org (http://netbeans.org/). GlassFish can be downloaded from glassfish.java.net (http://glassfish.java.net/). F a i r C o m C o r p o r a t i o n 20 Copyright 2012 FairCom Corporation