This is the detail of how to implement the SOAP protocol, rather than simply use RPC SOAP.
If we want to use RPC by SOAP, we can use apache SOAP, which have implemented the detail of SOAP code, and we can just use it by class Call. In this case, we don't need to care about the implement detail.
To study deeper knowledage of SOAP, we should figure out the detail of implement, so the following thing is introduce how to program SOAP in JAVA.
The main idea to implement SOAP:
Sender:
1, create a soap message object by message Factory
2, get the soap part, envolope, body, header from soap message.
3, add some elements to the body
4, save change and create a URL
5, create a soap connection by soap connection factory
6, use connection.call to send the soap message and get the reply
Receiver:
1, it has to be a servlet firstly, create a class which extends HttpServlet
2, there is two method should be override. we override the doPost()
3, get inputstream from request
4, create a soap message by messageFactory and inputstream.
5, get the soap body from message.
6, read the data from body.
7. new a message and body
8, write the reply data into body
9, get outputstream by response.
10, use message.writeTo(outputstream) to reply sender.
First of all, we need to setting the environment.
The environment I use: Win 8.1 + JDK 8 + Tomcat 8 + Eclipse.
The Eclipse + JDK 8 is easy to install and setting, so I am not going to talk the detail of that.
Tomcat.
1, Download the Tomcat file, of course.
2, Extract it to some folder
3, Open Eclipse(Java EE version) -> window -> preferences -> server ->runtime environment -> add - >apache -> tomcat 8.x -> indicate the path of Tomcat folder -> finish
4, install the pluge of Tomcat.
#download the tomcat pluge.
#extract the zip file and get the jar file.
#copy the jar file to Eclipse pluges folder.
#restart the Eclipse
#we can create a Tomcat project now~
5, add server
Eclipse: Window->preferences
Add
input the correct Tomcat path
Finish.
Find the Servers window box
And then we can see
Double click it, we can modify the setting of Server, like
port number
==> Finish the Tomcat setting<=====
Codeing:
In Eclipse
File->new->project
We can see the project struct in the Package Explorer
we should create a server class file in the src folder, which extends HttpServlet.
public
class
SoapServer
extends
HttpServlet
and override the doPost() method
code:
//
orange line is the most important code
protected
void
doPost(HttpServletRequest
request
, HttpServletResponse
response
)
throws
ServletException, IOException {
SOAPMessage
message
=
null
;
SOAPPart
part
=
null
;
SOAPEnvelope
envelope
=
null
;
SOAPHeader
header
=
null
;
SOAPBody
body
=
null
;
if
(
request
.getContentLength() > 0){
try
{
message = MessageFactory.newInstance().createMessage( new MimeHeaders(),request .getInputStream());
message
.saveChanges();
body
=
message
.getSOAPBody();
String
nameString
=
body
.getFirstChild().getTextContent();
String
luck
=
"Good"
;
System.
out
.println(
"run"
);
response.setContentType("text/xml" );
message
= MessageFactory.newInstance().createMessage();
body
=
message
.getSOAPPart().getEnvelope().getBody();
body
.addChildElement(
"Luck"
).addTextNode(
luck
);
message
.saveChanges();
message.writeTo( response.getOutputStream());
}
catch
(SOAPException
e
) {
e
.printStackTrace();
}
}
else
{
System.
out
.println(
"request is null"
);
}
}
//orange line is the most important code
Client:
import
java.io.IOException;
import
java.util.Scanner;
import
javax.xml.soap.MessageFactory;
import
javax.xml.soap.Name;
import
javax.xml.soap.SOAPBody;
import
javax.xml.soap.SOAPBodyElement;
import
javax.xml.soap.SOAPConnection;
import
javax.xml.soap.SOAPConnectionFactory;
import
javax.xml.soap.SOAPEnvelope;
import
javax.xml.soap.SOAPException;
import
javax.xml.soap.SOAPHeader;
import
javax.xml.soap.SOAPMessage;
import
javax.xml.soap.SOAPPart;
public
class
MySoapClient {
public
static
void
main(String[]
args
){
SOAPConnection
connection
=
null
;
SOAPMessage
message
=
null
;
SOAPPart
part
=
null
;
SOAPEnvelope
envelope
=
null
;
SOAPHeader
header
=
null
;
SOAPBody
body
=
null
;
Scanner
input
=
new
Scanner(System.
in
);
String
name
=
input
.nextLine();
try
{
connection = SOAPConnectionFactory.newInstance().createConnection();
message = MessageFactory.newInstance().createMessage();
part = message.getSOAPPart();
envelope = part.getEnvelope();
header
=
envelope
.getHeader();
body = envelope.getBody();
Name
bodyEle
=
envelope
.createName(
"process"
,
"Request"
,
"http://www.soptme.com"
);
SOAPBodyElement
bodyElement
=
body
.addBodyElement(
bodyEle
);
bodyElement
.addChildElement(
"name"
).addTextNode(
name
);
message.saveChanges();
System.
out
.println(
"Request:"
);
message
.writeTo(System.
out
);
System.
out
.println();
String
url = "http://localhost:7647/MyServlet/SoapServer" ;
SOAPMessage resp = connection.call(message , url );
System.
out
.println(
"connected"
);
resp
.writeTo(System.
out
);
System.
out
.println();
SOAPBody
reBody = resp.getSOAPBody();
System.
out
.println(
"Your luck of today:"
);
System.
out
.println(
"==>"
+
reBody
.getFirstChild().getTextContent());
}
catch
(UnsupportedOperationException
e
) {
e
.printStackTrace();
}
catch
(SOAPException
e
) {
e
.printStackTrace();
}
catch
(IOException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
}
}
}
Publish server side to Tomcat
Then start the Tomcat server.
At that time, SOAP server side is already runing on the computer!
we can run the client to test it!