接着写服务器端的处理SOAPMessage 的Servlet:
public class ReceiveNFServlet extends HttpServlet {
static MessageFactory mf = null;
static {
try {
mf = MessageFactory.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
};
MessageFactory msgFactory;
private Logger m_Logger = null;
public void init(ServletConfig sc) throws ServletException {
super.init(sc);
this.m_Logger = Logger.getInstance(this);
try {
// Initialize it to the default.
msgFactory = MessageFactory.newInstance();
} catch (Exception ex) {
this.m_Logger.error(ex.getMessage());
throw new ServletException("Unable to create message factory"
+ ex.getMessage());
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
// Get all the headers from the HTTP request.
MimeHeaders headers = this.getHeaders(req);
// Get the body of the HTTP request.
InputStream is = req.getInputStream();
// Now internalize the contents of the HTTP request and
// create a SOAPMessage
SOAPMessage msg = mf.createMessage(headers, is);
SOAPMessage reply = this.onMessage(msg);
resp.setStatus(HttpServletResponse.SC_OK);
resp.getOutputStream().print(msg.toString());
} catch (Exception ex) {
this.m_Logger.error(ex.getMessage());
throw new ServletException("JAXM POST failed " + ex.getMessage());
}
}
private MimeHeaders getHeaders(HttpServletRequest req) {
Enumeration enumt = req.getHeaderNames();
MimeHeaders headers = new MimeHeaders();
while (enumt.hasMoreElements()) {
String headerName = (String) enumt.nextElement();
String headerValue = req.getHeader(headerName);
this.m_Logger.info("HeaderName:" + headerName + "value:"
+ headerValue);
StringTokenizer values = new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens()) {
headers.addHeader(headerName, values.nextToken().trim());
}
}
return headers;
}
private void putHeaders(MimeHeaders headers, HttpServletResponse res) {
Iterator it = headers.getAllHeaders();
while (it.hasNext()) {
MimeHeader header = (MimeHeader) it;
String[] values = headers.getHeader(header.getName());
if (values.length == 1)
res.setHeader(header.getName(), header.getValue());
else {
StringBuffer concat = new StringBuffer();
int i = 0;
while (i < values.length) {
if (i != 0)
concat.append(',');
concat.append(values[i++]);
}
res.setHeader(header.getName(), concat.toString());
}
}
}
// 处理传过来的SOAP消息,并返回一个SOAP消息
/**
* @param msg
* @return
*/
public SOAPMessage onMessage(SOAPMessage msg) {
SOAPMessage resp = null;
try {
m_Logger.info("The Information received:");
String stMsg = msg.getSOAPPart().getEnvelope().getBody().toString();
m_Logger.info(stMsg);
// SOAPToBeanEngine oToBean = new SOAPToBeanEngine();
// oToBean.process(msg);
SOAPEngine engine = new SOAPEngine();
engine.process(msg);
} catch (Exception e) {
e.printStackTrace();
this.m_Logger.error(e.getMessage());
}
try {
resp = mf.createMessage();
if (resp.saveRequired())
resp.saveChanges();
} catch (Exception e) {
e.printStackTrace();
this.m_Logger.error(e.getMessage());
}
return resp;
}
}
也可以直接继承另外一个 Serlet,名字忘了,处理过程也是写在onMessage方法中。此处理程序创建并返回了一个空的SOAPMessage,根据需要可以返回包含消息的Message。具体的处理方法可以使用Dom4j或者jDOM来解析XML,也可以使用javax.xml.soap.这里就不详细写了