Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

JAX-WS Web Services On Tomcat

In this tutorial we are going to see how to deploy JAX-WS Web Services on Tomcat. For this tutorial we are going to use Eclipse Eclipse 4.3 Kepler, which will also help us to construct the necessary WAR file for the Web Application.

In order to deploy a Web Service on Tomcat one should follow these steps:

  1. Frist of all you have to download Apache Tomcat.
  2. Copy JAX-WS RI jars in TOMCAT_HOME/lib folder
  3. Create a Dynamic Web Application in Eclipse.
  4. Create a JAX-WS Endpoint (Web Service interface, and Web Service implementation).
  5. Create a sun-jaxws.xml to define the Web Service implemenation class.
  6. Create a web.xml to describe the structure of the web project.
  7. Export WAR file from Eclipse and copy it to TOMCAT_HOME/webapps folder.
  8. Start Tomcat.

1. JAX-WS Dependencies in Tomcat

Tomcat will need some jars in order to deploy a JAX-WS Web Service. You have to go to : http://jax-ws.java.net/ and download JAX-WS RI library. Unzip the folder and copy the jars in TOMCAT_HOME/lib folder. If you don’t want to copy the complete library these are the jars that are necessary:

  • gmbal-api-only.jar
  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • management-api.jar
  • policy.jar
  • stax-ex.jar
  • streambuffer.jar

2. Create a Dyncamic Web Project in Eclipse

Open Eclipse IDE and go to File -> New -> Project -> Web -> Dynamic Web Project :

create-project

Then create a Project with name JAX-WS-Tomcat.

3. Service Endpoint

In order to create our Web Service Endpoint:

  • First you have to create a Web Service Endpoint Interface. This interface will contain the declerations of all the methods you want to include in the Web Service.
  • Then you have to create a class that actually implements the above interface, which will be your Endpoint implementation.

Web Service Endpoint Interface

WebServiceInterface.java:

package com.javacodegeeks.enterprise.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {

	@WebMethod
	String printMessage();

}

Web Service Endpoint Implementation

WebServiceImpl.java:

package com.javacodegeeks.enterprise.ws;

import javax.jws.WebService;

@WebService(endpointInterface = "com.javacodegeeks.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{

	@Override
	public String printMessage() {
		return "Hello from Java Code Geeks Server";
	}

}

4. Create the web.xml file

Go to WebContent/WEB-INF folder and create a new XML file .This is a classic web.xml file to deploy a Web Service.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.3//EN"
"http://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>sayhello</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>sayhello</servlet-name>
		<url-pattern>/sayhello</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
</web-app>

5. Create sun-jaxws.xml file.

You have to define the Service Endpoint Implementation class as the endpoint of your project, along with the URL pattern of the Web Service. Go to WebContent/WEB-INF folder and create a new XML file

sun-jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
	version="2.0">
	<endpoint name="WebServiceImpl"
		implementation="com.javacodegeeks.enterprise.ws.WebServiceImpl"
		url-pattern="/sayhello" />
</endpoints>

You can find more info in the JAX-WS Documentation.

This is the Eclipse Project Structure:

project-structure

6. Export WAR file

Now, go to the Package explorer and Right Click on the Project -> Export -> WAR file :

export-war

Now you have to save the WAR file:

war-export

After exporting the WAR file you have to copy it to TOMCAT_HOME/webapps folder. There are quite a few ways to create the WAR file. You can use Maven, Ant, or even the jar command line tool.

Now you can start Tomcat. Then put the following URL in your Web Browser :

http://localhost:8080/JAX-WS-Tomcat/sayhello

If everything is ok this is what you should get:

web-service-deployed

Now you can create a consumer of the Web Service like we did in previous tutorials like JAX-WS Hello World Example – RPC Style.

This was an example on how to deploy JAX-WS Web Services on Tomcat. Download the Eclipse Project of this example : JAX-WS-Tomcat.zip

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books