我刚开始学习JSP而且我需要发送一条肥皂消息,我将该消息创建为字符串。我只想将其发送到url
,我找不到任何简单的示例,我创建了一个jsp
页面和一个这样的类:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%
String file=request.getParameter("file");
%>
<jsp:useBean id="soap" class="omnix.jo.soap.soap" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>default</title>
<%=file%>
</head>
<body></body>
</html>
我正在使用Bean来上课,我在互联网上找到了这门课程:
package omnix.jo.soap;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
/**
* SOAP Client Implementation using SAAJ Api.
*/
public class soap
{
/**
* Method used to create the SOAP Request
*/
private static SOAPMessage createSOAPRequest() throws Exception
{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
/*
Construct SOAP Request Message:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:sam="http://samples.axis2.techdive.in">
<soap:Header/>
<soap:Body>
<sam:getStudentName>
<!--Optional:-->
<sam:rollNumber>3</sam:rollNumber>
</sam:getStudentName>
</soap:Body>
</soap:Envelope>
*/
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("sam", "http://samples.axis2.techdive.in");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("getStudentName", "sam");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("rollNumber", "sam");
soapBodyElem1.addTextNode("3");
soapMessage.saveChanges();
// Check the input
System.out.println("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
/**
* Method used to print the SOAP Response
*/
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception
{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
/**
* Starting point for the SAAJ - SOAP Client Testing
*/
public static void main(String args[])
{
try
{
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
//Send SOAP Message to SOAP Server
String url = "http://localhost:8080/axis2/services/Student?wsdl";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
}
catch (Exception e)
{
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
}
但我没有wsdl
。我知道消息的样子,第二件事是如何在上面的jsp
页面中使用这个类。
由于
答案 0 :(得分:0)