Sample Project (Service Creation-jsp)

In this chapter, Server Side Service used in XPLATFORM program is now created. When sending data received from form to the server, XPLATFORM program uses “Transaction” function. Applicable “Transaction” function transforms the received variables and dataset to XML Format to send calling JSP. The result value of calling JSP is confirmed at the callback function registered during calling through the factor of ErrorCode and ErrorMs.

Created service in Server Side can use language such as JSP, Java Servlet, ASP, ASP.NET, PHP and etc. In this sample, enter and received data from the form (Form) is saved by using JSP and approaching to Database (MySql). Created Service cannot send the enter value in forms of DataSet through web browser. Therefore, only through XPLATFORM form, it can be tested.

The example of server composition used in general.

Service creating language: Java series (Java Servlet, JSP), ASP series (ASP, ASP.NET)

Web server : web server included in Apache and WAS.

Database : Oracle, MS SQLServer, Sybase, DB2

WAS(Web Application Server) : WebLogic, WebSphere, JEUS, Tomcat, Resin

Database binding module should be provided in service creating language.
When creating service, general web editor (eclips, Visual Studio .NET, NotePad, …) is used but not USXtudio.
In this sample, it has been implemented to allow the execution in Tomcat using JSP.

Referral Service

<!-- 3.Proclaiming XPLATFORM XAPI(Library) -->
<!-- 2.Proclaiming Java Library -->
<!-- 1. Creating General JSP -->
<%
/** 4. Generating XPLATFORM General Object(PlatformData) **/
/** 7-1. ErrorCode, ErrorMsg Transaction **/
try {
/** 5-1. Database Connection **/
try {
/** 5-2. Creating SQL Perform Syntax **/
/** SELECT Transaction **/
/** 6. Creating Dataset and Saving Data in Dataset **/
/** 7-2. ErrorCode, ErrorMsg Transaction **/
} catch (SQLException e) {
/** 7-3. ErrorCode, ErrorMsg Transaction **/
}
/** 5-3. Database Close**/
} catch (Throwable th) {
/** 8-4. ErrorCode, ErrorMsg Transaction **/
}
/** 8-5. ErrorCode, ErrorMsg Transaction **/
/** 9. Creating XML print object (PlatformResponse) **/
%>

The Entire Code (search.jsp)

<!-- 3.Proclaiming XPLATFORM XAPI(Library) -->
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>
<!-- 2. Proclaiming Java Library -->
<%@ page import="java.sql.*"%>
<!-- 1. Greating General JSP -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%!
public String rsGet(ResultSet rs,String id)throws Exception{
if(rs.getString(id) == null)return "";
else return  rs.getString(id);
}
public boolean isEmpty(String str) {
if (str == null)return true;
if ("".equals(str.trim()))return true;
return false;
}
%>
<%
out.clearBuffer();

/** 4.XPLATFORM General Object (PlatformData) Creation**/
PlatformData pdata = new PlatformData();

/** 7-1. ErrorCode, ErrorMsg Proclaiming Part **/
int nErrorCode = 0;
String strErrorMsg = "START";
/** 5-1.Database Connection **/
try {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {
String url = "JDBC:mysql://localhost:3306/demo";
Class.forName("com.mysql.jdbc.Driver"); 
con = DriverManager.getConnection(url,"demo","1234");
stmt = con.createStatement();

/** 5-2.SQL Creating Perform Syntax **/
String sql = " SELECT * FROM customer " ;

if (!isEmpty(request.getParameter("keyword"))){
sql += " WHERE LCASE(name) like LCASE('%" + request.getParameter("keyword")+ "%') " ;
}
rs = stmt.executeSearch(sql);

/** 6. Creating Dataset and Saving Data in Dataset**/
DataSet ds = new DataSet("customers");
ds.addColumn("id",DataTypes.INT);
ds.addColumn("name",DataTypes.STRING, 16);
ds.addColumn("email", DataTypes.STRING, 32);
ds.addColumn("birthday", DataTypes.STRING, 8);
ds.addColumn("phone", DataTypes.STRING, 16);
ds.addColumn("home_addr", DataTypes.STRING, 256);
ds.addColumn("company", DataTypes.STRING, 32);
ds.addColumn("jobtitle", DataTypes.STRING, 32);
ds.addColumn("busi_phone", DataTypes.STRING, 16);
ds.addColumn("busi_addr", DataTypes.STRING, 256);

int row = 0;
while(rs.next()){
row = ds.newRow();
ds.set(row,"id",rs.getInt("id"));
ds.set(row,"name",rsGet(rs,"name"));
ds.set(row,"email",rsGet(rs,"email"));
ds.set(row,"birthday",rsGet(rs,"birthday"));
ds.set(row,"phone",rsGet(rs,"phone"));
ds.set(row,"home_addr",rsGet(rs,"home_addr"));
ds.set(row,"company",rsGet(rs,"company"));
ds.set(row,"jobtitle",rsGet(rs,"jobtitle"));
ds.set(row,"busi_phone",rsGet(rs,"busi_phone"));
ds.set(row,"busi_addr",rsGet(rs,"busi_addr"));
}
pdata.addDataSet(ds);

/** 7-2. ErrorCode, ErrorMsg Transaction **/
nErrorCode = 0;
strErrorMsg = "SUCC";

} catch (SQLException e) {
/** 7-3. ErrorCode, ErrorMsg Transaction **/
nErrorCode = -1;
strErrorMsg = e.getMessage();
}
/** 5-3. Database Close**/
if(rs != null){try{rs.close();} catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(stmt != null){try{stmt.close();}catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(con != null){try{con.close();} catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
} catch (Throwable th) {
/** 7-4. ErrorCode, ErrorMsg Transaction **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 7-5. ErrorCode, ErrorMsg Transaction **/
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 8. Creating XML print Object (PlatformResponse) **/
HttpPlatformResponse res = new HttpPlatformResponse(response);
res.setData(pdata);
res.sendData();
%>

Code Analysis

It is the beginning of the code.

<!-- 1. Creating General JSP -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%!
public String rsGet(ResultSet rs,String id)throws Exception{
if(rs.getString(id) == null)return "";
else return rs.getString(id);
}
public boolean isEmpty(String str) {
if (str == null)return true;
if ("".equals(str.trim()))return true;
return false;
}
%>
<%
/** Parts being implemented for data transaction **/
%>

Proclaiming Java Library

In order to create JSP service, proclaim general library of java.
Libraries proclaimed for general object of Java and log management of web server are as follows:
<!-- 2. Proclaiming Java Library -->
<%@ page import = "java.sql.*" %>
<%@ page import="org.apache.commons.logging.*" %>

Proclaiming XPLATFORM XAPI(Library)

When creating XML Data, it is necessary to follow the format used by XPLATFORM..
XML Format Data can use commercialized Parser like “xerces”, or implement directly.
However, if using library (XPLATFORM XAPI) for XPLATFORM Java, due to creating dedicated object and using it, it is easy to extract data using object functions without parsing XML Data Format separately.

The screipt proclaiming XPLATFORM XAPI is as follow:

<!-- 3.Proclaiming XPLATFORM XAPI(Library) -->
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>

Creating XPLATFORM Basic Object (PlatformData)

Proclaim basic object for transacting data.
After parsing XML data XPLATFORM received by program, it is stored in forms of Dataset in PlatformData.
XML Data can multiple Dataset at a time.
The result obtained by performance of SQL sentences from database is stored in Dataset.
Dataset can create multiples with proclaiming and create multiple dataset and receive them through just one communication.
Variables used as factors or return values are stored at VariableList.
Dataset and VariableList created from the procedure are included in PlatformData objects.
Therefore, to use variables(VariableList) or Dataset, proclaimation of PlatformData must be required.

The script proclaiming PlatformData is as follow:

out.clearBuffer();

/** 4.Creating XPLATFORM Basic Object(PlatformData) **/
PlatformData pdata = new PlatformData();

Binding Database & Creating SQL Perform Syntax

/** 5-1.Database Connection **/
/******* JDBC Connection *******/
try {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
String url = "JDBC:mysql://localhost:3306/demo";
Class.forName("com.mysql.jdbc.Driver"); 
conn = DriverManager.getConnection(url,"demo","1234");
stmt = conn.createStatement();

/** 5-2.Creating SQL Perform Syntaxd **/
String sql = " SELECT * FROM customer " ;

// If there is a condition for referring. 
if (!isEmpty(request.getParameter("keyword"))){
sql += " WHERE LCASE(name) like LCASE('%" + request.getParameter("keyword")+ "%') " ;
}
rs = stmt.executeSearch(sql);

} catch (SQLException e) {

}
/** 5-3. Database Close**/
/******** JDBC Close ********/
if(rs != null){try{rs.close();} 
catch(Exception e){nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(stmt != null){try{stmt.close();}
catch(Exception e){nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(con != null){try{con.close();} 
catch(Exception e){nErrorCode = -1; strErrorMsg = e.getMessage();}}
}catch (Throwable th) {}

Creating Dataset & Saving Searched Data in Dataset.

Even though data in “rs” which is ResultSet directly delivers to XPLATFORM, it is not recognized as Data. It should be transformed into XML Format that XPLATFORM approves.
Since its process might be slightly inconvenient, it is recommended to use DataSet of dedicated object that XPLATFORM library provides. DataSet included in it parent, PlatformData can be easily transformed to XML Format Data by using PlatformData features.
In this sample, the process that the value of ResultSet delivers to Dataset per each unit of Column in order to demonstrates basic features. However, for employing the real project, it is possible to reduce to create unnecessary script if applying this part made into common function.

The script for transferring data of ResultSet(“rs”) to DataSet is as follows:.

/** 6. Dataset Saving created and searched Data into Dataset **/


/********* Dataset Creation **********/
DataSet ds = new DataSet("customers");
ds.addColumn("id",DataTypes.INT);
ds.addColumn("name",DataTypes.STRING, 16);
ds.addColumn("email", DataTypes.STRING, 32);
ds.addColumn("birthday", DataTypes.STRING, 8);
ds.addColumn("phone", DataTypes.STRING, 16);
ds.addColumn("home_addr", DataTypes.STRING, 32);
ds.addColumn("company", DataTypes.STRING, 32);
ds.addColumn("jobtitle", DataTypes.STRING, 32);
ds.addColumn("busi_phone", DataTypes.STRING, 16);
ds.addColumn("busi_addr", DataTypes.STRING, 256);


int row = 0;
while(rs.next()){
row = ds.newRow();
ds.set(row,"id",rs.getInt("id"));
ds.set(row,"name",rsGet(rs,"name"));
ds.set(row,"email",rsGet(rs,"email"));
ds.set(row,"birthday",rsGet(rs,"birthday"));
ds.set(row,"phone",rsGet(rs,"phone"));
ds.set(row,"home_addr",rsGet(rs,"home_addr"));
ds.set(row,"company",rsGet(rs,"company"));
ds.set(row,"jobtitle",rsGet(rs,"jobtitle"));
ds.set(row,"busi_phone",rsGet(rs,"busi_phone"));
ds.set(row,"busi_addr",rsGet(rs,"busi_addr"));
}
// Add DataSet to PlatformData
pdata.addDataSet(ds);

Transacting Errorcode and ErrorMsg

With the process above, basic data transaction has been implemented.
However, there are exceptions very likely. When errors occur, it is preferable to transact the parts with script.
/** 7-1. Proclaiming parts of ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";
/** 7-2. Transacting ErrorCode and ErrorMsg **/
// If transaction is successful
nErrorCode = 0;
strErrorMsg = "SUCC";
/** 7-3. Transacting ErrorCode and ErrorMsg **/
// If transaction is failed
nErrorCode = -1;
strErrorMsg = e.getMessage();
/** 7-4. Transacting ErrorCode and ErrorMsg **/
// If transaction is failed
nErrorCode = -1;
strErrorMsg = th.getMessage();
/** 7-5. Trnasacting ErrorCode and ErrorMsg **/
//Referring to VariableList 
VariableList varList = pdata.getVariableList();
// Adding the value to VariableList directly
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

Creating XML print Object (PlatformResponse)

Through previous process, data is included in Dataset by extracting from Database.
The error message is also registered in VariableList. Since DataSet and VariableList are the member of PlatformData, the result value of JSP perform is included in PlatformData object. Now, parts to send in data of PlatformData will be implemented by extracting with ML Format that XPLATFORM approves.

For easily implementing features which send data, the scripts which creates PlatformResponse of XPLATFORM library and prints data from PlatformData are as follows:

/** 8. Creating XML print object(PlatformResponse) **/
// Generating HttpPlatformResponse using HttpServletResponse
HttpPlatformResponse res = new HttpPlatformResponse(response);
res.setData(pdata);
// Sending Data
res.sendData();

Service of Saving

The Entire Code (save.jsp)

<!-- 3.Proclaiming XPLATFORM XAPI(Library) -->
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>
<!-- 2. Proclaiming Java Library -->
<%@ page import="java.sql.*"%>
<!-- 1. Creating basic JSP -->
<%@ page contentType="text/xml; charset=UTF-8" %>

<%!
public String nvl(String id)throws Exception{
if(id == null )return "";
else return id;
}
public boolean isEmpty(String str) {
if (str == null)return true;
if ("".equals(str.trim()))return true;
return false;
}
%>

<%
out.clearBuffer();

/** Creating PlatformRequest & Reading Data **/
/** Generating HttpPlatformRequest using HttpServletRequest */
HttpPlatformRequest req = new HttpPlatformRequest(request);

/** XML Data Analysis */
req.receiveData();

/** Saving data in forms of PlatformData */
PlatformData i_xpData = req.getData();

/** Extracting Dataset from PlatformData */
DataSet inDs = i_xpData.getDataSet("customers");

/** Creating XPLATFORM basic object(PlatformData) **/
PlatformData pdata = new PlatformData();

/** Proclaiming part of ErrorCode and ErrorMsg **/
int nErrorCode = 0;
String strErrorMsg = "START";
/** Database Connection **/
try {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {
String url = "JDBC:mysql://localhost:3306/demo";
Class.forName("com.mysql.jdbc.Driver"); 
con = DriverManager.getConnection(url,"demo","1234");
stmt = con.createStatement();

/** Creating SQL Perform Systax **/
String sql = "";
/** Managing Transaction */
con.setAutoCommit(false);
int rowType=0;
int i=0;
/** Transact Dataset with INSERT, UPDATE **/
for ( i=0 ; i <inDs.getRowCount() ;i++){
rowType = inDs.getRowType(i);
if( rowType == DataSet.ROW_TYPE_INSERTED ){
sql = " INSERT INTO customer " 
+ " (name, email, birthday, phone, home_addr"
+ " , company, jobtitle, busi_phone, busi_addr ) "
+ " VALUES ( ' " 
+ nvl(inDs.getString(i,"name")) + "' , '"
+ nvl(inDs.getString(i,"email")) + "' , '"
+ nvl(inDs.getString(i,"birthday")) + "' , '"
+ nvl(inDs.getString(i,"phone")) + "' , '"
+ nvl(inDs.getString(i,"home_addr")) + "' , '"
+ nvl(inDs.getString(i,"company")) + "' , '"
+ nvl(inDs.getString(i,"jobtitle")) + "' , '"
+ nvl(inDs.getString(i,"busi_phone")) + "' , '"
+ nvl(inDs.getString(i,"busi_addr")) + "' ) " ;
stmt.executeUpdate(sql);
} else if( rowType == DataSet.ROW_TYPE_UPDATED ){
sql = " UPDATE customer SET " 
+ " name = ' " + nvl(inDs.getString(i,"name")) + "' , "
+ " email = ' " + nvl(inDs.getString(i,"email")) + "' , "
+ " birthday = ' " + nvl(inDs.getString(i,"birthday")) + "' , " 
+ " phone = ' " + nvl(inDs.getString(i,"phone")) + "' , "
+ " home_addr = ' " + nvl(inDs.getString(i,"home_addr")) + "' , "
+ " company = ' " + nvl(inDs.getString(i,"company")) + "' , "
+ " jobtitle = ' " + nvl(inDs.getString(i,"jobtitle")) + "' , "
+ " busi_phone = ' " + nvl(inDs.getString(i,"busi_phone")) + "' , " 
+ " busi_addr = ' " + nvl(inDs.getString(i,"busi_addr")) + "' "
+ " WHERE id = " + inDs.getInt(i,"id") ;
stmt.executeUpdate(sql);
}
}

/** Transacting Dataset with DELETE **/
for( i = 0 ; i< inDs.getRemovedRowCount() ; i++ ){
sql = " DELETE FROM customer WHERE id = " + Integer.parseInt(inDs.getRemovedData(i,"id").toString()) ;
stmt.executeUpdate(sql);
}
/** Managing Transaction */
con.commit();

/** 7-2. Transacting ErrorCode and ErrorMsg **/
nErrorCode = 1;
strErrorMsg = "SUCC";
} catch (SQLException e) {
/** 7-3. Transacting ErrorCode and ErrorMsg **/
nErrorCode = -1;
strErrorMsg = e.getMessage();
}
/** 5-3. Database Close**/
if(rs != null){try{rs.close();} catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(stmt != null){try{stmt.close();}catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
if(con != null){try{con.close();} catch(Exception e){
nErrorCode = -1; strErrorMsg = e.getMessage();}}
} catch (Throwable th) {
/** 7-4. Transacting ErrorCode and ErrorMsg **/
nErrorCode = -1;
strErrorMsg = th.getMessage();
}
/** 7-5. Transacting ErrorCode and ErrorMsg **/
VariableList varList = pdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 8. Creating XML print object(PlatformResponse) **/
HttpPlatformResponse res = new HttpPlatformResponse(response);
res.setData(pdata);
res.sendData();
%>

Analyzing Code

It is a beginning part of code.

<!-- 1. Creating basic JSP -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%!
public String nvl(String id)throws Exception{
if(id == null )return "";
else return id;
}
public boolean isEmpty(String str) {
if (str == null)return true;
if ("".equals(str.trim()))return true;
return false;
}%>
<%
/** The part to be implemented for transacting data **/
%>

Proclaiming Java Library

It is the same as the contents of “Proclaiming Java Library”.

Proclaiming XPLATFORM XAPI (Library)

It is the same as the contents of “Proclaiming XPLATFORM XAPI(Library)”.

Creating XPLATFORM Basic Object (PlatformData)

It is the same as the contents of “Creating XPLATFORM Basic Object (PlatformData)“.

Creating XML enter object (PlatformRequest) and Reading Data

Data which was delivered by XPLATFORM program is XML Format.
This can be used directly through direct parsing.
However, if using XPLATFORM library, it can be implemented easily.

The script creating PlatformRequest object is as followss.

/** 5.Creating PlatformRequest & Reading Data **/
// Creating HttpPlatformRequest using HttpServletRequest. 
HttpPlatformRequest req = new HttpPlatformRequest(request);
// XML Data Analysis
req.receiveData();
// Save data in forms of PlatformData. 
PlatformData i_xpData = req.getData();
// Extract Dataset from PlatformData.
DataSet inDs = i_xpData.getDataSet("customers");
%>

Database Connection & Creating SQL Perform Syntax

/** 6-1.Database Connection **/
try {
/******* JDBC Connection *******/
Connection conn = null;
Statement stmt = null;
try{
String url = "JDBC:mysql://localhost:3306/demo";
Class.forName("com.mysql.jdbc.Driver"); 
con = DriverManager.getConnection(url,"demo","1234");
stmt = con.createStatement();
} catch (SQLException e) {

}
/******** JDBC Close ********/
if ( stmt != null ) try { stmt.close(); } catch (Exception e) {
nErrorCode = -1; strErrorMsg = e.getMessage();}
if ( con != null ) try { con.close(); } catch (Exception e) {
nErrorCode = -1; strErrorMsg = e.getMessage();}
}catch (Throwable th) {

}
/** 6-2. Creating SQL Perform Systax **/
String sql = "";
// Managing Transaction
con.setAutoCommit(false);
int rowType=0;
int i=0;
/** INSERT and UPDATE Dataset **/
for ( i=0 ; i <inDs.getRowCount() ;i++){
rowType = inDs.getRowType(i);
if( rowType == DataSet.ROW_TYPE_INSERTED ){
sql = " INSERT INTO customer " 
+ " (name, email, birthday, phone, home_addr"
+ " , company, jobtitle, busi_phone, busi_addr ) "
+ " VALUES ( ' " 
+ nvl(inDs.getString(i,"name")) + " ' , ' "
+ nvl(inDs.getString(i,"email")) + " ' , ' "
+ nvl(inDs.getString(i,"birthday")) + " ' , ' "
+ nvl(inDs.getString(i,"phone")) + " ' , ' "
+ nvl(inDs.getString(i,"home_addr")) + " ' , ' "
+ nvl(inDs.getString(i,"company")) + " ' , ' "
+ nvl(inDs.getString(i,"jobtitle")) + " ' , ' "
+ nvl(inDs.getString(i,"busi_phone")) + " ' , ' "
+ nvl(inDs.getString(i,"busi_addr")) + " ' ) " ;
stmt.executeUpdate(sql);
}else if( rowType == DataSet.ROW_TYPE_UPDATED ){
sql = " UPDATE customer SET " 
+ " name = ' " + nvl(inDs.getString(i,"name")) + " ' , "
+ " email = ' " + nvl(inDs.getString(i,"email"))+ " ' , "
+ " birthday = ' " + nvl(inDs.getString(i,"birthday"))  + " ' , " 
+ " phone = ' " + nvl(inDs.getString(i,"phone")) + " ' , "
+ " home_addr = ' " + nvl(inDs.getString(i,"home_addr")) + " ' , "
+ " company = ' " + nvl(inDs.getString(i,"company")) + " ' , "
+ " jobtitle = ' " + nvl(inDs.getString(i,"jobtitle")) + " ' , "
+ " busi_phone = ' " + nvl(inDs.getString(i,"busi_phone")) + " ' , " 
+ " busi_addr = ' " + nvl(inDs.getString(i,"busi_addr")) + " ' "
+ " WHERE  id  = " + inDs.getInt(i,"id") ;
stmt.executeUpdate(sql);
}
}
/** Deleting Dataset **/
for( i = 0 ; i< inDs.getRemovedRowCount() ; i++ ){
sql = " DELETE FROM customer WHERE id = " + 
Integer.parseInt(inDs.getRemovedData(i,"id").toString()) ;
stmt.executeUpdate(sql);
}
// Managing Transaction
con.commit();

Transacting Errorcode, ErrorMsg

It is the same as "Transacting Errorcode and ErrorMsg".

Creating XML print Object (PlatformResponse)

It is the same as "Creating XML print Object (PlatformResponse)".