The booking author of this article should have aware of the basic specifications of SOAP 1.1, and familiar with J2EE's basic development. If you are not familiar, you can look at my blog: Use SOAP to develop Java Web services - AXIS development plan, detailed criteria You can view the official website of W3C, connect the following: http://www.w3.org/tr/2000/note-soap-20000508/. This article is mainly to explore SAAJ (SOAP WITH ATACHMENT API for Java), JAXM (Java API for XML Messaging) to learn about SOAP's role in J2EE development. JAXM and SAAJ support for B2B and Web service applications, XML-based messaging, support many industry standards, including SOAP and EBXML. Saaj is one of the components of JWSDP. JWSDP has now updated 1.5 version, Saaj can download from Sun's website: http://java.sun.com/xml/downloads/saaj.html, Jaxm is used for XML messages The criterion is not in the release package of JWSDP1.5, you can download: http://java.sun.com/xml/jaxm/downloads/index.html, Because Saaj is based on JavaMailTM API (1.2), JavaBeansTM Activation Framework (JAF) (1.1.3) and JAXP (1.2.6), so I have to download them, and Sun's official website has provided, ok, now we get the following package (pressing the name I downloaded) : Activation.jar (jaf), jaxm-api.jar (jaxm), mail.jar (javamail), saaj-api.jar (saaj), saaj-impl.jar (Saaj), my JAXP is already in JDK1. 5 in 5. There is also a container that supports servlets. Ok, ready to work, let's start our learning: First, saaj can be used to send and receive XML documents as SOAP messages without having to process the basic program structure of the JAXM provider, and no need to handle SOAP-based HTTP request / response. SAAJ was originally an integral part of the JAXM1.0API package, and starting from JAXM1.1, the package is renamed is Saaj1.1API. Let's take a simple SOAP1.1 message: post / stockquote http / 1.1 host: www.stockquoteserver.com content-type: text / xml; charset = "UTF-8" Content-length: nnnn soapaction: "Some- URI "
<------------ This is an envelope sign
<------------ this is the message head sign
5
<------------ This is the main sign
DEF
Saaj API provides us with a high-level SOAP message package interface, such as an envelope interface: javax.xml.soap.soapenvelope, and provides a message header (), which returns a message header: javax.xml.soap.soapheader. So, by calling Saaj interface functions, we can operate the SOAP message. Second, now let's discuss the JAXM that does not use the message exchange provider, the application client sends and receives message operations directly with its remote partners through SOAP (defined the interaction and synchronous communication model of point-to-point, where the sender is sent. The recipient switches the message in the form of a request and response. The sender sends a message and waits for the response of the lock target location.
Transmitting step: 1) Creating a SOAP connection; 2) Creating a message factory; 3) Creating a message; 4) Fill messages; 5) Add a message; 6) Add SOAP attachment; 7) Send a message and receive a response; 8) Close the provider Connection; one is one of my writes: / ** * sender.java * Copyright 2005-2-10 * / Import javax.xml.soap.soapConnectionFactory; import javax.xml.soap.soapexception; import javax.xml.soap .SOAPConnection; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPHeader; import javax .xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPHeaderElement; import javax.xml.soap.Name; import javax.xml.soap.SOAPElement; import java.net.URL; import javax.xml.messaging.URLEndpoint; import javax.activation.DataHandler; import java.io.IOException; public class Sender {public SOAPMessage getMessage () throws SOAPException, Exception {// message factory MessageFactory msgFactory = MessageFactory.newInstance (); SOAPMESSAGE MESSAGE = msgfactory.createMessage (); // Get a SOAPPART Object SOAPPART SOAPPART = message.getsoAppart (); // Get envelope SOAPEnvelope soapEnvelope = soapPart.getEnvelope (); // get the message header SOAPHeader soapHeader = soapEnvelope.getHeader (); // get the SOAP body SOAPBody soapBody = soapEnvelope.getBody (); // add a header element SOAPHeaderElement headerElement = soapHeader.addHeaderElement (soapEnvelope .createname ("Studentno", "STU", "http://www.cun.edu.cn/jws")); HeadeRelement.addTextNode ("jws0229043"); // Add Message Main Name BodyName = SOAPENVELOPE.CREATENAME ( "GetStudentInfo", "STU", "http://www.cun.edu.cn/jws"); SOAPBODYEELEMENT BODYEEEEMENT = SOAPBODY.ADDBODYEEEMENT (BODYNAME); Name EleName =
soapEnvelope.createName ( "StudentName"); SOAPElement se = bodyElement.addChildElement (eleName); se.addTextNode ( "Wang wenyin"); // add SOAP attachment URL url = new URL ( "http: //img20.photo.163 .com / gdanthrowwy / 5123911 / 80707051.jpg "); dataHandler dataHandler = new dataHandler (url); // use the JAF message.addAttachmentPart (message.createAttachmentPart (dataHandler)); // update SOAP message message.saveChanges (); return message;} public void send (SOAPMessage message) throws SOAPException, IOException {// create a SOAP connector SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance (); SOAPConnection sc = scf.createConnection (); // send a SOAP message to a destination, and returns a message uRLEndpoint urlEndpoint = new uRLEndpoint ( "http: // localhost / saaj / StudentInfoServlet"); SOAPMessage response = sc.call (message, urlEndpoint); if (response = null!) {// SOAP message is output to the console System .out.println ("Receive SOAP Message from localhost:"); response.writeto (system.out);} else {system.err.println ("No Response Received from Partner!");} sc.close (); } public static void main (string [] args) throws soape Xception, Exception {sender sender = new sender (); soapMessage message = sender.getMessage (); sender.send (message);}} then compile, pay attention to the set of ClassPath variables to add the above packages (you can set one The script is complete, if you are familiar with Ant, it is more simple. After the compilation is successful, we waited for a sender.class file, which is a sender file. When you run Java Sender, you will send the SOAP message to our companion http: // localhost / saaj / studentinfoservlet, and wait back. Here we continue to write a servlet to receive the message just sent. Third, the receiver servlet application saaj.war.
/ ** * JAXMReceiveServlet.java * Copyright 2005-2-10 * / import javax.xml.messaging.JAXMServlet; import javax.xml.messaging.ReqRespListener; import javax.xml.soap.MessageFactory; import javax.servlet.ServletException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPEnvelope; import javax.servlet.ServletConfig; import java.io.FileOutputStream; import java.io.File; public class JAXMReceiveServlet extends JAXMServlet implements ReqRespListener {static MessageFactory mf = NULL; // Create a message plant static {try {mf = messagefactory.newinstance ();} catch (exception e) {E.PrintStackTrace ();}}; public void init (servletconfig sc) throws servleTexception {super.init (SC);} // Treated the SOAP message, and return a SOAP message public soapMessage OnMessage (SOAPMESSAGE MSG) {SOAPMESSAGE RESP = NULL; try {system.out.println ("incoming message:"); msg .writto (new fileoutputstream (new file ("../ WebApps / soapMessage.xml")))); // Create a return message resp = mf.createMessage (); SOAPENVELOPE SE = Resp.GetsoAppart (). getENvelope (); se.getBody (). AddChildElement (se.create Name ("ResponseMessage"). AddTextNode ("Received Message, THANKS"); Return Resp;} Catch (Exception E) {E.PrintStackTrace ();} Return Resp;}} then add the associated classpath into, compile ( No, I will check the programming of the servlet, and the space is limited.) Web.xml Deployment File: StudentInfoservlet
JaxmreceiveServlet
1
StudentInfoservlet
/ Studentinfoservlet