Create a task command

In this lesson, you learn how to create a task command interface and its associated implementation class. The task command handles the view parameters and has fields for defaultCommandClassName, URL parameters, and the current value of bonus points. The command includes a method for getting the current value of bonus points.

About this task

A controller command represents a business process or complex function. For example, all of the business logic that is related to processing orders is encapsulated in the OrderProcessCmd controller command. A business process can often be divided up into smaller, more specific tasks. For example, the OrderProcessCmd controller command consists of several task commands that each perform individual units of work. In this lesson, you create the task command for MyNewControllerCmdImpl.

After completing this lesson, you should be able to perform the following tasks:

  • Create a task command interface and its associated implementation class
  • Add fields and methods to the task command

Creating MyNewTaskCmd

Creating a task command involves creating an interface and an implementation class. When you are creating a task command interface, extend For the implementation class, extend


  1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src >
  2. Open the file for editing
  3. Locate and uncomment Section 1 to introduce the code to create a field that specifies the default implementation class to be used by the interface:
    /// Section 1 ///////////////////////////////////////////////
    // set default command implement class
      static final String defaultCommandClassName=
    /// End of section 1//////////////////////////////////////////
    Note: The default implementation is specified in the code since the same implementation class is used for the entire site. No default properties are passed to the command. If you have a command that has multiple implementations, or has default properties that are stored in the CMDREG table, you must register the command in the CMDREG table. Registering the command creates the mapping between the interface and implementation class.
  4. Locate and uncomment Section 2 to create the getter and setter methods that are used in the MyNewTaskCmdImpl implementation class.
    These methods are for fields corresponding to the following types of information:
    • A customer's user ID
    • A value of bonus points
    • A greeting message
    /// Section 2 ////////////////////////////////////////////////
    // set interface methods
      public void setInputUserName(java.lang.String inputUserName);
      public void setInputPoints(Integer inputPoints);
      public void setGreetings(java.lang.String greeting);
      public java.lang.String getInputUserName();
      public java.lang.Integer getInputPoints();
      public java.lang.String getGreetings();
    /// End of section 2//////////////////////////////////////////
  5. Save your changes.
  6. Open the file for editing
  7. Locate and uncomment Sections 1A and 1B to create fields and their corresponding getter and setter methods in the implementation class.
    //// Section 1A //////////////////////////////////////////////
      private java.lang.String inputUserName;
      private java.lang.String greetings; 
      private java.lang.Integer inputPoints;
    ////End of Section 1A /////////////////////////////////////////
    //// Section 1B //////////////////////////////////////////////  
      public void setInputUserName(java.lang.String newInputUserName) {
        inputUserName = newInputUserName;
      public void setInputPoints(Integer newInputPoints) {
        inputPoints = newInputPoints;
      public void setGreetings(java.lang.String newGreetings) {
        greetings = newGreetings;
      public java.lang.String getInputUserName() {
        return inputUserName;
      public Integer getInputPoints() {
        return inputPoints;
      public java.lang.String getGreetings() {
        return greetings;
    ////End of Section 1B /////////////////////////////////////////
  8. Save your changes.
  9. In the Outline view of, select the performExecute method.
  10. Locate and uncomment Section 1 to introduce the code to update the greetings value and make the value available in the name-value pair (NVP) list.
    The greetings value is then made available then to other objects by using the getGreetings() method.
    /// Section 1 ////////////////////////////////////////////////
    /// modify the greetings and see it in the NVP list
      setGreetings( "Hello " + getInputUserName() );
    /// End of section 1 /////////////////////////////////////////
  11. Save your changes.
  12. Call the task command by using your controller command
    1. Open the file for editing
    2. Locate and uncomment sections 4A, 4B, and 4C.
      The code in Section 4A creates the task command object by using the command factory, sets the command context, and sets input parameters of the task command. The code in Section 4B calls the validateParameters method for access control purposes before it invokes the execute method of the task command. The code retrieves the greetings value from the task command. The code in Section 4C adds a catch block for exceptions.
      /// Section 4A
          /// see how the controller command calls a task command
        MyNewTaskCmd cmd = null;
        try {
          cmd = (MyNewTaskCmd) CommandFactory.createCommand(
               "", getStoreId());
              // this is required for all commands
          /// set input parameters to task command
          cmd.setInputPoints(getPoints()); // change to Integer
      /// End Section 4A ///////////////////////////////////////
      /// Section 4B ///////////////////////////////////
          /// invoke the command's performExecute method
          /// retrieve output parameter from task command, then put it to 
           /// response properties
          rspProp.put("taskOutputGreetings", cmd.getGreetings());
      /// End Section 4B /////////////////////////////////////
      /// Start Section 4C ////////////////////////////////////
        } catch (ECException ex) {
          /// throw the exception as is
          throw (ECException) ex;
      /// End Section 4C //////////////////////////////////////
      Note: The performExecute method must never be called directly. Instead, the execute method must be called.
    3. Right-click the file; select Source > Organize Imports. If you are prompted to choose a type to import, select from the dialog box.
      The remaining compilation error should be resolved.
    4. Save your changes.
  13. Modify the MyNewJSPTemplate.jsp file to add the greetings message
    1. In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
    2. Open the MyNewJSPTemplate_All.jsp and MyNewJSPTemplate.jsp files for editing.
    3. Within the MyNewJSPTemplate_All.jsp file, locate and copy the code in section 7.
    4. Within the MyNewJSPTemplate.jsp file, paste the copied code into section 7. :
      The code can resemble
      <!-- SECTION 7 -->
      <fmt:message key="Greeting" bundle="${tutorial}" />
      <c:out value="${taskOutputGreetings}"/> <br /> <br />
      <!-- END OF SECTION 7 -->
    5. Save your changes.
  14. Test MyNewTaskCmd
    1. Start or restart the WebSphere Commerce test server
    2. In the Enterprise Explorer view, expand Stores > WebContent > Madisons.
    3. Right-click the index.jsp file; click Run As > Run on Server.
      Your storefront page is displayed in the web browser.
      Note: If prompted to select a server, select Choose an existing server and click Finish.
    4. In the web browser, enter the following URL: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=wcsadmin&input2=1000.
      The MyNewJSPTemplate page displays in the web browser and the greeting message displays:

      Screen capture of My new JSP template page that shows the greeting message created by the task command.