SOAP Servlet Example Example
843834May 5 2004 — edited May 21 2004Hi all, I have got the following method which resides on my java client (application) and I want to send these data to a servlet that is hosted on a Apache Jakarta SOAP server. I can send the SOAP message but once the message reaches the server I am getting an error 500, using the tcpmon I found that the error "Unsupported content type" occurs. I suspect that my servlet does not work well. I would be grateful if someone could give me an example of a servlet that would get some values from a SOAP message and send another SOAP message back to the client.
Thank you very much in advance!
public void sendAddDataToServer()
{
try//Try to establish connection with the SOAP server.
{
String soapServer = "http://localhost:8080/soap/servlet/rpcrouter";
// Process the arguments.
URL serverURL = new URL(soapServer);//
URLConnection urlConnection = serverURL.openConnection();
HttpURLConnection connection = (HttpURLConnection) urlConnection;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
String SOAP_ACTION = "http://localhost:8080/soap/servlet/rpcrouter";
OutputStream out = connection.getOutputStream();
Writer writer = new OutputStreamWriter(out);
writer.write("<?xml version='1.0' encoding='UTF-8'?>\r\n");
writer.write("<SOAP-ENV:Envelope xmlns:SOAP-ENV=");
writer.write("\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n");
writer.write("xmlns:xsi=");
writer.write("\"http://www.w3.org/2001/XMLSchema-instance\"\r\n");
writer.write("xmlns:xsd=");
writer.write("\"http://www.w3.org/2001/XMLSchema\">\r\n");
writer.write("<SOAP-ENV:Body>\r\n");
writer.write("<ns1:add xmlns:ns1=\"urn:add\">\r\n");
writer.write("<prefix xsi:type=\"xsd:string\">"+prefixString+"</prefix>\r\n");
writer.write("<firstName xsi:type=\"xsd:string\">"+firstNameString+"</firstName>\r\n");
writer.write("<middleName xsi:type=\"xsd:string\">"+middleNameString+"</middleName>\r\n");
writer.write("</ns1:add>\r\n");
writer.write("</SOAP-ENV:Body>\r\n");
writer.write("</SOAP-ENV:Envelope>");
writer.flush();
writer.close();
InputStream inputStream = connection.getInputStream();
int c;
while ((c = inputStream.read())!= -1) System.out.write(c);
inputStream.close();
}
catch(IOException e)
{
System.err.println(e);
}
}