# Working with Custom Controls

This topic describes how to use a custom Java control. It explains how to:

- Create a custom control
- Copy an existing custom control
- Add an existing custom control

A custom Java control can be invoked by other custom controls or by a web service using the procedures described here. A custom Java control can also be invoked from a web page, although the procedure for invoking the control from a web page environment is somewhat different. For more information, see Developing Web Applications and Page Flows and JSPs.

## To Create a Custom Control

To implement business logic for your web service, you can use a custom Java control. It may be a good idea create a folder in your project specifically for your new control. You cannot create the control in the root of a project:

- Right-click the folder for the new custom control, then choose New-->Custom Java Control.
- In the New File dialog, in the left-hand pane, select All.
- In the right-hand pane, select Custom Java Control.
- In the File name field, enter a name for your new control source (JCS) file. Be sure to append "Impl" to the file name, preceding the JCS extension.

  The JCS file will contain your control's implementation class, which is why it requires an "Impl" ending. Note that WebLogic Workshop will preserve the extension as you type.

- Click Create.

  WebLogic Workshop creates the JCS file and displays it in Design View. It also creates a JAVA file without the "Impl" ending for your control's public interface. As you build your control, you work in the JCS file, adding code for the control's logic. As you do, WebLogic Workshop updates the JAVA file code to reflect changes to the control's public interface. In other words, you never have to edit the JAVA file manually.

You can now add methods and callbacks to your control. For more information, see How Do I: Begin a New Custom Java Control?

## Using an Existing Custom Control

If you have access to a custom Java control that you either implemented yourself or that was implemented by another developer, you can add this control to a web service or another custom Java control. You have access to a control if you have access to its JCS file in your project. If the control is not in your project, you can copy it to your project.

## Copying the Custom Control

If the JCS file and the associated Java file for the custom control you wish to use is not in your project, you can copy it to your project. The destination to which you copy the control depends on the expected usage. If the control will be used only by a single web service in your project, you may choose to copy the custom control to the same folder as your web service, or to a different folder in the same project as your web service. If you expect that you will reuse this control in multiple projects, consider creating a Java control project. For more information, see How Do I: Create and Use a Java Control Within a Control Project?

Be aware that if you copy a custom control, you need to copy both the JCS and the associated Java file. If your custom control in turn uses other controls, all the associated files must be copied. An easy alternative is creating a copy of the folder that contains the files of your custom control and the invoked controls. You can then move this duplicate folder to the other project. To copy a folder:

- Right-click the folder that contains you custom control.
- Select Duplicate. The duplicate is created in the same project. You can now drag the duplicate to the target project.

If you copy a custom control, you are in effect creating a new control. If you do not change the definition of the copy, you now have two identical but separate controls. If the original copy of the control is subsequently changed, the second copy will remain unchanged. This means that any bug fixes or feature enhancements made to the original control will not be reflected in the copy.

## To Add an Existing Custom Control

If you have an existing custom control file in your project, you can add a reference to that control by dragging the JCS file from the Application pane to the Design View of the web service or custom control from which you would like to use it.

You can make multiple references to the same existing built-in control, for instance by referencing it in several web services in the same project. Note that you are making a reference and are not creating a copy. When you change the built-in control, the control is modified for every file in which it is referenced.

### Control References in Source Code

When you add a control to your application, WebLogic Workshop modifies your file’s source code to include an annotation and variable declaration for the control. The annotation ensures that the control is recognized by WebLogic Workshop, and the variable declaration gives you a way to work with the control from your code. For example, if you create a new custom control named Subscriptions in the CustomerControls folder in your project, and specify the variable name subscription, the following code will be added to your file:

    /**
      * @common:control
      */
    private CustomerControls.Subscriptions subscriptions;

Related Topics

Working with Built-In Controls

Building Web Services