Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Extend Your J2EE Applications into Web Services : Page 3

Web services are here. It's time to extend your J2EE applications. Apache Axis allows you to Web-service enable existing J2EE applications without touching your back-end business logic.

Transform J2EE Application into Web Services Application
Web Services Description Language (WSDL)
The first step to creating a Web services application is to define its services in a WSDL file. WSDL defines an XML grammar for describing the network services (communications) in a structured way.

Type tag:
In this example, since the service reverseString exchanges simple strings, you don't need to define a 'TYPE' tag. If your service exchanged complex type information, you'd use the 'TYPE' tag to define the complex types data.

Message tag:
The service reverseString takes an input string and returns an output string. In MyWebService.wsdl, the input and output strings are defined with the 'MESSAGE' tag. This is similar to the concept of passing parameters to a method call in Java or C++:

<message name="reverseStringRequest"> <part name="orgStr" type="xs:string"/> </message> <message name="reverseStringResponse"> <part name="revStr" type="xs:string"/> </message>

PortType tag:
The abstract end point of the service reverseString operation is defined with the 'PORTTYPE' parameter. The service takes an input and returns an output. This also is similar to method signatures in Java or C++:

<portType name="myEJBPortType"> <operation name="reverseString"> <input message="reverseStringRequest"/> <output message="reverseStringResponse"/> </operation> </portType>

Binding tag:
The concrete protocol and data format specifications used in the service reverseString are defined with the 'BINDING' parameter:

<binding name="rs" type="y:myEJBPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="reverseString"> <soap:operation soapAction="http://localhost/MyWebService"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding>

Define your services in WSDL and include the WSDL file in your existing J2EE war file. Redeploy the war file, and you will be able to view the published services with your browser. If you use the file that comes with this tutorial (MyWebService.wsdl), you can view the published WSDL file at http://localhost/MyWebService.wsdl.

Web Services Deployment Descriptor (WSDD)
Axis provides a custom Web services deployment descriptor (WSDD) for exposing the existing J2EE application APIs (classes) as Web services. The server-config.wsdd file exposes the example J2EE application into a Web service application.

The global configuration parameters define the request flow parameters that the Axis engine uses. Should you need to log the Web service requests, you can define your own JWSHandler:

<globalConfiguration> <parameter name="adminPassword" value="foobar"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration>

When a Web service request comes, the Axis engine uses the <service> tag mapping to identify the Java class to be invoked. If the provider is specified as java:EJB, org.apache.providers.java.EJBProvider instantiates and invokes the appropriate EJB. The EJBProvider class uses reflection to load and instantiate the Remote interface of the EJB.

As explained in the Invoking EJBs section, the EJBProvider class initializes the context, looks up the remote interface, and narrows the object using reflection:

<service name="MyWebService" provider="java:EJB"> <parameter name="allowedMethods" value="reverse,toUpperCase"/> <parameter name="className" value="mypkg.MyEJBBean"/> <parameter name="remoteInterfaceName" value="mypkg.MyEJB"/> <parameter name="beanJndiName" value="MyEJB"/> <parameter name="homeInterfaceName" value="mypkg.MyEJBHome"/> <!-- <typeMapping xmlns:ns1="http://localhost/"
qname="ns1:reverseStringType" type="java:java.lang.String"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> --> </service>

The main advantage that Axis offers users is its highly configurable nature. Though Axis comes with an optimal performance SAX parser, it allows you to write your own serializer and deserializer. If you decide to implement your own Serializer, simply specify its name in the Serializer tag.

Axis routes all the Web service requests to Axis Engine using AxisServlet. The Servlet mapping is defined in the J2EE web.xml file:

<servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>

The Servlet tag specifies the name of the Servlet and the Java class. Axis forwards URL patterns to the Servlet based on the servlet mappings. So the AxisServlet will process any request that has the pattern "/services/*" in its URL.

AxisServlet acts as a mediator between the Web request and the Axis Engine. It reads all the parameters from the Web request, constructs a SOAP request (XML), and hands the XML to the Axis Engine. The Axis Engine processes the request and returns the SOAP response message (XML). AxisServlet in turn writes the response back to the Web client.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date