Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a web service

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 195
    Comment on it

    SOAP:

    Simple Object Access Protocol defines the request and response message format in a web service. It is XML-based communication format and encoding format for inter-application communication. SOAP is built to use for cross-platform, cross-language distributed computing applications knows as Web Services.

    AXIS:

    Apache eXtensible Interaction System is an open source SOAP implementation for web service framework. AXIS is used to constuct SOAP processors such as clients, servers, gateway etc. or in other words, AXIS is a SOAP engine. AXIS can generate Web Services Description Language (WSDL) for other systems.

    WSDL:

    Web Services Description Language is an XML format that describes the functionality of a web service. WSDL provides machine readable description how a web service can be called, what data it expact and how it responds to the client.

    How to create Web-Service

    Step 1

    Create a dynamic web project in eclipse.

    Step 2

    Create processor class for your web-service. You can have as many as processors for your web service. These processors are the endpoints for your web services.

    Step 3

    Add the bussiness login inside the processor. I've added the following sample code to acess the users ids from a sample database.

    package com.evontech.ws;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    public class Processor {
    
        public String processWS() {
            String dbids = null;
            try {
                InitialContext context = new InitialContext();
                Context envContext = (Context) context.lookup("java:comp/env");
                DataSource dataSource = (DataSource) envContext.lookup("jdbc/UsersDB");
    
                Connection con = dataSource.getConnection();
    
                String query = "SELECT * FROM usertbl";
                Statement statement = con.createStatement();
                ResultSet rs = statement.executeQuery(query);
    
                dbids = "";
                while ( rs.next() ) {
    
                    String dbid = rs.getString("DBID");
                    dbids += dbid + ",";
                }
    
            } catch (NamingException e) {
                dbids = e.getMessage();
                e.printStackTrace();
            } catch (SQLException e) {
                dbids = e.getMessage();
                e.printStackTrace();
            }
    
            return dbids;
        }
    
    }
    

    Please notice, I've used JNDI to get the DataSource from container. Please refer this to understand how we configure JNDI DataSource for a container.

    Step 4

    Right click on processor class. Click 'Web Service'-> 'Create Web Service'

    alt text Step 5

    Select your configured Server Runtime and Web Service Runtime for your project and click Next. Check the allowed methods for web service and click Finish. Once you Create Web Service, it will generate the WSDL and WSDD configuration as per your java classes.

    Setp 6

    Start the server.

    Service is available now the at your server runtime. I deployed the web service on my local tomcat server and it is available at the following url -

    http://localhost:8080/web-service-test/services

    The url show all the web services generated at this url. Clicking wsdl lead to the WSDL generated for your web service. AXIS generate two web services Version and AdminService for itself along with your web service.

    alt text alt text

    Thanks.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: