Web Service using MyEclipse using JAX-API and Web Client


    1. Create a “Web Service Project” in the “New” window.

    1. Click on NEXT button, the “New Web Service Project” appears.

    1. Fill the “Project Name” textbox and click on FINISH button.

You will see the below screen:-

    1. Create a Java class which has will be exposed as webservice.

For our example we will be making a Calculator Service.

We will create a class <Calculator> and keep it inside the package named “com.calculator.ws”.

Click on the FINISH button after filling the details as above.

    1. The Calculator class will have the following:-

package com.calculator.ws;

public class Calculator {

public int add(int a, int b) { return (a + b); }

public int subtract(int a, int b) { return (a – b); }

public int multiply(int a, int b) { return (a * b); }

public int divide(int a, int b) { return (a / b); }
}

    1. Now click on the Web Service icon, it will show a drop-down. Then select “New Web Service”.

    1. “New Web Service” window pops up as below. Select the “Project” as your current project. Select Framework as “JAX-WS” and Strategy as “Create web service from Java class (Bottom-up scenario). Click on NEXT button.

    1. “New Soap Web Service – Bottom-up Scenario” window pops up.

Locate the java class from the project by using BROWSE button. Click on “Generate WDSL in project” check-box and click on FINISH button.

    1. You will see the following directory structure. Also the wsdl and xsd files are created.

    1. Now we create the client component to consume the webservice.

Select the “New Web Service Client”

    1. “New Web Service Client” window pops up.

Check that the project name is the same as before and select “JAX-WS” as framework. Press NEXT.

    1. You will see the following window.

Click on the BROWSE button to locate the WSDL file (follow step 9).

We will create a new package named “com.calculator.client” which will contain the client classes.
Press NEXT after completing the steps.

    1. You should see the following window, without any error displayed.

Press NEXT button.

    1. Press FINISH after you see the below window.

    1. You will see the following files been created inside the client package, see below.

    1. Now write the code to consume the web service.

We will be using a JSP file as client. You can also use a java file to access the web service.

<%@page import=“com.calculator.client.CalculatorService” %>
<%@page import=“com.calculator.client.CalculatorDelegate” %>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>
<head>
<title>Calculator Test</title>
</head>

<body>
<h1>Calculator Page</h1>
<form action=“” method=“post”>
<table border=“0”>
<tr><td>First Value</td><td><input type=“text” name=“firstvalue” /></td></tr>

<tr><td>Second Value</td><td><input type=“text” name=“secondvalue”/></td></tr>

<tr><td><input type=“submit”></td></tr>
</table>
</form>

<%
if(request.getParameter(“firstvalue”)!=null && request.getParameter(“secondvalue”)!=null){
int first=Integer.parseInt(request.getParameter(“firstvalue”));
int second=Integer.parseInt(request.getParameter(“secondvalue”));
CalculatorService service = new CalculatorService();
CalculatorDelegate delegate = service.getCalculatorPort();
try{

/* Using the web service, perform the 4 calculations */
out.println(“1. “+first+”+”+second+”=” + delegate.add(first, second)+”<br>”);
out.println(“2. “+first+”-“+second+”=” + delegate.subtract(first, second)+”<br>”);
out.println(“3. “+first+”*”+second+”=” + delegate.multiply(first, second)+”<br>”);
out.println(“4. “+first+”/”+second+”=” + delegate.divide(first, second)+”<br>”);
}catch(Exception e){
out.print(“ERROR: Cannot divide by ZERO!”);
}
}else{
out.print(“Please supply values”);
}
%>

</body>
</html>

    1. Run the project using a web server, in this case we will use Apache Tomcat.

Now we can have a cup of hot coffee, because we deserve it, now that our Web Service is up and running successfully.

Remember:-

Web Service is the foundation of Service Oriented Architecture.

 

Advertisements

5 thoughts on “Web Service using MyEclipse using JAX-API and Web Client”

  1. hi tried the above example ,i got the result with java client . but access the webservices with jsp i got initialization error. please solve me fast.
    HTTP Status 500 –

    ——————————————————————————–

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: An exception occurred processing JSP page /result.jsp at line 7

    4: String path = request.getContextPath();
    5: String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;%>
    6:
    7:
    10:

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    java.lang.ExceptionInInitializerError
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    javax.xml.ws.WebServiceException: Error creating JAXBContext for W3CEndpointReference.
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:222)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:218)
    java.security.AccessController.doPrivileged(Native Method)
    com.sun.xml.ws.spi.ProviderImpl.getEPRJaxbContext(ProviderImpl.java:217)
    com.sun.xml.ws.spi.ProviderImpl.(ProviderImpl.java:88)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
    Two classes have the same XML type name “address”. Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Address
    at public com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Address com.sun.xml.ws.developer.MemberSubmissionEndpointReference.addr
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference
    this problem is related to the following location:
    at javax.xml.ws.wsaddressing.W3CEndpointReference$Address
    at private javax.xml.ws.wsaddressing.W3CEndpointReference$Address javax.xml.ws.wsaddressing.W3CEndpointReference.address
    at javax.xml.ws.wsaddressing.W3CEndpointReference
    Two classes have the same XML type name “elements”. Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Elements
    at public com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Elements com.sun.xml.ws.developer.MemberSubmissionEndpointReference.referenceProperties
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference
    this problem is related to the following location:
    at javax.xml.ws.wsaddressing.W3CEndpointReference$Elements
    at private javax.xml.ws.wsaddressing.W3CEndpointReference$Elements javax.xml.ws.wsaddressing.W3CEndpointReference.referenceParameters
    at javax.xml.ws.wsaddressing.W3CEndpointReference

    com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
    com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:438)
    com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:286)
    com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:139)
    com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:117)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211)
    javax.xml.bind.ContextFinder.find(ContextFinder.java:372)
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:220)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:218)
    java.security.AccessController.doPrivileged(Native Method)
    com.sun.xml.ws.spi.ProviderImpl.getEPRJaxbContext(ProviderImpl.java:217)
    com.sun.xml.ws.spi.ProviderImpl.(ProviderImpl.java:88)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.

    ——————————————————————————–

    Apache Tomcat/6.0.13

  2. access with java its working fine but access with jsp gives error initialization error.

    org.apache.jasper.JasperException: An exception occurred processing JSP page /result.jsp at line 7

    4: String path = request.getContextPath();
    5: String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;%>
    6:
    7:
    10:

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    java.lang.ExceptionInInitializerError
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    javax.xml.ws.WebServiceException: Error creating JAXBContext for W3CEndpointReference.
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:222)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:218)
    java.security.AccessController.doPrivileged(Native Method)
    com.sun.xml.ws.spi.ProviderImpl.getEPRJaxbContext(ProviderImpl.java:217)
    com.sun.xml.ws.spi.ProviderImpl.(ProviderImpl.java:88)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

    root cause

    com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
    Two classes have the same XML type name “address”. Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Address
    at public com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Address com.sun.xml.ws.developer.MemberSubmissionEndpointReference.addr
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference
    this problem is related to the following location:
    at javax.xml.ws.wsaddressing.W3CEndpointReference$Address
    at private javax.xml.ws.wsaddressing.W3CEndpointReference$Address javax.xml.ws.wsaddressing.W3CEndpointReference.address
    at javax.xml.ws.wsaddressing.W3CEndpointReference
    Two classes have the same XML type name “elements”. Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Elements
    at public com.sun.xml.ws.developer.MemberSubmissionEndpointReference$Elements com.sun.xml.ws.developer.MemberSubmissionEndpointReference.referenceProperties
    at com.sun.xml.ws.developer.MemberSubmissionEndpointReference
    this problem is related to the following location:
    at javax.xml.ws.wsaddressing.W3CEndpointReference$Elements
    at private javax.xml.ws.wsaddressing.W3CEndpointReference$Elements javax.xml.ws.wsaddressing.W3CEndpointReference.referenceParameters
    at javax.xml.ws.wsaddressing.W3CEndpointReference

    com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
    com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:438)
    com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:286)
    com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:139)
    com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:117)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211)
    javax.xml.bind.ContextFinder.find(ContextFinder.java:372)
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:220)
    com.sun.xml.ws.spi.ProviderImpl$2.run(ProviderImpl.java:218)
    java.security.AccessController.doPrivileged(Native Method)
    com.sun.xml.ws.spi.ProviderImpl.getEPRJaxbContext(ProviderImpl.java:217)
    com.sun.xml.ws.spi.ProviderImpl.(ProviderImpl.java:88)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    javax.xml.ws.spi.FactoryFinder.newInstance(FactoryFinder.java:36)
    javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:95)
    javax.xml.ws.spi.Provider.provider(Provider.java:83)
    javax.xml.ws.Service.(Service.java:56)
    org.me.cli.CalService.(CalService.java:54)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:63)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

  3. I think that what you typed was very reasonable. But, think about this, what if you added a little information?
    I mean, I don’t want to tell you how to run your blog, however what if you added a post title that makes people desire more? I mean Web Service using MyEclipse using JAX-API and Web Client Sushantnayak’s Techincal Weblog is kinda plain.
    You might look at Yahoo’s front page and watch how they create news headlines to get people to open the links. You might add a related video or a related picture or two to grab readers excited about what you’ve written.
    In my opinion, it would bring your posts a little livelier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s