Problem solve Get help with specific problems with your technologies, process and projects.

Use the Struts framework to build Web applications, Part 4

Austin Sincock continues his discussion about Web Applications.

This tip demonstrates a very simple technique for encapsulating the JCo libraries to call SAP. Rather than going into great detail on how to use the JCo libraries, here are two Java classes that encapsulate logic to retrieve a list of materials from SAP. Because the purpose of this tip series is to learn Struts and not JCo, I present these as a quick way to SAP functionality.

Cut and paste the following code into a text editor and save the file as ''. As long as you have the JCo libraries in your CLASSPATH, you can compile this class. If not, see the installation documentation that comes with JCo.

import java.util.Hashtable;
public class CallSap {
    String SAP_CLIENT = "100";
    String USER_ID = "username";
    String PASSWORD = "password";
    String LANGUAGE = "E";
    String HOST_NAME = "servername";
    String SYSTEM_NUMBER = "00";
    JCO.Client aConnection;
    IRepository aRepository;

    public CallSap() {
      try {
        aConnection = JCO.createClient(SAP_CLIENT,
        aRepository = new JCO.Repository("SAPRep", aConnection);
      catch (Exception ex) {
        System.out.println("Call to SAP has failed.");

    public Hashtable getMaterials(String materialSearch) {
      IFunctionTemplate functionTemplate = aRepository.getFunctionTemplate("BAPI_MATERIAL_GETLIST");
      JCO.Function function = new JCO.Function(functionTemplate);
      JCO.ParameterList tabParams = function.getTableParameterList();
      JCO.Table materials = tabParams.getTable("MATNRSELECTION");

      materials.setValue("I", "SIGN");
      materials.setValue("CP", "OPTION");
      materials.setValue(materialSearch, "MATNR_LOW");

      JCO.ParameterList resultParams = function.getExportParameterList();
      JCO.Table materialList = function.getTableParameterList().getTable("MATNRLIST");

        Hashtable returnHash = new Hashtable();
        Hashtable rowHash = new Hashtable();
        int i = 0;
        if (materialList.getNumRows() > 0) {
            do {
                for (JCO.FieldIterator fI = materialList.fields();
                    JCO.Field tabField = fI.nextField();
                    returnHash.put("line" + i, rowHash);
                    rowHash = new Hashtable();
                while(materialList.nextRow() == true);
        else {
              System.out.println("Sorry, couldn't find any materials");
        return returnHash;
Cut and paste the following code into a text editor and save it as '' in the same directory that the '' was saved. Compile and execute TestCall from the command line to make sure that the CallSap class is functioning correctly. Bear in mind that the following code will use the CallSap class to retrieve a list of ALL the materials within your R/3 system. If you need to limit this search, change the 'materialSearch' variable to reflect a more specific search pattern.
  import java.util.Hashtable;
  import java.util.Enumeration;
  public class TestCall {

  public static void main(String[] args) {
  //Change this variable to narrow search criteria
    String materialSearch = "*";
    CallSap callSap = new CallSap();
    Hashtable resultHash = callSap.getMaterials(materialSearch);
    Hashtable tempRow;
    for (Enumeration e = resultHash.elements(); e.hasMoreElements();) {
      tempRow = (Hashtable)e.nextElement();
      System.out.println("Material: " +
          (String)tempRow.get("MATERIAL") + "n");
      System.out.println("Material description: " +
          (String)tempRow.get("MATL_DESC") + "nn");
Ultimately, you will use the CallSap class to connect to SAP through a Struts Action class. This class delivers a discrete method to call the BAPI_MATERIAL_GETLIST as well as providing the JCo connection logic. All of this without actually having to use the JCo libraries from the Action class. This means that your JCo development is finite and you can encapsulate all of your SAP connection logic within one or two Java classes. These classes can then be passed along to Java developers for integration into an overall Web application framework.

Check out Austin's site for more insight into building applications for the Enterprise.

Author Austin Sincock is a freelance Java/SAP consultant who contributes regularly to Web and print journals. He can be reached at

Dig Deeper on SAP integration

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.