Web Services Example Using Glassfish 2

The aim of this java web-service tutorial is to write a simple 'Hello World' web service and client in Java. This tutorial will be using the jax-rpc framework in the Glassfish 2 application server. We will create a web-serive and deploy it in Glassfish. Then we will connect to it using a java client. The files and structures are exactly the same as the JWSDP 2 tutorial. In fact you could deploy the war file from that tutorial in Glassfish and it would work fine. Leave any questions or problems as comments and I will endeavour to answer them.

This article assumes that you have Galssfish 2 installed and a domain configured. Please see Installing and Setting up Glassfish 2 for more details. For purposes of this example the glassfish domain is running on port 8082 and the project directory is GlassfishWSHelloWorld.

Versions used in this example
Windows XP SP2
Glassfish 2
Java JDK 1.5.0
Links to these files can be found here

Although this is a quick 'helloworld' tutorial we will not sacrifice neatness for speed - so all the source files, class files etc will be kept in separate directories and as clean as possible. You will thank me for this later.

Create this directory structure in your working directory- similar to something you would get from eclipse. The structure used in this example is given below. GlassfishWSHelloworld will be referred to as your project or working directory. Create this structure.


The Glassfish app server is installed in the D:\downloads\glassfish directory for this tutorial.

Compiling the webservice code
  1. Write the code
    We don't really need an interface for this example, but I have added one for completeness, because we will use these exact same files for the JWSDP example. In this tutorial the HelloCallEcho function will be exposed.
     1. package helloworld;
     3. import java.rmi.RemoteException;
     5. public interface HelloWorldServiceInterface extends java.rmi.Remote{
     6.     public String HelloCallEcho(String hellostring)throws java.rmi.RemoteException;
     7.     public int HelloCallAdd(int helloNumber1, int helloNumber2)throws java.rmi.RemoteException;
     8. }
    Hide line numbers
    Save this as HellowWorldServiceInterface.java in your web_src directory.

    Next, create the implementation
     1. package helloworld;
     3. public class HelloWorldServiceImplementation implements HelloWorldServiceInterface{
     5.     public String HelloCallEcho(String hellostring) {
     6.         return "Web service echoing back - "+hellostring;
     7.     }
     9.     public int HelloCallAdd(int helloNumber1, int helloNumber2) {
    10.         return helloNumber1 + helloNumber2;
    11.     }
    12. }
    Hide line numbers
    Save this as HelloWorldServiceImplementation.java under in your web_src directory
  2. `
  3. Now compile the code.

    ..workspace\GlassfishWSHelloWorld>javac -d web_bin\WEB-INF\classes web_src\*.java

  4. `
  5. Open a command prompt and cd into your project directory (GlassfishWSeHelloWorld in this example).Your web_bin directory should have some class file in there.
  6. `
  7. Create the jaxrpc-ri.xml file in the web_bin\META-INF directory.
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <webServices xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd"
     3.     version="1.0"
     4.     targetNamespaceBase="http://hello.org/wsdl"
     5.     typeNamespaceBase="http://hello.org/types"
     6.     urlPatternBase="/ws">
     8.     <endpoint name="JWSDPGlassHelloWorld" 
     9.         displayName="Hello World from JWSDP 2" 
    10.         description="This is a Hello World Echo service"
    11.         wsdl="/WEB-INF/JWSDPGlassHelloWorld.wsdl"
    12.         interface="helloworld.HelloWorldServiceInterface"
    13.         implementation="helloworld.HelloWorldServiceImplementation"/>
    15.     <endpointMapping
    16.         endpointName="JWSDPGlassHelloWorld"
    17.         urlPattern="/hello"/>
    19. </webServices>
    Hide line numbers

  8. `
  9. Create the web.xml file in the same WEB-INF directory.
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <web-app>
     3.     <display-name>Hello Service</display-name>
     4.     <description>
     5.         Returns a String saying Hello
     6.     </description>
     8.     <session-config>
     9.         <session-timeout>60</session-timeout>
    10.     </session-config>
    12. </web-app>
    Hide line numbers

  10. `
  11. Cd into web_bin directory and jar it all up using,

    ..workspace\GlassfishWSHelloWorld\web_bin>jar -cvf HelloWorldServices.jar *

  12. `
  13. Now use the wsdeploy tool to create the war file that will be deployed to tomcat
    ..workspace\GlassfishWSHelloWorld\web_bin>d:\downloads\glassfish\bin\wsdeploy.bat -o GlassfishWSHelloWorld.war HelloWorldServices.jar

Deploying the Service
  1. Open up your broswer and go to the Glassfish admin console. This is for this tutorial. See Installing and Setting up Glassfish 2 for more details.
  2. `
  3. Login as admin to the console.
  4. `
  5. Deploy the GlassfishWSHelloWorld.war application under applications -> web applications -> deploy.
  6. `
  7. Now navigate to '' You should see a table with some links. Click on the WSLD link to have a look at the WSDL.

Generating the stub code - required for the client.
  1. Create the config.xml file in your client_src directory. Wscompile will use this to find the wsdl to create the stubs,
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <configuration
     3.     xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
     4.     <wsdl
     5.         location=""
     6.         packageName="helloworld">
     7.     </wsdl>
     8. </configuration>
    Hide line numbers

  2. `
  3. Open a prompt and cd to your working directory.
  4. `
  5. Generate the client stub by running wscompile.bat. This should be in your jwsdp install directory under 'jaxrpc\bin.' We use -s option to install all the stub code in the client_src directory.
    workspace\GlassfishWSHelloWorld>D:\downloads\glassfish\bin\wscompile.bat -gen -keep -d client_bin -s client_src client_src\config.xml

Compiling the client
  1. Write the actual client code and save this as GlassfishWSHelloWorldClient.java under the client_src directory.
     1. import helloworld.*;
     3. public class GlassfishWSHelloWorldClient
     4. {
     5.     public static void main (String args[])
     6.         throws Exception
     7.     {
     8.         JWSDPGlassHelloWorld_Impl service =  new JWSDPGlassHelloWorld_Impl();
     9.         HelloWorldServiceInterface stub = service.getHelloWorldServiceInterfacePort();
    10.         String response = stub.helloCallEcho("client says hello JWSDP");
    11.         System.out.println ( response );
    12.     }
    13. }
    Hide line numbers

  2. `
  3. Now compile it using

    ..workspace\GlassfishWSHelloWorld\client_src>javac -d ..\client_bin -Djava.ext.dirs=D:\downloads\glassfish\lib GlassfishW
    This should place all the files into the 'client_bin' directory

Running the application
  1. CD into client_bin and run the client using the command below,
    ..workspace\GlassfishWSHelloWorld\client_bin>java -Djava.ext.dirs=D:\downloads\glassfish\lib GlassfishWSHelloWorldClient
    You should see a printout saying 'hello back' from the web service.

Looking at the soap messages using tcpmon
The way the monitor works is like a proxy. Your client connects to the monitor and the monitor connects to the web-service. Remember that Glassfish is running on port 8082
  1. Go back and edit the HelloworldServiceInterface_Stub.java. This is one of the files generated by wscompile and should be under the client_src\helloworld directory. Around line 40. Change the port to 8083 and save.
    public HelloWorldServiceInterface_Stub(HandlerChain handlerChain) {
    _setProperty(ENDPOINT_ADDRESS_PROPERTY, "");
    public HelloWorldServiceInterface_Stub(HandlerChain handlerChain) {
    _setProperty(ENDPOINT_ADDRESS_PROPERTY, "");

  2. `
  3. Compile the client code as explained previously.
  4. `
  5. Open up another seprate command prompt and start up tcpmon.

    ...\anywhere>java -cp D:\downloads\axis-bin-1_4\axis-1_4\lib\axis.jar org.apache.axis.utils.tcpmon
    Please refer to the final section in the Setting up Axis2 on Tomcat for more details on this step and why we are using the axis.jar core from Axis 1. This should startup tcpmon.
  6. `
  7. In the tcpmon gui change the target port from 8080 to 8082, in the listen port type in 8083 and press add.
  8. `
  9. Go to the 8083 tab at the top.
  10. `
  11. Now open up a command prompt and run the recompiled client as explained in the previous section- You should see SOAP messages sent back and forth.

Back to the tutorial trail | Home.


wtb said...

Good to see web app creation example from the bottom, so often the IDE covers it up

Sourav said...

the deployment gives the following warning:
rror occurred during application loading phase. The application will not run properly. Please fix your application and redeploy. WARNING: com.sun.enterprise.deployment.backend.IASDeploymentException: ContainerBase.addChild: start: LifecycleException: javax.servlet.ServletException: JAXRPCSERVLET50: caught throwable during servlet initialization: JAXRPCSERVLET01: class not found: helloworld.HelloWorldServiceInterface com.sun.enterprise.deployment.backend.IASDeploymentException: ContainerBase...

also giving the 404 error

Naviya Nair said...

Very interesting and good Explanation
ASP NET Training
ASP NET Training
ASP NET Online Training
C-Sharp Training
Dot Net Training in Chennai
Online .Net Training

MVC Training
WCF Training
Web-API Training
LINQ Training
Entity Framework

Dot Net Interview Questions

Thanu Abi said...

Truely a very good article on how to handle the future technology.In this reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. keep sharing your information.This details creates a new hope for me.It is totally awesome information.

java training in chennai

Training Chennai said...

Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
JAVA Training | Multimedia Training in Chennai