Engineering Full Stack Apps with Java and JavaScript
Prerequesites
Setup and start tomcat
Download the jax-ws ri zip, unzip it and copy paste the required jars into lib directory of tomcat.
Development and deployment
Build the class files and place class files under WEB-INF/classes according to your package structure.
Create files web.xml and sun-jaxws.xml (with required details) under WEB-INF folder.
Package your web service classes and configuration files into a war file.
Restart the server (or refresh the app list in the manager gui)
Verifying the results
Run the service url to see the service.
Append ?wsdl to the url to see the wsdl.
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"
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.)
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.
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.
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.)
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.
<?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>
<?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>
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.