03 SAP Resource Adapter SOL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SAP Java Resource Adapter, Solutions

Unit: Remote call using SAP Resource Adapter


Topic: Java Connector

At the conclusion of this exercise, you will be able to:


• How to use the Common Client Interface to make a remote function
call.

1. Development Objectives
Creating a J2EE Project and using the Common Client Interface API to make a
remote function call. The J2EE Project will contain an EAR application and a
WAR application. The Web Application will have a Servlet which will make use
of the Common Client Interface API Classes to get a list of Company Codes from
SAP R/3 function module BAPI_COMPANYCODE_GETLIST.

2. Result
As a result of the exercise you will be able to display a list of Company Codes and
Company Names.

3-1. Prerequisites
3-1 You have installed the SAP Resource Adapter file on the Web Application
Server.
3-2 You have launched the Netweaver Developer Studio.
3-3 You have selected the J2EE perspective.

3-2. Overview Developing

For your convenience, you can start


developing with a predefined
Project. The graphics on the left
shows the J2EE Enterprise
Application Project (EAR) and Web
Application Project (WAR)
imported into the Developer Studio
Workplace.

SAPJRAExample
The EAR Project which
contains the web project.
SAPJRAExampleWeb
The WAR Project which
contains the Servlet which uses the
CCI API.
TestServlet
The Servlet used for the
remote function call.

4. Complete the Servlet code


4-1 Setup Project Build Path
Right Click on SAPJRAExampleWeb -> Properties ->Java Build Path-
>Libraries ->Add External Jars. Add connector.jar to the project build
path.
4-2 Obtain the JNDI Context
Context ctx = null;
try
{
//Step 1. Obtain the initial JNDI context
ctx = new InitialContext();
}
catch (Exception e){
out.println("<p>Can't get context."+e.toString()+"</p>");
out.println("</body></html>");
return;
}
4-3 Lookup a Connection Factory
// Step 2. Look up for ConnectinFactory
ConnectionFactory connectionfactory = null;
try
{
connectionfactory =(ConnectionFactory)
ctx.lookup("java:comp/env/eis/SAPJRAFactory");

}
catch (Exception ex){
out.println("Can't look up SAPJRAFactory.");
out.println(ex);
}
4-4 Get a Connection
// Step 3. Get Connection
try
{
connection = connectionfactory.getConnection();
// Print the System details.
out.println("Connection to "+
connection.getMetaData().getEISProductName()+
", "+ connection.getMetaData().getEISProductVersion() +
" with user \""+connection.getMetaData().getUserName() +
"\" created <br/><br/>");
}
catch (Exception ex){
out.println("Can't create Connection .");
}
4-5 Get a Record Factory and an Interaction
// Step 4. Create a record factory
RecordFactory recordfactory =
connectionfactory.getRecordFactory();

// Step 5. Create Interaction


Interaction interaction = connection.createInteraction();
4-6 Make the Remote Function Call

//Step 6.Call the R/3 function module BAPI_COMPANYCODE_GETLIST

MappedRecord input = connectionfactory.getRecordFactory().


createMappedRecord("BAPI_COMPANYCODE_GETLIST");

MappedRecord output=(MappedRecord)interaction.execute
(null,input);
ResultSet list = (ResultSet) output.get("COMPANYCODE_LIST");
4-7 Display the List in Tabular Format

//Step 7. Display the list in tabular format


out.println ( "<table border=1><tr><td><b>Company
Code</b></td><td><b>Company Name<b></td></tr>");

while( list.next())
out.println("<tr><td>" + list.getObject(1).toString()
+"</td><td>"+list.getObject(2).toString()+
"</td></tr>");
out.println ( "</table>");
out.println ( "</table>");
4-8 Release the Connection
// Step 8. Release interaction
interaction.close();

5. Deploying the Application


5-1 Right Click on the SAPJRAExampleWeb Project and “Choose Build Web
Archive”.
5-2 Right Click on the SAPJRAExample Project and “Choose Build Application
Archive”.
5-3 Right Click on the SAPJRAExample.ear file and “Choose Deploy to J2EE
Engine”

6. Testing the Application


Open Web Browser and Navigate to this URL http://localhost:<http-
port>/SAPJRATest/SAPJRATestServlet

You might also like