Deploy JAX-WS web services on Tomcat
Here’s a guide to show you how to deploy JAX-WS web services on Tomcat servlet container. See following summary steps of a web service deployment.
- Create a web service (of course).
- Create a sun-jaxws.xml, defines web service implementation class.
- Create a standard web.xml, defines
WSServletContextListener,WSServletand structure of a web project. - Build tool to generate WAR file.
- Copy JAX-WS dependencies to “${Tomcat}/lib” folder.
- Copy WAR to “${Tomcat}/webapp” folder.
- Start It.
Directory structure of this example, so that you know where to put your files.

1. WebServices
A simple JAX-WS hello world example.
File : HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(); }
File : HelloWorldImpl.java
package com.mkyong.ws; import javax.jws.WebService; //Service Implementation Bean @WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString() { return "Hello World JAX-WS"; } }
Later, you will deploy this hello world web service on Tomcat.
2. sun-jaxws.xml
Create a web service deployment descriptor, which is also known as JAX-WS RI deployment descriptor – sun-jaxws.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="HelloWorld" implementation="com.mkyong.ws.HelloWorldImpl" url-pattern="/hello"/> </endpoints>
When user access /hello/ URL path, it will fire the declared web service, which is HelloWorldImpl.java.
For detail endpoint attributes , see this article.
3. web.xml
Create a standard web.xml deployment descriptor for the deployment. Defines WSServletContextListener as listener class, WSServlet as your hello servlet.
File : 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>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>
4. WAR Content
Use Ant, Maven or JAR command to build a WAR file to include everything inside. The WAR content should look like this :
WEB-INF/classes/com/mkyong/ws/HelloWorld.class WEB-INF/classes/com/mkyong/ws/HelloWorldImpl.class WEB-INF/web.xml WEB-INF/sun-jaxws.xml
For those who are interested, here’s the Ant file to build this project and generate the WAR file.
File : build.xml
<project name="HelloWorldWS" default="dist" basedir="."> <description> Web Services build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <property name="webcontent" location="WebContent"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="war" depends="compile" description="generate the distribution war" > <!-- Create the war distribution directory --> <mkdir dir="${dist}/war"/> <!-- Follow standard WAR structure --> <copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" /> <copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" /> <jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/> </target> </project>
5. JAX-WS Dependencies
By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.
1. Go here http://jax-ws.java.net/.
2. Download JAX-WS RI distribution.
3. Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.
- jaxb-impl.jar
- jaxws-api.jar
- jaxws-rt.jar
- gmbal-api-only.jar
- management-api.jar
- stax-ex.jar
- streambuffer.jar
- policy.jar
6. Deployment
Copy the generated WAR file to {$TOMCAT}/webapps/ folder and start the Tomcat server.
For testing, you can access this URL : http://localhost:8080/HelloWorld/hello, if you see following page, it means web services are deploy successfully.







Thanks.This is excellent tutorial.
Hi
I am confused as to how you began this project in Eclipse. Is this project meant to be a “Dynamic Web Project” ? I have tried many different wizards i.e. Dynamic Web Project, Web Service, Java Project but yet I am not able to get the same directory structure as your example. Any help will be appreciated here.
Thanks
This should be dynamic web project, if some folders don’t exists , just create it :)
Hi,
Thanks very much for this tutorial this was very helpful in implementing the webservice.
However when we build a war file it creates in the following format.
HelloWorld-${DSTAMP}.war
so in that case in order to access the webpage this would be the url
http://localhost:8080/${warFileName}/hello
eg:
http://localhost:8080/HelloWorld-20120510/hello
Here the war file that is created after running the build is having its name as
HelloWorld-20120510.
This is how it extracts the war file in the webapps folder.
please correct me if any thing is wrong .
Thanks
Vishwanath
I appreciate your tutorial.
It will be more useful if you show this in a maven project as your most of the tutorials. So, that we will not face the dependency related issues.
Thanks for nice tutorial with perfect example !!!
I have a question:
Intially, I have developed a web service using JDK1.5.
Now, I would like to upgrade it to jdk1.6. Will there be any change the wsdl generated?
Please let me know as soon as possible.
Great tutorial, this was extremely helpful!
Thank you for this clear and well laid out tutorial. It took me days to get a webservice up a running and all that was missing was the jar files at the end of the tutorial in tomcat/lib. Didn’t see that explain on any other site.
Thanks for your help, keep up the good work!
Thank you for this great tutorial!
If someone tries to start the tomcat and gets an error like “BackingStoreException… class not found…”, download the ha-api.jar and copy it in the {$tomcat}/lib order and try it again.
Really thanks for this clear, simple, and useful tutorial and example, it saved me a lot of time and took me to the point
Hi,
thank you for this really good tutorial.
I have a problem in the final step. I putted the war-File in the webapps-folder and started tomcat. The war-File is now unpacked in the webapps-folder, but I can’t open the URL in browser (HTTP Status 404). There were no errors during the start of tomcat.
Hope someone can help me. Thanks!
Please access tomcat admin page, to view the list of deployed app and its url.
the same problem. I open admin page:
/HelloWorld-20101123 false
and I cannot start service.
url for your example should be like this:
http://localhost:8080/HelloWorld-20101123/hello
and also i include all jars from jax-ws to the tomcat lib.
After that example begin working.
hello
plaese help me
i want implement ecommerce with metro web services
I dont know what i do?
Hi,
thank you for the excellent tutorial!
Perhaps only a supplement, I had to copy the ha-api.jar to Tomcat library folder.
Geo
Hi!
I am trying to run this example uisng java6 and Tomcat6 but when I compile in linux I get this error:
root@squezze:~/wshex# javac src/com/mkyong/ws/HelloWorldImpl.java
src/com/mkyong/ws/HelloWorldImpl.java:8: cannot find symbol
symbol: class HelloWorld
public class HelloWorldImpl implements HelloWorld{
^
src/com/mkyong/ws/HelloWorldImpl.java:10: method does not override or implement a method from a supertype
@Override
^
2 errors
Anybody has a cluee ?
May be packaging error at your environment. Attached is an Eclipse project with ant build tool, try build with ant tool.
Hi Team,
I’m getting following error when i tried to run the client , could you help me ?
Exception in thread “main” javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:8666/HelloWorld/hello?wsdl. It failed with:
Connection refused: connect.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.(Service.java:92)
at javax.xml.ws.Service.create(Service.java:722)
at com.mkyong.client.HelloWorldClient.main(HelloWorldClient.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
Thanks a lot for the tutorial!
However when I tried this on Eclipse I need to add more one jar file from JAX-WS RI distribution, that is ha-api.jar. :D
Hi your blog is very useful,but when i place war file in webapps,it is not automaically deployed.when i press start it shows the error “FAIL – Application at context path /HelloWorld could not be started”.what is the issue
[...] How to deploy JAX-WS on Tomcat [...]
Thanks for reply..
One more problem I am facing while creating war..
It is not able find the JAR file refference when i run ‘ant war’ command.
Getting Error:
package javax.jws does not exist
import javax.jws.WebMethod;
How can I give refference while creating war?
Also I have stored all jars in folder called ‘lib’ and I have adde following comd in build file
But still it is not able to get that jars..
Please help me on the same…
-Mahendra
Short and sweet explanation….
My problem is I am not able to find the above given dependency jars, pls can you help me.
-Mahendra
Refer to JAX-WS hello world, http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/
Actually, the web service dependency jars are bundle with the JDK1.6 , or you can try Metro
Thanks for reply..
One more problem I am facing while creating war..
It is not able find the JAR file refference when i run ‘ant war’ command.
Getting Error:
package javax.jws does not exist
import javax.jws.WebMethod;
How can I give refference while creating war?
Also I have stored all jars in folder called ‘lib’ and I have adde following comd in build file
But still it is not able to get that jars..
Please help me on the same…
-Mahendra
if I don’t prefer to Metro, how can I work with only JDK1.6? that is, Which jars copy to tomcat?
Read this article – JAX-WS hello world , it contains all the required jax-ws dependency libraries.
I was able to complete this tutorial, Deploy JAX-WS web services on Tomcat, successfully and I wanted to say thank you. But I had one question. When I clicked on the WSDL link I do NOT see the names of the params within the but only the following and I needed to know how to get the names of each param instead of arg1 – arg14 that was depicted in the WSDL? Here is what came out in the WSDL:
-
And within the :
-
Any help/direction would be greatly appreciated. Thank you.
I finally found the problem and my issue is now resolved. Thanks anyway.
Glad to know you solved it. Mind to share how to did it?
Can’t see your WSDL detail?
Short but everything. Excelent!!
[...] See this detail guide on how to deploy JAX-WS web services on Tomcat. [...]
[...] Deploy it like a normal web service, see this guide – Deploy JAX-WS web services on Tomcat servlet container. [...]
Hi,
I like your site …
Nice and clear explanation…..
Keep it up.
Thanks for your kind feedback.
you specifies @SOAPBinding(style = Style.RPC)
i need to know what are styles are applicable and i need an example for Rest style webservice example , and what are the main difference between soap based webservice and REST based web service and how to perform authentication in soap style and rest style
thanks, I’ll try if there is trouble I will be asking his opinion.
Hi,
did you try to use JAX-WS with digital sinature? I mean solution described in Metro guide: http://jax-ws.java.net/guide/Configuring_Keystores_and_Truststores.html and it doesn’t work for me.