Step 3: Define the Custom Control

In this step you will create the custom control Hello. The custom control will have two methods, createTable and hello. The createTable method will be called only once by the client application and calls the corresponding method on the database control visitDB to create the table to be used in this application. The hello method can be invoked repeatedly by the client application. This method takes a visitor's name as an argument, in turn calls the visitDB database control to determine how many times the visitor has visited before, and returns a greeting that takes into account the number of prior visits. In other words, the custom control implements the business logic that governs a particular interaction.

The tasks in this step are:


To Create a Custom Control

  1. Right-click the hello folder in the Application tab and select New-->Java Control. The New Java Control dialog appears.
  2. Make sure that Custom is selected in the top panel and enter the file name HelloImpl.

  3. Click Create.

The files HelloImpl.jcs and Hello.java now appear in the Application tab above VisitDB.jcx in the hello folder. As you build your Java control you will work in the JCS file. WebLogic Workshop automatically updates the code in JAVA file to reflect the changes you made in the JCS file. In other words, you should not edit the JAVA file directly.

Also notice that the custom control opens in Design View in the main area of the IDE.

To Add the Database Control

The Hello custom control will use the VisitDB database control to determine if a visitor is new or returning. In order for the custom control to invoke the database control's methods, a database control instance must be defined for the custom control. To do so, you simply drag and drop the database control into the custom control's Design View.

  1. Make sure the Hello custom control is opened in Design View.
  2. In the Application tab, locate VisitDB.jcx, and drag and drop it onto Hello.

The VisitDB database control has now been added to the Hello custom control.

To Add the Methods

Now you will implement the methods createTable and hello to create a table and to implement the business logic that computes a response to a user on the basis of his/her previous number of visits.

  1. Ensure that the Hello control is displayed in Design View.
  2. Right-click Hello and choose Add Method. A method called newMethod1 appears.
  3. Rename the method createTable. If you step off the method and need to rename it, right-click the method and select Rename.
  4. Click the createTable method to go to Source View and modify the method as shown in red below:
        /**
         * @common:operation
         */
        public void createTable()
        {
            visitDB.createTable();
        }
  5. Return to Design View, right-click Hello and choose Add Method. A method called newMethod1 appears.
  6. Rename the method hello. If you step off the method and need to rename it, right-click the method and select Rename.
  7. Click the component method to go to Source View and modify the method as shown in red below:
        /**
         * @common:operation
         */
        public String hello(String name)
        {
            int visits = visitDB.getVisits(name) + 1;
            
            if(visits == 1)
            {
                visitDB.insertVisitor(name);
                return "Hello, " + name + "!";
            }
            else if(visits == 2)
            {
                visitDB.updateVisits(name, visits );
                return "Hello again, " + name + "!"; 
            }
            else
            {
                visitDB.updateVisits( name, visits );
                return "Hello, " + name + "!  This is visit number " + visits + "."; 
            }
        }
    
  8. Save your results.

In the hello method, the visitDB's getVisits method is called to determine the number of previous visits, and this number is updated by one. If the user is not known in the database, the value 0 is returned by the getVisits method, the visitor is entered into the database, and a greeting is returned. In all other cases the number of visits is updated for this known visitor, and a different greeting is returned to the invoking client application. Also, visitors who have one prior visit will be returned a different message than visitors who have multiple prior visits.

When you return to Design View, the Hello control should look like this:

Build and Deploy

Now let's build the control project and make sure both controls are defined correctly.

  1. Locate the Build window in the IDE. If the Build window is not there, select View-->Windows-->Build.
  2. In the Application pane, right-click MyControlProject, and select Build MyControlProject.
  3. Monitor the Build window and verify that the build completes correctly
  4. Open the Libraries folder in the Application pane, and notice that the file MyControlProject.jar has been recreated.

If you encounter build errors, verify that the hello and createTable methods have been created correctly.

Related Topics

Building Custom Java Controls

Click one of the following arrows to navigate through the tutorial: