×
☰ See All Chapters

Deploy JAX-WS web services on Tomcat

In our previous chapters we published our web service in using javax.xml.ws.Endpoint. In practical Endpoint can be used as a quick test tool and cannot be used to deploy and publish real time applications. Real time web services should be published on servlet container. In this tutorial we will learn how to create a web service and deploy it on Apache Tomcat server.

For this, our first step is to create a Dynamic Web Project to write our business logic. Follow the below steps to learn to deploy and publish web services on Tomcat server.

Step 1: Create a dynamic web project.

deploy-jax-ws-web-services-on-tomcat-0
 

Step 2: Download the jar files and add to WebContent/WEB-INF/lib folder

Download from: https://jax-ws.java.net/

(Google search key word “jax-ws jar download”)

deploy-jax-ws-web-services-on-tomcat-1
 
deploy-jax-ws-web-services-on-tomcat-2
 
deploy-jax-ws-web-services-on-tomcat-3
 

Extract the downloaded zip file and find the jars inside lib folder. Copy all the jars into “WebContent/WEB-INF/lib ” folder.

deploy-jax-ws-web-services-on-tomcat-4
 

Step 3: Develop SEI and SEI implementation classes.

deploy-jax-ws-web-services-on-tomcat-5
 

HelloWorld.java

package com.java4coding;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import javax.jws.soap.SOAPBinding.Style;

 

@WebService

@SOAPBinding(style = Style.RPC)

//@SOAPBinding(style = Style.DOCUMENT)

public interface HelloWorld{

        @WebMethod

        String getHelloWorldAsString(String name);

}

 

HelloWorldImpl.java

package com.java4coding;

 

import javax.jws.WebService;

 

//Service Implementation

@WebService(endpointInterface = "com.java4coding.HelloWorld")

public class HelloWorldImpl implements HelloWorld{

 

        @Override

        public String getHelloWorldAsString(String name) {

                return "Hello World JAX-WS " + name;

        }

}

Step 4: Create a web service deployment descriptor, which is also known as JAX-WS RI deployment descriptor “sun-jaxws.xml”, place this under WebContent\WEB-INF folder.

deploy-jax-ws-web-services-on-tomcat-6
 

sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>

<endpoints

  xmlns="https://java.sun.com/xml/ns/jax-ws/ri/runtime"

  version="2.0">

  <endpoint

      name="HelloWorld"

      implementation="com.java4coding.HelloWorldImpl"

      url-pattern="/hello"/>

</endpoints>

When user access /hello URL path, it will fire the declared web service, Which is  HelloWorldImpl.java

Step 5: Open web.xml and configure com.sun.xml.ws.transport.http.servlet.WSServletContextListener as listener class and com.sun.xml.ws.transport.http.servlet.WSServlet with url pattern “/hello”

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,

Inc.//DTD Web Application 2.3//EN"

"https://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

 

<web-app>

    <listener>

        <listener-class>

                com.sun.xml.ws.transport.http.servlet.WSServletContextListener

        </listener-class>

    </listener>

    <servlet>

        <servlet-name>hello</servlet-name>

        <servlet-class>

                com.sun.xml.ws.transport.http.servlet.WSServlet

        </servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>hello</servlet-name>

        <url-pattern>/hello</url-pattern>

    </servlet-mapping>

    <session-config>

        <session-timeout>120</session-timeout>

    </session-config>

</web-app>

Step 6: Now deploy the project into server and hit the service with following URL

https://localhost:8090/JAX-WS_UsingTomcatServer/hello

Now you should see as below:

deploy-jax-ws-web-services-on-tomcat-7
 

With the URL https://localhost:8090/JAX-WS_UsingTomcatServer/hello?wsdl

We should see WSDL document as follows….

 

 

 

 

deploy-jax-ws-web-services-on-tomcat-8
 

NOTE:

 In the above application we are not creating any server side artifacts. When we deploy application into server, server itself will create the server side artifacts(If server is compatible).

 Some servers if our web service is having the following SOAP binding style, it won’t create server side artifacts.

 @SOAPBinding(style = Style.RPC , use= Use.LITERAL)

If server unable to create server side artifacts we will get the following exception, when we are publishing our web service.

deploy-jax-ws-web-services-on-tomcat-9
 

So next steps are required if we want to create server side artifacts on our own. Using wsgen tool we need to create server side artifacts.  When artifacts are generated, move WSDL and xsd files into inside WebContent\WEB-INF folder.  Keep the java artifacts generated in com.manum.hassan.jaxws package.

Even though artifatcs are generated manually, still sun-jaxws.xml file is needed and web.xml file should be configured with WSServletContextListener and WSServlet.

 

 


All Chapters
Author