Simplify your Business Delegates with the Service Locator

Unless your Business Delegates use a Service Locator, they will have duplicate code for dealing with the lookup service.

To implement a Service Locator, we’ll take all of the logic for doing the JNDI lookup and move it out of the multiple Business Delegates and into a single Service Locator.

Typically in J2EE applications, there will be a number of components that all use the same JNDI service. While a complex application might use several different registries such as JNDI and UDDI (for web service endpoints), an individual component will typically need access to only one registry. In general, a single Service Locator will support a single, specific registry.

By making the Business Delegate an object that handles only the business methods rather than also handling the registry lookup code, you increase the cohesion for the Business Delegates.

A Service Locator’s pseudo-code

// obtain an InitialContext object
// perform remote lookup
// handle remote issues
// optionally, cache references
image with no caption

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.