[Lab] Deploying a Bottom-Up Web Service in Tomcat Web Container

Summary of steps to deploy a web service in tomcat with JAX-WS

Prerequesites

  1. Setup and start tomcat

  2. Download the jax-ws ri zip, unzip it and copy paste the required jars into lib directory of tomcat.

 

Development and deployment

  1. Build the class files and place class files under WEB-INF/classes according to your package structure.

  2. Create files web.xml and sun-jaxws.xml (with required details) under WEB-INF folder.

  3. Package your web service classes and configuration files into a war file.

  4. Restart the server (or refresh the app list in the manager gui)

 

Verifying the results

  1. Run the service url to see the service.

  2. Append ?wsdl to the url to see the wsdl.

 

Prerequesites

Setup and start tomcat

Download and unzip latest tomcat zip version.

Set JAVA_HOME and JRE_HOME variables with JDK home location.

You can set the username, password and role in tomcat-users.xml file in which it might be commented out by default. If you are using manager gui, then you need to give manager-gui role as well:

<user username="tomcat" password="tomcat" roles="tomcat,manager-gui"/>

Go to bin folder of tomcat and run startup.bat to start tomat.

In case you run into AddressBindException, you can change the default 8080 port in tomcat inside server.xml under conf folder.

<Connector port="8081" protocol="HTTP/1.1"

 

Downloading dependencies

You need to download the jax-ws ri zip, unzip it and copy paste the required jars into lib directory of tomcat. (You can copy all jars from jaxws-ri\lib into tomcat lib directory if you are not sure.)

 

Development and deployment

  1. Build the class files and place class files under WEB-INF/classes according to your package structure.

    • The class files should be present in the package folder structure under WEB-INF/classes directory. So create WEB-INF, WEB-INF/classes and your package structure and class files inside the ‘classes’ folder.

  2. Create files web.xml and sun-jaxws.xml (with required details) under WEB-INF folder.

    • You need to have two files directly under WEB-INF - sun-jaxws.xml and web.xml. As discussed, some parts of web.xml is stricken out as this is automatically done by JAX-WS currently behind the scenes and not needed to be configured by us when using JAX-WS.

    • Samples are given below.

  3. Package your web service classes and configuration files into a war file.

    • You can package your web service classes and configuration files into a war file by running ‘jar cvf myws.war WEB-INF’ from the parent folder of WEB-INF and put it in tomcat webapps directory. (You can even create a myws folder directly in the webapps directory for your project and copy WEB-INF including subfolders and files to it.)

  4. Restart the server (or refresh the app list in the manager gui)

    • Finally you can restart the server (or refresh the app list in the manager gui) and see the deployed service in the url:  http://localhost:8081/myws/ts where myws is my context folder (name of war or folder under webapps) and ts is the url pattern I have configured in sun-jaxws.xml; and the wsdl can be found at the location: http://localhost:8081/myws/ts?wsdl.

 

Sample 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="TimeService"

 implementation="com.javajee.ws.tsexample1.TimeServiceImpl"

      url-pattern="/ts"/>

</endpoints>

 

Sample 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>ts</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>ts</servlet-name>

        <url-pattern>/ts</url-pattern>

    </servlet-mapping>

    <session-config>

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

    </session-config>

</web-app>

 

Important Note!

The steps and configurations might be different for different servers. For example, glassfish server now comes with metro and hence you have to just deploy your code and everything else is done behind the scene. There might be multiple ways for deploying a web service even on tomcat, but the steps given here is one of the easiest ways to do, especially if you are new. 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)