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

Use the Struts framework to build Web applications, part 5

This is the fifth of a continuing Struts tip series.

This is the fifth of a continuing Struts tip series. In this tip, we will develop a Struts Action class to call the JCo object developed in the previous tip. This Action class will then be integrated into the Struts framework, as we prepare for the front-end interface.

The following code represents a very basic Action class. Its essentially contains three steps:
1) Retrieve data from the user's Web session.
2) Call the getMaterials() method on the CallSap class using that data.
3) Post any results from this call back to the user's session object. Copy the following code and save it as ''. If you have set up the development environment with Struts as specified in a previous tip, this code will compile to MaterialAction.class. Make sure that the CallSap class created in the previous tip is in the same directory as this new class

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

public final class MaterialAction extends Action {
    public ActionForward perform(ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws IOException, ServletException
  // Extract attributes we will need
     HttpSession session = request.getSession();

        String searchString= (String)request.getParameter("SEARCHSTRING");

     // Make the call to the command class to check the userid/password
     CallSap sapCaller = new CallSap();
        Hashtable result = sapCaller.getMaterials(searchString);

        // An exception is thrown by SAP if the return type is equal to "E"
        if (result == null) {
      return (mapping.findForward("error"));
        session.setAttribute("MATERIALLIST", result);

        return (mapping.findForward("success"));
Let's quickly walk through this code for a better understanding of the Action class. After the import statements, notice that the class 'MaterialAction' extends the 'Action' class. This class is part of the Struts JAR library which you installed several tips back. The Action class forms the basis for this application's integration into the Struts framework.

The Action class has several different methods that can be used, but the most essential is the perform() method. Any logic encapsulated in this method will be executed when the class is called by the Struts framework. The perform() method returns an ActionForward object to Struts in order to indicate the status of the call to this Action class (success, failure, etc.). Likewise, the perform() method requires a number of different parameters. These parameters allow the Action class to interact with data sent through the Web page.

The HttpSession object stores the data sent from the user via Web form. The MaterialAction class retrieves the "SEARCHSTRING" parameter from the session object. Next, the class creates a new instance of the CallSap object developed in the last tip. Using the getMaterials() method, the MaterialAction class sends the sapCaller instance the material search string entered by the end user. The result is a Hashtable object that is stored in the HttpSession object, session. The Hashtable is saved in the session with a text attribute of "MATERIALLIST". This attribute will be used in the Web page to retrieve the search results.

If the call to sapCaller.getMaterials() does not return a result, the class returns an ActionForward set to "error". If the call does contain results, an ActionForward set to "success" is returned. These ActionForward objects tell Struts what Web page to send back to the end user. In this way, the Action class dictates the flow of business logic and not the specific screen flow.

Next time, we will build a request and response JSP to take and display material search results to the end user. We will also configure Struts to handle the MaterialAction class.

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.