Components of Java Web Services XML

A Java Web service uses one or more of the following components:

  • Java servlets
  • Java Server Pages
  • JAXP
  • JAXB
  • JAXM
  • JAX-RPC
  • JAXR

The following sections discuss these components in brief.

Java Servlets

Servlets are special Java programs that enhance the existing functionality of a Web site. These programs are deployed on the Web server that supports Java. You can include servlets in your Web site to make it dynamic. For example, you can include a Java servlet to display a welcome message to a user who logs on to a Web service. Servlets created by using Java offer several benefits:

  • Servlets use the features of Java, such as multithreading, accessing a database, networking, Remote Method Invocation (RMI), and so on. These features make servlets widely used programs.
  • Similar to Java programs, servlets are robust and portable. Servlets benefit from the exception handling and garbage collection features of Java.
  • Applications that use servlets are highly efficient. This is because the code that initializes a servlet is executed only the first time. Therefore, unnecessary processes are avoided, resulting in a higher efficiency of the Web servers that host servlets.
  • Servlets increase the performance of an application by storing the frequently used data in a temporary memory location. This saves the time taken to frequently access a disk.

To invoke a servlet, a client application needs to send in a request by using the HTTP GET or the HTTP POST methods. Then, the Web server processes the request and sends the result to the Web browser or the client application.

Java Server Pages

Java Server Pages (JSP) is another technology that is used to create dynamic Web pages. JSP provides encapsulation of the code of the Web page by separating the code written using Java from the interface of the Web page.

A JSP page contains HTML and JSP constructs. When a request is sent for a JSP page for the first time, the HTML and the JSP constructs in a JSP source file are compiled and converted into a servlet. After that, the servlets are loaded to handle any further request. Therefore, whenever a request for a JSP file is made, the Web server executes the respective servlet.

As discussed, a JSP source file contains JSP constructs. These constructs enable you to include code written in Java in a JSP page and provide the business logic or the programming code for the JSP page. The following list discusses the categories of JSP constructs:

  • JSP directives. These constructs provide additional information about a JSP page. They also allow you to import or include packages in your JSP page. The following code is an example of JSP directives:

    The JSP directives contain several attributes that store the information about a JSP page. These attributes are defined in Table.

    Attributes of JSP Directives

    Attributes of JSP Directives

    Attributes of JSP Directives

  • JSP declarations. These constructs declare methods and variables that are used in the JSP page. The following code is an example of the JSP declarations:
  • JSP expressions. These constructs store references to variables and methods that are used in a JSP page. The JSP expressions allow you to insert Java values directly to an output. However, before doing this, the Java values are converted into string values. An example of the JSP expressions is shown in the following code:
  • JSP scriptlet. These constructs are used to include Java code in JSP pages. They allow you to benefit from the features of the Java programming language in a JSP page. The features include decision-making and iteration loops, making the content in a JSP page dynamic. For example, a user tries to log on to the home page of a Web site. To do so, he needs to enter a username and a password. However, the programmer has applied a validation that a password must be at least eight characters long. If the password is eight characters or more, a welcome message is displayed; otherwise, an error message is displayed. The code for the JSP scriptlets is shown in the following example:
  • JSP comments. These constructs are used to include comments in your JSP page. The JSP comments can be of different types, as explained in Table.
  • Types of JSP Comments

    Types of JSP Comments

  • JSP actions. These constructs are used to extend the functionality of JSP pages. To do this, you can use the Java objects or include files or other JSP pages in your JSP page. The JSP actions are of several types, as explained in Table.
  • Types of JSP Actions

    Types of JSP ActionsTypes of JSP Actions

JAXP

As already discussed, the Java platform provides you with several APIs that you can use to create Web services and Web applications with the Java programming language. Java API for XML Processing (JAXP) is one such API that is used to process and transform XML documents.

JAXP, developed by JavaSoft and the JCP community, is a Java API that is used to easily and efficiently process XML documents. To do this, JAXP uses several parsers and applications that are created using the Java programming language. JAXP is a standard available in the Java XML Pack (JAX-Pack) and Java WSDP.

JAXP supports the industry standards, such as SOAP, UDDI, and WSDL. As you know, these industry standards are based on XML; therefore, it becomes important to have an API that processes an XML document. As a result, JAXP specifications were developed that allow you to access, convert, and process XML documents.

The latest version of JAXP specifications is version 1.2. You can download this version from the official Web site of JCP (http:// java.sun.com/ aboutJava / communityprocess /jsr /jsr_063_jaxp.html) as a JSR 63 document.

This version of JAXP includes support for XSLT and XML schema. The following list discusses the versions of specifications that are included in JAXP 1.2:

  • W3C XML 1.0 Recommendation (Second edition)
  • SAX 2.0
  • DOM Level 2
  • W3C XML Namespaces 1.0 Recommendation
  • XSLT 1.0

As discussed, Java APIs are flexible. This flexibility of JAXP is achieved because of the pluggability layer, which allows you to use XML parsers from your Web application. In addition, this layer allows you to plug an XSLT processor so that you can serialize XML data into various formats.

However, it is important to note that JAXP does not parse the XML documents. To do so, JAXP uses already-existing industry specifications. These specifications include Simple API for XML Parsing (SAX) and Document Object Model (DOM), as explained in the following sections.

SAX

The SAX specifications provide means to serially access an XML document. To do so, SAX uses a data stream; therefore, it's an event-based parser. The data stream serially reads data from top to bottom in an XML document and notifies the application about syntax construction. To notify the running application, the SAX API uses the ContentHandler interface. This interface contains several methods that are called whenever the SAX parser comes across a tag.

For example, the startElement() method is called for the < (start of element) tag, and the endElement() method is called for the /> (end of element tag). To understand the working of a SAX parser, consider the following example of an XML document:

To parse the previous XML document, the XML parser performs the following steps:

  1. The parser calls the startElement() method for the <Books> element.
  2. Similarly, the parser calls the startElement () method for the <Technical> element.
  3. The parser calls the characters() and endElement() methods for the <Software>, <Operating_Systems>, and <Programming_Languages> elements.
  4. Note?/td>The characters() method parses character data. This method also lies in the ContentHandler interface.

  5. Next, the endElement() method is called for the <Technical> element.
  6. Steps 2-4 are repeated for the <Non_Technical> element.
  7. Finally, the endElement() method is called for the <Books> element.

The previous steps are used to parse the XML document according to the SAX specifications. However, it is important to note that the SAX parsers do not explicitly parse an XML document. To do so, you need to perform the following steps:

  1. Create a class that extends DefaultHandler, which is the default implementation of the ContentHandler interface.
  2. Write implementations for the methods of the ContentHandler interface.
  3. Create an instance of the SAXParserFactory class.
  4. Create an object of the SAXParser class.
  5. Call the parse() method from this instance.
  6. Pass the instance of the class that extends DefaultHandler to the parse() method.

DOM

The W3C DOM Working Group developed the DOM API to access an XML document in the form of an object tree. The DOM API contains several interfaces that contain methods, such as insert() and remove(). These methods allow you to modify an object tree by adding or deleting elements, respectively.

As a result of the tree-like structure, a user can randomly acces data by using the DOM API. In addition, by using a SAX parser, you can only read data from an XML document. However, in contrast, a DOM API allows you to add or delete elements from the XML document. Therefore, a DOM parser is used when a user needs to modify the existing XML document. On the other hand, the SAX parser is used when you need to access small pieces of data. This is because a DOM parser requires the tree object of the whole XML document to be placed in the memory. As a result, if the document is large enough, the search for a small piece of data will be time consuming and, therefore, less efficient.

Note?/td>
The steps to access a DOM parser are the same as for that of the SAX parser, as explained in the previous section.

Java API for XML Binding

Java API for XML Binding (JAXB) is another Java API. In fact, it is an architecture that maps a Java object to an XML document. To do this, a Java object is created that is bound to the XML document. This process is called data binding. It is essential to incorporate data binding of Java objects with XML documents because the industry standards for transferring data over the Web are mainly XML based.

In the process of data binding, a class hierarchy is created that is similar to the structure of an XML document that allows you to access or modify data in this class hierarchy without actually considering the structure of the XML document. To do this, a DTD and an XML schema are required as inputs. From there, the elements of the XML document are mapped to the values or Java classes. For example, an XML element that contains subelements is mapped to Java classes, and the XML elements that do not contain subelements are mapped to the Java values.

Then a Java source file is created by using the schema compiler. Finally, the Java compiler compiles these Java source files to Java classes. JAXB uses the process of unmarshalling and marshalling. Unmarshalling is the process of creating applications that contain classes required to convert an XML document to a tree object. Marshalling is the process of converting a tree object back to an XML document.

Components of the JAXB API

The following list discusses the components of the Java API:

  • Binding language. This is an XML-based language that defines the data binding of a DTD to the Java classes.
  • Schema compiler. This is used to generate the Java source files based on the DTD and the schema.
  • Binding framework. This is a set of Java classes and interfaces that help in the process of marshalling and unmarshalling. In addition, JAXB performs validation of the XML documents. The classes required for the validation are also included in the binding framework.

These components work together to bind an XML document to the corresponding Java classes. The following section discusses the working of these components.

Working of the JAXB API

To work with the JAXB API, you need to perform the following steps:

  1. Create a DTD document for the corresponding XML document.
  2. Define a binding schema to generate the Java-derived classes.
  3. Run the command to enable the schema compiler to generate the derived classes.
  4. Convert the XML document to an object tree by using the unmarshall() method. You can then work with the Java object tree.
  5. Perform validation on the object tree.
  6. Convert the object tree back to the XML document.

Advantages of the JAXB API

The JAXB API has several advantages, as discussed in the following list:

  • JAXB is faster than the other XML parsers, such as SAX and DOM, because JAXB uses compiled Java classes. These classes already have a schema defined for them, which saves time. In addition, JAXB does not require you to create complex code that is required to process an XML document.
  • JAXB uses both Java and XML. As a result, JAXB benefits from the advantages of both.
  • Before binding an XML document to the corresponding Java classes, JAXB verifies whether the document is valid according to a schema. Only after the verification is complete is the XML document bound to the corresponding Java classes.
  • As already discussed, JAXB converts an XML document to a Java class. Therefore, JAXB supports data conversion.

Limitations of the JAXB API

In addition to the previously listed advantages, JAXB has certain limitations:

  • The schema compiler that is used with JAXB does not fully support all specifications defined in JAXB.
  • JAXB supports only the DTD that is specified in the XML 1.0 recommendation.
  • The code that the compiler generates is not very efficient

Java API for XML Messaging

As you know, the data that is transferred over the network is in XML format. The Java API for XML Messaging (JAXM) is an API used to transfer these XML messages. JAXM is based on industry standards, such as SOAP 1.1. Therefore, it is a standard used to exchange SOAP messages between the service provider and the service requestor applications.

In the e-business scenario, messages are exchanged between various applications or members of the value chain. To allow a smooth exchange of messages over the network, JAXM defines a framework that consists of the following types of messages:

  • Asynchronous message. These messages are one-way operations, such as a request from a service requestor application. In this case, the service provider application does not expect a prompt response. The concept of an asynchronous message is explained in Figure.
  • The Asynchronous Message

    The Asynchronous Message

  • Asynchronous message with acknowledgement. This is also a one-way operation, but in this case, the service provider sends a acknowledgement to the service requestor application. An asynchronous message with acknowledgement is explained in Figure.
  • The Asynchronous Message with Acknowledgement

    The Asynchronous Message with Acknowledgement

  • Synchronous message. This is a two-way message in which a service requestor application sends a request to a service provider application and then waits for the response from the service provider application. In this case, the service requestor application is blocked until the time it receives a response from the service provider application. Figure shows a synchronous message.
  • The Synchronous Message

    The Synchronous Message

  • Synchronous message with acknowledgement. This is a synchronous message in which the service provider application waits for an acknowledgement from the service requestor application. The synchronous message with acknowledgement is shown in Figure.
  • The Synchronous Message with Acknowledgement

    The Synchronous Message with Acknowledgement

  • One-way with no reply. As the name suggests, this is a one-way operation in which a requestor application sends a request to the provider application, but it does not expect a response or an acknowledgement from the provider application. Figure shows a one-way with no reply message.
  • The One-Way with No Reply Message

    The One-Way with No Reply Message

JAXM might use a messaging provider service to exchange or transfer messages between applications. This service routes the messages from one application to another. Therefore, when a message is sent from a client application (a service provider application in the case of Web services), the message goes to the messaging provider of the client application. The messaging provider then routes the message to the messaging provider of the receiving application, or the service provider application in the case of Web services. Finally, the messaging provider of the receiving application forwards the request to the receiving application—the ultimate destination of the request.

Note?/td>
It is important to note that JAXM can be used in all business scenarios that require transfer of data. This implies that JAXM is not limited to the Web services scenario. Figure shows the transfer of messages by using the JAXM API.

Transfer of Messages by using the JAXM API

Transfer of Messages by using the JAXM API

JAX-RPC

In today's business world, RPC-based Web services are used extensively. An RPC-based Web service contains Web methods that can be called remotely over the network. For example, a weather forecast Web service is an example of an RPC-based Web service that accepts the name of a place as a parameter and uses the SOAP message to return the temperature for the specified place.

Java API for XML-based RPC (JAX-RPC) is a set of APIs that allow you to develop a Web service that you can call remotely from a Java client application. The packages contained within the JAX-RPC are client-side API packages, as shown in the following list:

  • javax.xml.namespace
  • javax.xml.rpc
  • javax.xml.rpc.encoding
  • javax.xml.rpc.handler
  • javax.xml.rpc.handler.soap
  • javax.xml.rpc.holders
  • javax.xml.rpc.servers
  • javax.xml.rpc.soap

JAX-RPC hides the complexities of making calls to remote Web services from client applications. To do this, JAX-RPC uses the concepts of stubs, ties, and configuration files. In fact, JAX-RPC uses the SOAP-based XML messaging framework to invoke Web methods remotely. This framework is built on the concepts of SOAP, XML RPC, RMI, CORBA, and Messaging.

Note?/td>
You can install the latest version, JAX-RPC 0.8, from the official site of 303 JCR The URL for this site is http:// jcp .org/ jsr /detail /101.jsp.

JAX-RPC Framework

The JAX-RPC framework includes the Java initiative, which you can use to create Web services that can be accessed remotely from a client application. The creation of Web services by using the Java initiative includes the following stages:

  • Service definition. The service definition stage defines an interface for the Web service that is similar to the RMI interface. In addition, it defines the endpoints of a remote service.
  • Service implementation. The services created by using the JAX-RPC framework are implemented by using the Java platform. To do this, several tools are available that allow you to implement a Web service as a container that manages the Web service.
  • Service deployment. The JAX-RPC framework allows you to deploy a Web service both on the client side and the server side. The framework provides you with a deployment tool, such as xrpcc, that allows you to deploy a Web service within a container. This tool is used for protocol bindings, to create a configuration for the binding, and to provide a service description in the form of a WSDL document.
  • Service description. This stage involves creating a WSDL document for the Web service. For simplicity, you can use the deployment tool to describe a Web service in the form of a WSDL document.
  • Service invocation. In this stage, a client application remotely invokes a Web service. To do this, the deployment tool obtains a service definition in the form of WSDL-to-Javastubs. These stubs are the proxy definitions of a Web service at the client end. The client application then uses the stubs to invoke a Web service.

Figure shows the JAX-RPC framework.

The JAX-RPC Framework

The JAX-RPC Framework

Advantages of JAX-RPC

As discussed earlier, JAX-RPC allows you to remotely invoke the Web methods that a Web service exposes. To do this, JAX-RPC allows you to create client applications in Java and other platforms that can remotely access a Web service. In addition, JAX-RPC offers several advantages, as discussed in the following list:

  • The most important feature of a Web service is its interoperability. This feature is extended by using JAX-RPC because it allows you to create client applications in all platforms that can access the Java Web services. In addition, any Java client application can access any Web service created on any platform. This feature of interoperability is the result of JAX-RPC support for industry standards, such as SOAP, UDDI, and WSDL.
  • JAX-RPC is a user-friendly API. Programmers who have worked with RPC will definitely appreciate the ease that JAX-RPC provides to its users. JAX-RPC hides the implementation of the RPC-based Web services from the client and the server applications.

Note?/td>JAX-RPC is based on the standards defined in the SOAP specifications 1.1. These standards act as guidelines or rules for remotely accessing a Web service.

  • JAX-RPC provides an architecture that supports all protocols used for binding SOAP services. In addition, this architecture, called the Java initiative, is built such that it can be extended to support binding protocols that will be developed in the future. The Java 305 initiative remotely implements a Web service from a Java client application. You learned about the Java initiative earlier in the section "JAX-RPC Framework."
  • JAX-RPC provides several advantages over RPC. For example, JAX-RPC allows you to exchange complete documents or parts of documents, as required. In addition, JAXRPC can be used to transfer asynchronous and one-way messages.
  • JAX-RPC supports advanced SOAP message handlers that allow you to transfer SOAP messages in various formats over the network.

Java APIs for XML Registries

Java APIs for XML Registries (JAXR) provide APIs that allow you to conveniently and quickly access a business registry. As you know, a Web service needs to be registered with the business registries where the users can search for the required Web service. This search is facilitated by the JAXR APIs. To do this, JAXR supports a pluggable architecture, which allows you to connect to various business registries, such as the registries based on ebXML or UDDI registries. These registries are collectively called XML registries. We discussed the components of Java Web services that included a layer called the service broker in the section titled "Roles in the Java Service Model." These XML registries perform the role of a service broker in the Web services scenario.

Note?/td>
Registries that ebXML or OASIS develop are open standards registries that derive from existing registry standards. In contrast, a consortium of industry leaders develop and maintain UDDI registries. As a result, UDDI registries are more widely used.

APIs in JAXR

JAXR, developed by the JCP organization, is currently in version 1.0. You can download this version from http:// jcp.org/ jsr /detail /93.jsp, where the jsr of the JAXR API is 93. The 0.9 version of JAXR contains several interfaces, which are further contained within APIs. The APIs in the JAXR API package are broadly categorized into two categories, as discussed in the following list:

  • javax.xml.registry.infomodel. This API includes interfaces that define the objects in a service registry. For example, the javax.xml.registry.infomodel API includes interfaces such as Association, Concept, EmailAddress, Key, User, Service, Slot, Organization, and so on.
  • javax.xml.registry. This API includes interfaces that allow a JAXR client to access the service registry. The interfaces that are included in the JAXR API package include BulkResponse, CapabilityProfile, Query, Federation, RegistryService, Connection, and so on.

JAXR is an API that allows a Java client application to access various registry standards. This is the limitation of JAXR; only Java client applications can access the registries. This feature is illustrated in Figure.

The Interoperability Feature of JAXR

The Interoperability Feature of JAXR

As you know, JAXR allows a client application to access registries that are based on XML. This implies that JAXR supports the feature of interoperability. In fact, XML-based registries aim to integrate various Web services developed on different standards, enabling these Web services to interoperate using a common standard, XML. Therefore, any communication that takes place between the components of a Web service uses XML messaging technology. Due to support for interoperability of Web services, JAXR API is extensively used in a B2B scenario. To understand the feature of interoperability that JAXR supports, it is important for you to understand the working of the JAXR API The following section discusses this.

Working of JAXR API

The following steps show the working of JAXR in the B2B scenario:

  1. The client application sends a request by using JAXR to the service registry.
  2. The XML-based registry sends an XML specification document to the client application. This specification defines the process of information exchange in the B2B scenario.
  3. The client application implements the business process, as defined in the XML specifications.
  4. The provider application registers its service with the service registry by using JAXR.
  5. The client application searches for this information about the service in the service registry.
  6. A list of Web services that matches the search criteria is displayed.
  7. The client application can access the detailed information about any desired Web service in the available list. To do this, the client application uses JAXR.
  8. If the available Web service is appropriate for the client, the client negotiates with the provider of the service and signs a contract with service provider.
  9. The client binds to the contracted Web service and implements it.

The working of the JAXR API is illustrated in Figure

The Working of the JAXR API

The Working of the JAXR API



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics