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

Use the Struts framework to build Web applications, Part 3

This tip focuses on the Action class within the Struts framework as a means to encapsulate business and connection logic.

This tip focuses on the use of the Action class within the Struts framework as a means to encapsulate both business...

and connection logic. Once we have developed an Action class we can simply plug it into our existing Web application. The advantage here is that the Struts framework maintains control over how the Action class is used, requiring only minimal XML configuration from us. Let's take a look at the Action class API that is delivered with Struts. For this tip, I have included the following code snippets to build an Action class. The first step is to understand the various components of our Action object -----CODE----- package myApp.action; import java.io.IOException; 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 MyAction extends Action { -----CODE----- In order to use the Action class API, we must first import the libraries into our Java class. In this case we have named our class 'MyAction' as indicated by the 'public final class' line. The second piece of our code snippet deals with actually implementing a higher level Java class, as indicated by the term 'extends'. Extending another Java class within our Java class allows our class to have all the methods and attributes available to the higher level class, without having to recreate those methods and supporting code manually. This is standard object oriented development within the Java world and it allows developers to use and resuse code easily. -----CODE-----

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

        String username = (String)request.getParameter("USERNAME");
        String password = (String)request.getParameter("PASSWORD");
-----CODE-----

We must now add at least one method to our Action class, as defined by the higher level Action class that we extended earlier. That is the 'perform' method and it's parameters and returns are defined within the Javadoc provided in the Struts documentation. The perform method is used exclusively by the Struts framework - we never have to write any code directly accessing this method! However, we do need to implement the method so that our Action class will have access to information coming from the Web. Let's take a look at the parameters expected by our perform method:

The ActionMapping parameters allow us to specify a return code based on the code execution within our Action class. If, for example, we are building a login Action class and the user login fails, we could return an Action mapping of 'failure'. The ActionForm object allows us to bring in form data from our Web page (text fields, password fields, etc.). The HttpServletRequest object is perhaps the most important parameter used when calling the 'perform' method. The Request object contains all the data entered into our Web page by the end user, as well as metadata provided by the Web browser.

The last two lines of this code snippet demonstrate the use of the Request object. In each of these lines, we have initialized a new String variable, then called the 'getParameter' method on our Request object. By passing a field key associated with the "name='USERNAME'" used in our Text Field Type HTML tag, we can retrieve all of the form data entered by the user. The Request object is essentially an instance of a session object that is unique to each Web user.

-----CODE-----
        Hashtable result = CallSAP.checkUser(username, password);

        // An exception is thrown by SAP if the return type is equal to "E"
        if ("E".equals((String)result.get("RETURN.TYPE"))) {
      return (mapping.findForward("error"));
  }

        session.setAttribute("USRENAME", username);

        return (mapping.findForward("success"));
    }
}
-----CODE-----

And now we are ready to make the call to SAP. In this tip, we are not looking specifically at connecting to SAP so we have not built any calls to SAP in our code. However, we have encapsulated all the logic to connect to our SAP BAPIs within another Java object called 'CallSAP'. In doing so, we create a logical separation between our Web application logic and the SAP connection logic. We will examine this 'CallSAP' object in an upcoming tip.

For now, we can assume that the CallSAP class has a method called 'checkUser' which requires two String parameters, username and password. We don't need to know how the method operates; all we need to know is that this method attempts to authenticate a user in SAP. We call this method by initializing a Hashtable that is populated by the return from the CallSAP.checkUser method.

Next we check to see if SAP has returned an error message by retrieving the 'RETURN.TYPE' key from our Hashtable result. If we get an 'E' in this field, we know SAP has returned an error, so we return an ActionMapping of 'error' back to the Struts framework. Otherwise, we assume the user was authenticated and set the 'USERNAME' value in our session object to the user's name. The session object allows us to persist data between calls to Action classes and is also used to display data to the user in a Web page. We can even use the session object to store and retrieve data from different Action classes, allowing us to maintain persistence between Web pages. An example of this would be storing item data into the session object as the user fills up a virtual shopping cart and retrieving that data from different Action classes.

Finally, we return an ActionMapping of 'success' back to the Struts framework. Through our configuration, Struts knows which Web page to forward the user to based on a mapping of 'success'.

So that wraps up the Action class. Next time we will focus on actually calling SAP using JCo, SAP's Java Connector.

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

Author Austin Sincock is product manager for ROBUSTA(tm), Gamma Enterprise Technologies Web sales solution for SAP.


This was last published in July 2002

Dig Deeper on SAP ABAP

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close