Posted at

SAP RFC from JSP via JCO

More than 1 year has passed since last update.

This article shows a simple JSP program calling SAP Remote function module via JCo(SAP Java Connector).

How to create JCO and JDK environment is on another article(Japanese).

How to create Eclipse and Jetty environment is on another article(Japanese).


Development Environment


  • IDE: Eclipse(Oxygen.2 Release (4.7.2))

  • JDK: Oracle JDK 1.8.0_152(not SAP JDK)

  • JCo: SAP Java Connector 3.0.17

  • Web Server: Jetty(Eclipse plugin "Eclipse Jetty3.9.0" and "Run-Jetty-Run 1.3.5-nightly")


JSP code

Very simple program calling function module "STFC_CONNECTION".

<HTML>

<BODY>
<%@ page language="java" contentType="text/html; charset=Windows-31J"%>
<%@ page
import="java.io.File,
java.io.FileOutputStream,
java.util.Properties,
com.sap.conn.jco.JCoAttributes,
com.sap.conn.jco.AbapException,
com.sap.conn.jco.JCoDestination,
com.sap.conn.jco.JCoDestinationManager,
com.sap.conn.jco.JCoException,
com.sap.conn.jco.ext.DestinationDataProvider,
com.sap.conn.jco.JCoFunction"
%>
<%!
// ABAP Application Server
static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";

// Destination Properties
static {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "XXXX"); //host name or ip address
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "400");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "fukuhara");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXX"); //password
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
createDataFile(ABAP_AS, "jcoDestination", connectProperties);
}

//Create SAP Destination properties
static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
if (!cfg.exists()) {
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
}

%>
<%
//Get Destination
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);

//Get metadata of function module "STFC_CONNECTION"
JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
if (function == null)
throw new RuntimeException("TFC_CONNECTION not found in SAP.");

//Set Import parameter of the function module
function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");
try {

//Call Function
function.execute(destination);
} catch (AbapException e) {
System.out.println(e.toString());
return;
}
%>

<%-- Get result of the function module--%>
<H3><%=function.getExportParameterList().getString("ECHOTEXT")%></H3>
<H3><%=function.getExportParameterList().getString("RESPTEXT")%></H3>

</BODY>
</HTML>

This is the result of JSP screen. The screen displays ABAP system information.

10.Result01.JPG