"Oracle Certified Master, Java EE 5 Enterprise Architect" Certified. TOGAF 9 Foundation Certified. Software Architect with more than 8 years of professional hands-on software development experience; utilizing best practices, Object Oriented designs and open standards in developing Portals and enterprise multi-tier applications. BSc from Jordan University faculty of Engineering & Technology – Electrical Engineer Department. Mohammad is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

JAX-WS Deployment Five Minute Tutorial

03.30.2010
| 49623 views |
  • submit to reddit

After we explained how we can implement a JAX-WS web service (endpoint, client) in the JAX-WS Five Minute Tutorial, we will continue by explaining how we can deploy the web service endpoint on any application server... and here we'll use Tomcat.

To deploy your WS endpoint you need to package it as a war first then deploy it on your application server.

Note:- You can download the source code for this example from the resources section.

Ok, lets begin

 1) Open Eclipse.

2) Create a new Web project .

3) Create your Web Service interface (Greeting):

package juma.mohammad;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface Greeting {
@WebMethod String sayHello(String name);
}

 4) Create your Web Service implementation (GreetingImpl):

package juma.mohammad;

import javax.jws.WebService;

@WebService(endpointInterface = "juma.mohammad.Greeting")
public class GreetingImpl implements Greeting {

@Override
public String sayHello(String name) {
return "Hello, Welcom to jax-ws " + name;
}

}

 5) Now , you need generate Web Services classes, open your command line, and type :

cd %project_home%
wsgen -s src -d build/classes -cp build/classes juma.mohammad.GreetingImpl

Good , now you have two classe(SayHello.java, SayHelloResponse.java) generated under /greetingWS/src/juma/mohammad/jaxws  .

6) Now we need to write our web.xml and put it under /greetingWS/WebContent/WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>GreetingWS</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>GreetingWS</servlet-name>
<url-pattern>/greeting</url-pattern>
</servlet-mapping>
</web-app>

Note that in this web.xml we  just defined two things : 1) listener-class, 2)servlet !

7) ok,the final step is that you need to add sun-jaxws.xml under /greetingWS/WebContent/WEB-INF  which contains endpoints definition:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint
name="GreetingWS"
implementation="juma.mohammad.GreetingImpl"
url-pattern="/greeting"/>
</endpoints>

8) You need to download JAX-WS library and put jars under /greetingWS/WebContent/WEB-INF/lib.

You can get jars from the attached sample :)

9) Great, now you just need to export this project as a war, and drop it under your Tomcat webapps folder .

10) Run Tomcat.

11) Try this url: http://localhost:8080/greetingWS/greeting

Congratulations... web service information page appeared :)

Legacy
Article Resources: 
Published at DZone with permission of Mohammad Juma, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Fayez Alrafeea replied on Tue, 2010/03/30 - 7:57am

Thank you..

I found it very simple and useful.

Josh Chappelle replied on Wed, 2010/03/31 - 12:56pm

Great article. It would be great if you could post an article about implementing security with the jax-ws library.

Sebastián Open replied on Thu, 2010/04/01 - 4:04pm

great article, I wish I had discovered it before, because I had to inspect what netbeans did to discover it all... here I document the process BTW netbeans just deploys the following libraries: activation.jar
webservices-api.jar
webservices-extra.jar
webservices-extra-api.jar
webservices-rt.jar
webservices-tools.jar
where have you downloaded JAX-WS libraries from? saludos sas

Mohammad Juma replied on Sun, 2010/04/04 - 1:12am

Hi sas,

I am really happy coz u found this article useful :)

u can download jax-ws library from :https://jax-ws.dev.java.net/ri-download.html

Tom Healy replied on Mon, 2011/03/21 - 12:28pm

Hi Mohammad,

I deployed the webservice to tomcat (jakarta tomcat 5.0.27) and got the error...

Provider org.apache.xalan.processor.TransformerFactoryImpl not found

I removed the xml-apis.jar from common/endorsed and tried again this time with no error. Howver, when I trya and access the service with the url http://localhost:8080/greetingWS/greeting I get a http 404. Any idea what I am doing wrong?

Thanks Tom

Tom Healy replied on Tue, 2011/03/22 - 4:39am

Please disregard previous post. My webapp is 'WS-Serverweb' and not 'greetingWS' so my url was incorrect. looks good!  Thanks, Tom.

Nat Do replied on Tue, 2011/06/21 - 2:14pm

this is so awesome.

i create a webservice my own.

 

many thanks for awesome tutor

Mohammad Juma replied on Sun, 2011/07/03 - 8:50am

I am happy that this article helped you "Nat" :)

Edwin Jaws replied on Sat, 2011/07/30 - 2:49pm

Hi ! When I try to compile both classes I get this error symbol: class Greeting public class GreetingImpl implements Greeting { ^ GreetingImpl.java:8: method does not override or implement a method from a supertype @Override ^ 2 errors I dont know why ? Any cluees?

Jose Alvarez replied on Tue, 2012/02/07 - 11:16am

Thanks, Mohammad, a very good and useful article. Keep on the good work !

Mohammad Juma replied on Tue, 2012/04/17 - 7:59am in response to: Jose Alvarez

Thanks Jose :)

Attila Vangel replied on Fri, 2012/11/30 - 10:57am

Thanks, this is quite useful, although there is an issue I can't solve.

You wrote:

" 11) Try this url: http://localhost:8080/greetingWS/greeting"

but what can I do when I want the Web Service to be available in an url with only one / (slash character in it), e.g. http://localhost:8080/greetingWS ?

The problem is that in this exampe 'greetingWS' comes from the .war file name, and /greeting is specified in web.xml and sun-jaxws.xml. I tried to set '/greeting' to '/' but then it would listen on http://localhost:8080/greetingWS/ and not in http://localhost:8080/greetingWS and that's not what I want.

Maybe the solution would be to somehow magically reconfigure tomcat or subclass WSServlet and hack it somehow? I would prefer a solution where I don't have to reconfigure tomcat just because of this.

Please help if you can.

Smitha Pitla replied on Tue, 2013/07/09 - 2:41pm

Hi,

After publishing the endpoint and creating the stubs, I made changes to the implementation class. What do I do to make these changes reflect in the newly generated stubs. Re-publishing wasn't the option as it kept complaining about Adress already in use:bind

if I restart the laptop it might work, but I am looking for a quicker resolution. Sorry this comment was meant for the first article on JAX-WS

Attila Vangel replied on Wed, 2013/07/10 - 3:23am in response to: Smitha Pitla

If you want to develop a WS probably you are looking for a tutorial like this:
http://www.wikijava.org/wiki/Debugging_a_servlet_with_tomcat_and_Eclipse_tutorial

Of course, it's always a very good practice to unit test your code first (e.g. with junit and mockito or easymock or whatever).

But if you simply want to run a newer version of your application in a standalone Tomcat, I would undeploy the old .war file and deploy the new one.

Hope that this answers your question.

Naveen Narang replied on Wed, 2014/04/23 - 12:34am

Hi Mohammad,

Do you have a tutorial which invoke this deployed web service from Javascript?

My client side code is in Javascript and I need to call the webservice from client side.

I was wondering if you have a tutorial for that too.

Thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.