Friday, 2 August 2013

Web Service in JAVA

Web Service in JAVA (using Notepad, JAVA JDK, Tomcat Web Server, some additional JARs)

In this tutorial we will learn how to create Web Service using JAVA.

What is a WEB Service?

Web Service Wiki
Web Service W3School

WEB Service in JAVA

Java provide "javax.jws.WebService" class for creating a Web Service. How ever for creating a web service, with just notepad, we need some additional JAR files, which can be found form Net Beans installation directory. If you do not have Net Beans installed in your system, do not bother, these file are available in this tutorial.
Follow the steps.

Step 1: Create a .java file and Compile it.

Open notepad write the following code and save it with Addition.java name (Extension should be .java) at path "c:\java\src\ws\Addition.java".

Addition.java

// c:\java\src\ws\Addition.java
package ws;
import javax.jws.WebService;

@WebService
public class Addition {
 public double add( double value1, double value2 ) {
  return value1 + value2;
 }
}
   Now compile this file.
  1. Open Command prompt 
  2. Navigate to path "c:\java\src\ws\" 
  3. Type Command: javac Addition.java 
Snapshot:
Note: Make sure your environment variable is set for Java PATH if not then either set it or compile this file your java's bin folde. 

Step 2: Create web.xml file. 

Open notepad write the following code and save it with web.xml name (Name should be web.xml only).

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Addition</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>Addition</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
<!-- not needed
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
-->
</web-app>

Step 3: Create sun-jaxws.xml file. 

Open notepad write the following code and save it with sun-jaxws.xml name (Name should be sun-jaxws.xml only).

sun-jaxws.xml

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

Step 4: Create context.xml file. 

Open notepad write the following code and save it with context.xml name (Name should be context.xml only).

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Addition"/>

Step 5: Create directories. 

Now go to your Tomcat's webapps ("Tomcat's installation path\Tomcat\webapps") folder and create following directories in it
  1. Tomcat's installation path\Tomcat\webapps\Addition
  2. Tomcat's installation path\Tomcat\webapps\Addition\META-INF
  3. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF
  4. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes
  5. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes\ws
  6. Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\lib

Step 6: Download JARs. 

  1. activation.jar
  2. webservices-api.jar
  3. webservices-extra.jar
  4. webservices-extra-api.jar
  5. webservices-rt-2.0.jar
  6. webservices-tools.jar

Step 6: Copy files. 

  • Copy and paste all the jar files in "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\lib" folder.
  •  Copy and paste Addition.class file form "c:\java\src\ws\Addition.class" to "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF\classes\ws"
  • Copy and paste web.xml & sun-jaxws.xml to "Tomcat's installation path\Tomcat\webapps\Addition\WEB-INF"
  • Copy and paste context.xml to "Tomcat's installation path\Tomcat\webapps\Addition\META-INF"

Step 7: Run on localhost. 

Start your Tomcat server and run the web service in browser at web address

http://localhost:port/Addition/add?wsdl