How to call a Web service using servlet

Output of this servlet, SOAP UI, Client class and Publisher class  in previous example is same.
Add servlet.jar file in lib filder.

1) First you need to publish the web service as shown in previous example
2) Check this url to see if wsdl file is created:
   http://localhost:9090/WS/WSServiceHello?wsdl
3) After creating wsdl file run this(CallWeb.java) servlet as shown below.

Note:
Sometimes you may be given .asmx url which may have been developed in asp.net. You should have WSDL file for the client .

How to create wsdl file from .asmx
Soln:
You must have a webservice like this
http://localhost/MyWebService.asmx

To retreive your wsdl file you must put this address:
http://localhost/MyWebService.asmx?wsdl

Use wizdler tool to test this SOAP webservice
.

If you have WSDL file you can directly use below servlet to call that webservice(WSDL file).

CallWeb.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.servlet.wscall;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CallWeb extends HttpServlet {
 private static final long serialVersionUID = 1L;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  //String a=request.getParameter("sParams");
  //System.out.println(a);
  String name="RAHUL";
  String responseString;
  String outputString="";
  try{
   
   System.out.println("Checking 1 *****************");
   String WSurl="";
   String envelope = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.dineshonjava.com/\"><soapenv:Header/><soapenv:Body><ws:sayHello><!--Optional:--><arg0>"+name+"</arg0></ws:sayHello></soapenv:Body></soapenv:Envelope>";
//refer soap ui for above input(String envelope="....") 

   String SOAPAction ="";
    
   // http://localhost:9090/WS/WSServiceHello?wsdl
    
   WSurl = "http://localhost:9090/WS/WSServiceHello";   
    
   SOAPAction = "http://localhost:9090/WS/WSServiceHello";
   URL url = new URL(WSurl);
   java.net.URLConnection connection = url.openConnection();
   HttpURLConnection httpConn = (HttpURLConnection)connection;
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   System.out.println("Checking 2 *****************");
    
   byte[] buffer = new byte[envelope.length()];
   buffer = envelope.getBytes();
   bout.write(buffer);
   byte[] b = bout.toByteArray();
   // Set the appropriate HTTP parameters.
   httpConn.setReadTimeout(60000);
   httpConn.setConnectTimeout(60000);
   System.out.println("Checking 3 *****************");
   httpConn.setRequestProperty("Content-Length",String.valueOf(b.length));
   httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
   httpConn.setRequestProperty("SOAPAction", SOAPAction);
   httpConn.setRequestMethod("POST");
   httpConn.setDoOutput(true);
   httpConn.setDoInput(true);
   OutputStream out = httpConn.getOutputStream();
   System.out.println("Checking 4*****************");
   //Write the content of the request to the outputstream of the HTTP Connection.
   out.write(b);
   out.close();
   System.out.println("Checking 5*****************");
   //Ready with sending the request.
   //Read the response.
   InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
   BufferedReader in = new BufferedReader(isr);
   //Write the SOAP message response to a String.
   while ((responseString = in.readLine()) != null) {
    
   outputString = outputString + responseString;
    
         
   }
   System.out.println("Checking 6*****************"+outputString);
  }
  catch(Exception e)
  {
   System.out.println("********Stack Trace starts**********"+" ");
   e.printStackTrace();
   System.out.println("********Stack Trace ends**********" );
   
   outputString = "ERROR";
  }

 }


}



Output:

Checking 1 *****************
Checking 2 *****************
Checking 3 *****************
Checking 4*****************
Checking 5*****************

Checking 6*****************<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://ws.dineshonjava.com/"><return>Hello, Welcome to JAX-WS World and keep learning!! RAHUL</return></ns2:sayHelloResponse></S:Body></S:Envelope>


Later we could parse this xml output to get values and use it in our application.

Explanation:
Refer soap ui for input(String envelope="....") in above servlet. Please refer this image
You need to start webservice first so that wsdl file is available when this servlet hits that wsdl file. Refer previous example.

No comments:

Post a Comment