Building a Java servlet framework using reflection, Part 2

Gain greater functionality with less code using these reflective code samples

1 2 3 Page 3
Page 3 of 3

A graphic artist creates templates with placeholders for the variable data elements in the design. In this fashion, the most page creation will be static, with some dynamic data added in at runtime. The following section further develops this model with a simple example.

Developing presentation

The presentation layer interacts closely with the majority of the application servers present on the market. In the case of the Netscape Application Server, there is a method through which templates can be evaluated and HTML can be output to the user.

If you consider that, due to its proximity to the user interface, a platform change is likely to have an impact on this layer, you should attempt to isolate the specific functionality from the majority of your developers. You can accomplish this through the PresentationServices interface. Once you implement this interface, you will be responsible for the platform-specific code required to interface with the application servers, Web servers, and template engines as needed. The methods exposed by this interface will allow the other development teams to use this functionality without being fully trained in the deployment platform tool. Here's the basic PresentationServices interface:

public interface PresentationServices{
    public void initializePresentation(ProxyInterface proxy) 
         throws IOException;
    public void output(String text) throws IOException;
    public void finalizePresentation() throws IOException;
}

Upon instantiation, the PresentationObject is passed a reference to the PresentationServices. Using this reference, the PresentationObject can delegate its behavior to the platform-specific object while maintaining a loose coupling between the object and the deployment platform. In this manner, the PresentationObject is isolated from the deployment platform code, but can still discharge the duties required by the business action.

The interaction between the business objects and presentation objects is managed through the PresentationInterface. This interface in turn provides methods that allow the business objects to interact with the presentation layer. The presentation object exposes methods, but does not give up its position as the expert presentation-handling object. This encapsulation allows each team to focus on its main development tasks without being too concerned that another team will misuse its object.

public interface PresentationInterface
{
    public boolean isLastPage();
    public void showNextPage(Hashtable pageData);
    public void showPage(Hashtable pageData);
    public void showConfirmation(Hashtable confData);
    public void showReference( Hashtable referenceData);
    public void showErrorPage(String errorMessage); 
}

To continue with my example, here are descriptions of the two pages I've developed for this article.

  • Registration page: Captures user information for use in reporting
  • Report page: Outputs usage information detailing the registered users and their city and country of origin

The PresentationRules are loaded in a similar fashion to the BusinessRules and DataRules, with the action name providing the key to the rules' location. In this case, a vector contains all of the pages for a given action. In the case of the multiple page action, more than one page will be in the vector.

public static Vector RegisterUserRules = new Vector();

The values can then be added to the vector to ensure that the pages are traversed properly, as follows:

RegisterUserRules.addElement(LOCATION + "RegisterUser.html");

The PresentationServices object does not contain any procedural code. It is driven by the business object to ensure that the presentation flows with the business logic. Having loaded the PresentationRules object, the PresentationObject can interact with the business objects as they perform their functionality.

As an example, the RegisterUser action will have a single page associated with it. When the BusinessObject interacts with the PresentationObject, it will be determined that there is only one page associated with this action, thereby triggering the transaction that will indicate that the action has been completed. Should an action have multiple pages, the business object will advance to the next page until the last page has been reached. This allows the business object to rely on the presentation object for the number of pages in an action further reducing the dependencies.

Additional pages can be added to the presentation simply by inserting additional references into the Vector. Because the business objects are not dependent on the specific pages on which fields reside, it is possible to make large changes to the presentation without necessitating changes in the business layer.

Conclusion

As I mentioned earlier, it is not possible to develop a singular all-encompassing framework. That is not the intent of this article; instead, the ideas presented here are intended to demonstrate some fairly simple methods through which you can use the power of Java to increase functionality exponentially while making sure that your code base grows only linearly.

Throughout the design and development of any system, the development team must make assumptions and decisions based on resources and time constraints. These decisions may make some of this framework infeasible, but I feel quite confident that a most of the ideas presented here can be applied across multiple development efforts. The following aspects of the framework should be widely applicable:

  • The separation of the business, data, and presentation layers into components, allowing development teams to work independently by reducing the required interactions
  • The creation of a reusable code base that performs such operations as validation, error handling, and database interaction
  • The use of interfaces to enforce the contracts agreed upon during the segmentation of layers
  • The use of reflection to reduce the if-then clauses that sometimes sneak into code
  • The isolation of product-specific features through the concept of services, which isolate the platform-specific code from the majority of the developers.

If you can spend the time on design and have a skilled development team, this is a good methodology. It requires a disciplined and detail-oriented team for the benefits to be fully realized. In addition, the underlying systems upon which the code is being developed must have some stability through the application design.

Michael Cymerman is a consultant specializing in Java/Internet software solutions. He provides Java/Internet-based architecture, design, and development solutions to Fortune 500 companies.

Learn more about this topic

1 2 3 Page 3
Page 3 of 3