The J2EE platform is designed to provide server-side and client-side support for developing Web services and distributed, multi-tier enterprise applications. Web Services can provide unlimited functionalities by connecting with an existing service API or creating your own.
If you can efficiently develop and deploy distributed applications and Web services for use on both corporate intranets and over the Internet, you will gain competitive advantage.
J2EE
The J2EE platform is architected to facilitate the deployment of multi-tier distributed applications and Web services. The platform defines different tiers, including a client tier, one or more middle tiers, and a back-end tier. The J2EE platform supports component-based development of applications and services. The portions of application logic that reside on separate tiers can make use of the different components provided by the platform for each tier. To ensure that the components interact in a standard way, the platform introduces the concept of container-based component management. Components run within containers, which are standardized runtime environments that provide specific services to components and thus ensure application portability across platform implementations.
I assume that you already have J2EE platform on your servers, if not you can access it on with downloadable link and installation guide http://www.oracle.com/technetwork/java/javaee/download-141771.html
Web Services
As Web services can be used with any application or technology, the services should be written in such a way that it can communicate with any software, application and technology. The four basic standards required in web services are:
- Who make services available, and service requestors, who use services, must be able to write Web services using Extensible Markup Language (XML).
- Simple Object Access Protocol (SOAP) provides a common message format for Web services.
- All service providers need to have a common format to specify service details, such as the service type, how to access the service, and so forth, which is written in Web Services Description Language (WSDL).
- Service requestors must have a common way to look up and obtain details of a service. This is accomplished by having common, well-known locations where providers can register their service specifications and where requestors know to go to find services. Universal Description, Discovery, and Integration (UDDI) specification defines a common means for looking up Web services.
Communication between Java Client and Java Web Services on J2EE Platform
In a Web service scenario, a client makes a request to a particular Web service, such as asking for the weather at a certain location, and the service, after processing the request, sends a response to the client to fulfill the request. When both the client and the Web service are implemented in a Java environment, the client makes the call to the service by invoking a Java method, along with setting up and passing the required parameters, and receives as the response the result of the method invocation.
Once the client knows how to access the service, the client makes a request to the service by invoking a Java method, which is passed with its parameters to the client-side JAX-RPC runtime. With the method call, the client is actually invoking an operation on the service. These operations represent the different services of interest to clients. The JAX-RPC runtime maps the Java types to standard XML types and forms a SOAP message that encapsulates the method call and parameters. The runtime then passes the SOAP message through the SOAP handlers, if there are any, and then to the server-side service port.
Java API
Java 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.
Implementation of JAX-RPC Service Endpoint Interface for the weather service interface using a Web component will be like:
public interface WeatherService extends Remote {
public String getWeather(String city) throws RemoteException;
}
JAX-RPC Service Implementation will be like:
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() {}
}
A Java/J2EE Client Accessing the Weather Service will be:
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("San Francisco");
This way you need to code very less and yet you can use lot of functionalities in your applications. The equivalent EJB service endpoint implementation for the same 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() {}
}
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.
There are other ways to write the services code as we will so start from the simple one and then explore more.