Home > web service > Develope & deploy a simple web service

Develope & deploy a simple web service

In this article, a web service is created without IDE and is deployed into Glassfish server in the form of war file. The purpose of doing so is to gain more independence when deploying services.
Meanwhile, a client is built using NetBeans IDE’s benefits to achieve fast development. The client can be distributed and run on the computer equipped with JVM.

The files related to this article can be found in this zip file (all.zip).

Server side
(Attached with source codes and the war file, the Glassfish will auto-deploy the war file)

1 Initial Setup

 1.1 JAVA_HOME (if you are already using Java, skip this step)

 1.1.1  Download and install Java JDK 6
http://java.sun.com/javase/downloads/index.jsp

 1.1.2  set an environment variables, named JAVA_HOME, point it to the installation directory of Java. (to set environment variables in Windows, right click “my computer” > properties > advanced > environment variables )
 
1.2 AS_HOME

1.2.1  Download Application Server. (I am using the Glassfish v2.1)
Download: http://java.sun.com/javaee/glassfish/getit.jsp

1.2.2  set an environment variables, named: AS_HOME
to the installation directory of Glassfish Server
for example, my server is installed at E:toolsglassfish-v2.1
 
1.3 ANT_HOME

set an environment variables, named: ANT_HOME, to the installation directory of Apache Ant. If you have already been using ANT and have set the ANT_HOME, just skip this step; if you haven’t got the Apache ANT on your computer, you can find it is part of Glassfish Server. In Windows, Ant resides on libant. Thus, ANT_HOME can be defined as %AS_HOME%libant; then append %ANT_HOME% to the environment variable PATH
 
1.4 use command line to start server

(For UNIX)  $AS_HOME/bin/asadmin start-domain domain1
(For Windows)  %AS_HOME%/bin/asadmin start-domain domain1
 
Here is an example of the output:
Starting Domain domain1, please wait.
Default Log location is E:toolsglassfish-v2.1domainsdomain1logsserver.log
The domain (domain1) is already running.
 
 
So, what is a domain? From Glassfish v2.1 reference manual: http://docs.sun.com/app/docs/doc/820-4332
A domain provides a common authentication and administration point for a collection of zero or more server instances. The administration domain encompasses several manageable resources, including instances, clusters, and their individual resources. A manageable resource, such as a server instance, may belong to only one domain.
 
2 Development and Deployment

2.1 Development

I create simply web service HelloImpl, in which there is a method sayHello takes a String as input, and return this String with prefix and postfix.
 
This is the source code:

  1. package service;
  2. import javax.jws.WebService;
  3.  
  4. @WebService
  5. public class HelloImpl {
  6.   /**
  7.   * @param name
  8.   * @return Say hello to the person.
  9.   */
  10.   public String sayHello(String name) {
  11.     return "Hello, " + name + "!";
  12.   }
  13. }
  14.  

2.2 Deployment

All the works of compiling, generating war file is done by the ant script. You can find a war file in the “war/” folder and it can be deployed to the server. I insert sun-web.xml and web.xml in the folder WEB-INF manually (Actually, it is done by ant script, see it in the source code).
 
any problems will be recorded in the log file, it is helpful when debugging on deployment: %AS_HOME%/domains/domain1/logs/server.log
 
Now we have a war file called Helloworld.war. There are two ways to deploy it:
1. use http://localhost:4848/, click Applications > Web Applications > deploy
2.
 copy the file into %AS_HOME%domainsdomain1autodeploy
 
Now we have deployed the service, the Endpoint Address URI: /HelloWorld/HelloImplService
So the WDSL is: http://localhost:8080/HelloWorld/HelloImplService?wsdl
 
Client side
(The client is developed in Netbeans, attached with project source and jar file)

The client is designed to take a string from user and send it to the web service we have just created. If the JVM is installed on the machine, you can type in the command line, “Jin” is the parameter for you to assign:
java -jar HelloWorldClient.jar Jin
 
The result should be:
I send: Jin
Result = Hello, Jin!

This is the main method in the source code:

  1. public static void main(String[] args) {
  2.  
  3.     try { // Call Web Service Operation
  4.         service.HelloImplService service =
  5.             new service.HelloImplService();
  6.         service.HelloImpl port = service.getHelloImplPort();
  7.         // initialize WS operation arguments here
  8.         java.lang.String arg0 = args[0];
  9.         // process result here
  10.         System.out.println("I send: "+ arg0);
  11.         java.lang.String result = port.sayHello(arg0);
  12.         System.out.println("Result = "+result);
  13.     } catch (Exception ex) {
  14.         ex.printStackTrace();
  15.         System.exit(1);
  16.     }
  17. }

Finally, the client will call the web service on the “localhost”. Future works are:
1. Let client use properties files to store the server’s location;
2. Let server use database technique to maintain the information;
3. Use objects as input and output parameters.

References:
Managing and Monitoring Web Services in Project GlassFish
http://developers.sun.com/appserver/reference/techart/ws_mgmt.html
Using the IDE to develop a JAX-WS web service.
http://www.netbeans.org/kb/docs/websvc/jax-ws.html

Great appreciations to the helps from Arif (University of Melbourne) and Nathan (Sun).
Development and deployment of a simple web service and a client consuming it.
Jin Li, auswer[at]hotmail.com

Jin Li web service , , , , , ,

  1. June 6th, 2009 at 08:44 | #1

    Of sense because the standards for Web services development already exist … Web Service

  2. June 12th, 2009 at 21:31 | #2

    Hi, very nice post. I have been wonder’n bout this issue,so thanks for posting

  3. June 15th, 2009 at 01:37 | #3

    Great post! I’ll subscribe right now wth my feedreader software!

  4. June 21st, 2009 at 01:45 | #4

    Ugh, I liked! So clear and positively.
    Thank you
    AlexAxe

  5. webservice developer
    December 22nd, 2009 at 05:13 | #5

    this information was very helpful

  6. webservice developer
    December 22nd, 2009 at 05:15 | #6

    @webservice developer
    especially all.zip file

  1. No trackbacks yet.