Home > Articles > Programming > Java

This chapter is from the book

2.2 J2EE: The Integrated Platform for Web Services

Starting with the J2EE 1.4 platform, with its main focus on Web services, the existing Java-XML technologies are integrated into a consolidated platform in a standard way, thereby allowing applications to be exposed as Web services through a SOAP/HTTP interface. The next sections briefly describe the Web service-specific additions made in the J2EE 1.4 platform. (Chapter 1 includes an overview of the J2EE 1.4 platform. See the J2EE 1.4 specification listed in "References and Resources" on page xx for complete information on the platform.)

This section is intended to give you an overview of the various Web service-specific additions in the J2EE platform. The next three chapters cover how to use these technologies in detail.

2.2.1 Java APIs for XML Processing

JavaTM APIs for XML Processing (JAXP) is a vendor-neutral set of lightweight APIs for parsing or processing XML documents. Because XML is the common language enabling Web services, an XML parser is a necessity to process the messages—the XML documents—exchanged among Web services. Figure 2.4 depicts how the JAXP API abstracts the parser implementations from the user application.

02fig04.gifFigure 2.4 Using JAXP to Abstract Parser Implementations from User Application88380 Figure caption Figure 1.5 JAXP

Keep in mind that the JAXP API is not new to the J2EE 1.4 platform. It has been part of the earlier versions of both the J2EE and JavaTM 2 Standard Edition (J2SETM) platforms. In the J2EE 1.4 platform implementation, JAXP has added support for XML schemas.

Although it has its own reference implementation, JAXP allows JAXP specification-conforming parsers from other vendors to be plugged in. JAXP falls back to parsing an XML document using its own implementation if no other implementation is provided. JAXP processes XML documents using the SAX or DOM models, and it permits use of XSLT engines during the document processing. (XSLT, which stands for eXtensible Stylesheet Language Transformation, is used for transforming XML documents from one format to another.)

The main JAXP APIs are available through the javax.xml.parsers package, which provides two vendor-agnostic factory interfaces—one interface for SAX processing and another for DOM processing. These factory interfaces allow the use of other JAXP implementations.

Figure 2.5 shows how the SAX and DOM parsers function. SAX processes documents serially, converting the elements of an XML document into a series of events. Each particular element generates one event, with unique events representing various parts of the document. User-supplied event handlers handle the events and take appropriate actions. SAX processing is fast because of its serial access and small memory storage requirements. Code Example 2.7 shows how to use the JAXP APIs and SAX to process an XML document.

Example 2.7. Using SAX to Process an XML Document

public class AnAppThatUsesSAXForXMLProcessing
       extends DefaultHandler {

   public void someMethodWhichReadsXMLDocument() {

      // Get a SAX PArser Factory and set validation to true
      SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setValidating(true);

      // Create a JAXP SAXParser
      SAXParser saxParser = spf.newSAXParser();

      // Get the encapsulated SAX XMLReader
      xmlReader = saxParser.getXMLReader();

      // Set the ContentHandler of the XMLReader
      xmlReader.setContentHandler(this);

      // Tell the XMLReader to parse the XML document
      xmlReader.parse(XMLDocumentName);
   }
}

02fig05.gifFigure 2.5 SAX- and DOM-Based XML Parser APIs43520 Figure caption Figure 1.1 SAX- and DOM-Based XML Parser APIs

DOM processing creates a tree from the elements in the XML document. Although this requires more memory (to store the tree), this feature allows random access to document content and enables splitting of documents into fragments, which makes it easier to code DOM processing. DOM facilitates creations, changes, or additions to incoming XML documents. Code Example 2.8 shows how to use the JAXP APIs and DOM to process an XML document.

Example 2.8. Using DOM to Process an XML Document

public class AnAppThatUsesDOMForXMLProcessing {

   public void someMethodWhichReadsXMLDocument() {

      // Step 1: create a DocumentBuilderFactory
      DocumentBuilderFactory dbf =
                DocumentBuilderFactory.newInstance();
      dbf.setValidating(true);
      // Step 2: create a DocumentBuilder that satisfies
      // the constraints specified by the DocumentBuilderFactory
      db = dbf.newDocumentBuilder();

      // Step 3: parse the input file
      Document doc = db.parse(XMLDocumentFile);

      // Parse the tree created - node by node
   }
}

2.2.2 JavaTM API for XML-Based RPC

JavaTM API for XML-based RPC (JAX-RPC) supports XML-based RPC for Java and J2EE platforms. It enables a traditional client-server remote procedure call (RPC) mechanism using an XML-based protocol. JAX-RPC enables Java technology developers to develop SOAP-based interoperable and portable Web services. Developers use the JAX-RPC programming model to develop SOAP-based Web service endpoints, along with their corresponding WSDL descriptions, and clients. A JAX-RPC-based Web service implementation can interact with clients that are not based on Java. Similarly, a JAX-RPC-based client can interact with a non-Java-based Web service implementation.

For typical Web service scenarios, using JAX-RPC reduces complexity for developers by:

  • Standardizing the creation of SOAP requests and responses

  • Standardizing marshalling and unmarshalling of parameters and other runtime and deployment-specific details

  • Removing these SOAP creation and marshalling/unmarshalling tasks from a developer's responsibilities by providing these functions in a library or a tool

  • Providing standardized support for different mapping scenarios, including XML to Java, Java to XML, WSDL-to-Java, and Java-to-WSDL mappings

JAX-RPC also defines standard mappings between WSDL/XML and Java, which enables it to support a rich type set. However, developers may use types that do not have standard type mappings. JAX-RPC defines a set of APIs for an extensible type mapping framework that developers can use for types with no standard type mappings. With these APIs, it is possible to develop and implement pluggable serializers and de-serializers for an extensible mapping. Figure 2.6 shows the high-level architecture of the JAX-RPC implementation.

02fig06.gifFigure 2.6 JAX-RPC Architecture99136 Figure caption Figure 1.6 JAX-RPC Architecture

A client application can make a request to a Web service in one of three ways. Chapter 5 contains detailed descriptions of these client access approaches.

  1. Invoking methods on generated stubs— Based on the contents of a WSDL description of a service, tools can be used to generate stubs. These generated stubs are configured with all necessary information about the Web service and its endpoint. The client application uses the stubs to invoke remote methods available in the Web service endpoint.

  2. Using a dynamic proxy— A dynamic proxy supports a Web service endpoint. When this mode is used, there is no need to create endpoint-specific stubs for the client.

  3. Using a dynamic invocation interface (DII)— In this mode, operations on target service endpoints are accessed dynamically based on an in-memory model of the WSDL description of the service.

No matter which mode is used, the client application's request passes through the client-side JAX-RPC runtime. The runtime maps the request's Java types to XML and forms a corresponding SOAP message for the request. It then sends the SOAP message across the network to the server.

On the server side, the JAX-RPC runtime receives the SOAP message for the request. The server-side runtime applies the XML to Java mappings, then maps the request to the corresponding Java method call, along with its parameters.

Note that a client of a JAX-RPC service may be a non-Java client. Also, JAX-RPC can interoperate with any Web service, whether that service is based on JAX-RPC or not. Also note that developers need only deal with JAX-RPC APIs; all the details for handling SOAP happen under the hood.

JAX-RPC supports three modes of operation:

  1. Synchronous request–response mode— After a remote method is invoked, the service client's thread blocks until a return value or exception is returned.

  2. One-way RPC mode— After a remote method is invoked, the client's thread is not blocked and continues processing. No return value or exception is expected on this call.

  3. Non-blocking RPC invocation mode— A client invokes a remote procedure and continues in its thread without blocking. Later, the client processes the remote method return by performing a blocked receive call or by polling for the return value.

In addition, JAX-RPC, by specifying a standard way to plug in SOAP message handlers, allows both pre- and post-processing of SOAP requests and responses. These message handlers can intercept incoming SOAP requests and outgoing SOAP responses, allowing the service to do additional processing. See the JAX-RPC specification (listed in "References and Resources" on page xx) for more information on JAX-RPC.

Code Example 2.9 is an example of a JAX-RPC service interface for a simple service that provides weather information for a city.

Example 2.9. JAX-RPC Service Endpoint Interface Example

public interface WeatherService extends Remote {
   public String getWeather(String city) throws RemoteException;
}

Code Example 2.10 shows the implementation of the weather service interface using a Web component.

Example 2.10. JAX-RPC Service Implementation

public class WeatherServiceImpl implements
                 WeatherService, ServiceLifecycle {
   public void init(Object context) throws JAXRPCException {}

   public String getWeather(String city) {
      return ("Early morning fog clearing midday; " +
             "over all great day expected in " + city);
   }

   public void destroy() {}
}

Code Example 2.11 shows how a client, using JAX-RPC to access this weather service.

Example 2.11. A Java/J2EE Client Accessing the Weather Service

.....
Context ic = new InitialContext();
Service svc = (Service)
       ic.lookup("java:comp/env/service/WeatherService");
WeatherSvcIntf port = (WeatherSvcIntf)
       svc.getPort(WeatherSvcIntf.class);
String info = port.getWeather("New York");
.....

These examples illustrate that a developer has to code very little configuration and deployment information. The JAX-RPC implementation handles the details of creating a SOAP request, handling the SOAP response, and so forth, thereby relieving the developer of these complexities.

2.2.3 JavaTM API for XML Registries

JavaTM API for XML Registries (JAXR), a Java API for accessing business registries, has a flexible architecture that supports UDDI, and other registry specifications (such as ebXML). Figure 2.7 illustrates the JAXR architecture.

02fig07.gifFigure 2.7 JAXR Architecture72953 Figure caption Figure 1.7 JAXR Specification

A JAXR client, which can be a stand-alone Java application or a J2EE component, uses an implementation of the JAXR API provided by a JAXR provider to access business registries. A JAXR provider consists of two parts: a registry-specific JAXR provider, which provides a registry-specific implementation of the API, and a JAXR pluggable provider, which implements those features of the API that are independent of the type of registry. The pluggable provider hides the details of registry-specific providers from clients.

The registry-specific provider plugs into the pluggable provider, and acts on requests and responses between the client and the target registry. The registry-specific provider converts client requests into a form understood by the target registry and sends the requests to the registry provider using registry-specific protocols. It converts responses from the registry provider from a registry-specific format to a JAXR response, then passes the response to the client.

Refer to the JAXR specification for more information.

2.2.4 SOAP with Attachments API for JavaTM

SOAP with Attachments API for JavaTM (SAAJ), which enables developers to produce and consume messages conforming to the SOAP 1.1 specification and SOAP with Attachments note, provides an abstraction for handling SOAP messages with attachments. Advanced developers can use SAAJ to have their applications operate directly with SOAP messages. Attachments may be complete XML documents, XML fragments, or MIME-type attachments. In addition, SAAJ allows developers to enable support for other MIME types. JAX technologies, such as JAX-RPC, internally use SAAJ to hide SOAP complexities from developers.

SAAJ allows the following modes of message exchanges:

  • Synchronous request-response messaging— the client sends a message and then waits for the response

  • One-way asynchronous messaging (also called fire and forget)— the client sends a message and continues with its processing without waiting for a response

    Refer to the SAAJ specification for more information.

2.2.5 Web Service Technologies Integrated in J2EE Platform

Up to now, we have examined how the Java XML technologies support various Web service standards. Now let's see how the J2EE 1.4 platform combines these technologies into a standard platform that is portable and integrated. Not only are the Java XML technologies integrated into the platform, the platform also defines Web service-related responsibilities for existing Web and EJB containers, artifacts, and port components. The J2EE 1.4 platform ensures portability by integrating the Java XML technologies as extensions to existing J2EE containers, packaging formats, deployment models, and runtime services.

A Web service on the J2EE 1.4 platform may be implemented as follows:

  • Using a JAX-RPC service endpoint— The service implementation is a Java class in the Web container. The service adheres to the Web container's servlet lifecycle and concurrency requirements.

  • Using an EJB service endpoint— The service implementation is a stateless session bean in an EJB container. The service adheres to the EJB container's lifecycle and concurrency requirements.

In either case, the service is made portable with the definition of a port component, which provides the service's outside view for Web service implementation. A port component consists of:

  • A WSDL document describing the Web service that its clients can use

  • A service endpoint interface defining the Web service's methods that are available to clients

  • A service implementation bean implementing the business logic of the methods defined in the service endpoint interface. The implementation may be either a Java class in the Web container or a stateless session bean in the EJB container.

Container-specific service interfaces, created by the J2EE container, provide static stub and dynamic proxies for all ports. A client of a J2EE platform Web service can be a Web service peer, a J2EE component, or a stand-alone application. It is not required that the client be a Web service or application implemented in Java.

How do clients use a J2EE platform Web service? Here is an example of a J2EE component that is a client of some Web service. Such a client uses JNDI to look up the service, then it accesses the Web service's port using methods defined in the javax.xml.rpc.Service interface. The client accesses the service's functionality using its service endpoint interface. A client that is a J2EE component needs only consider that the Web service implementation is stateless. Thus, the client cannot depend on the service holding state between successive service invocations. A J2EE component client does not have to know any other details of the Web service, such as how the service interface accesses the service, the service implementation, how its stubs are generated, and so forth.

Recall (from Code Example 2.9 and Code Example 2.10) what a Web service interface, such as the weather Web service, looks like when implemented as a JAX-RPC service endpoint on a J2EE platform. In contrast, Code Example 2.12 shows the equivalent EJB service endpoint implementation for the same weather service.

Example 2.12. EJB Service Endpoint Implementation for a Weather Service

public class HelloService implements SessionBean {
   private SessionContext sc;

   public WeatherService(){}
   public void ejbCreate() {}
   public String getWeather(String city) {
      return ("Early morning fog clearing midday; " +
             "over all great day expected in " + city);
   }
   public void setSessionContext(SessionContext sc) {
      this.sc = sc;
   }
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
}

Keep in mind that any client can use the code shown in Code Example 2.11 to access this weather service. This holds true

  • Regardless of whether the service is implemented as a JAX-RPC service endpoint or an EJB service endpoint

  • Regardless of whether the client is a servlet, an enterprise bean, or a stand-alone Java client

2.2.6 Support for WS-I Basic Profile

So far we have seen how the various Java technologies support Web service standards. We have also examined how these Java technologies have been integrated into the J2EE platform in a standard way to ensure portability of Web service implementations across J2EE platforms. Since ensuring interoperability among heterogeneous platforms is a primary force for Web services, the J2EE platform supports the WS-I Basic Profile.

As already seen in "Emerging Standards" on page 40, WS-I is an organization that spans industries and whose charter is to create and promote interoperability of Web services. WS-I has published the WS-I Basic Profile, which dictates how a set of Web service standards should be used together to ensure interoperability. The WS-I Basic Profile covers:

  • Messaging standards (such as SOAP)

  • Description and discovery standards (such as UDDI)

  • Security

By supporting the WS-I Basic Profile, the J2EE platform is assured of providing an interoperable and portable platform for the development of Web services.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020