JAX-WS + Java Web Application Integration Example
Often times, JAX-WS always be part of your Java web application. Here we show you how to integrate JAX-WS into Java web application easily.
1. Project Folder
First, review this project folder structure.

2. Web Service
A super simple web service. Code is self-explanatory.
File : HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloWorld{ @WebMethod(operationName="getHelloWorld") public String getHelloWorld(String name) { return "Hello World JAX-WS " + name; } }
3. Web Service Deployment Descriptor (sun-jaxws.xml)
Create a web service deployment descriptor, named 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="HelloWorldWs" implementation="com.mkyong.ws.HelloWorld" url-pattern="/hello"/> </endpoints>
4. Web Application Deployment Descriptor (web.xml)
In the standard web.xml,
- Defines “
com.sun.xml.ws.transport.http.servlet.WSServletContextListener” as listener class. - Defines “
com.sun.xml.ws.transport.http.servlet.WSServlet” as your web service (hello) servlet.
File : web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <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> </web-app>
5. Done
The integration between JAX-WS and web application is done. Deploy it and access via URL : http://localhost:8080/WebServicesExample/hello







im getting the following error when performing mvn tomcat:redeploy in tomcat
SEVERE: Error listenerStart
May 17, 2012 12:34:06 AM org.apache.catalina.core.StandardContext start
SEVERE: Context [/WS_mkyong_web] startup failed due to previous errors
any idea, why?
here is the pom.xml that im using..
thanks in advance
ok, we are missing the following dependency from pom.xml
Thanks MK
Good tutorial. however, I don’t know how to call the web method in the web service.
Do I need to use wsimport tool to generate the artifact and invoke the web method in servlet. THanks.
that would be a helpful tutorial. thanks MK
java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
I use:
– JDK 1.6_26
– Eclipse Indigo
Is there no other solutions???
Thanks.
Fernando Aspiazu
http://www.mkyong.com/webservices/jax-ws/java-lang-classnotfoundexception-com-sun-xml-ws-transport-http-servlet-wsservletcontextlistener/
Attractive portion of content. I just stumbled upon your site and in accession capital to claim that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing for your feeds or even I success you get admission to consistently quickly.
[...] JAX-WS + Java Web Application Integration Example [...]