使用Jsp发送Soap信封

时间:2013-12-31 08:02:15

标签: java jsp soap

我刚开始学习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页面中使用这个类。

由于

1 个答案:

答案 0 :(得分:0)

要使用JSP,您需要使用Glassfish之类的应用程序服务器进行部署。您下载的类用作创建SOAP信封的独立类(不会过多地检查代码)。

尝试浏览JSP的一些教程,以便在开始调用webservices之前了解具有支持bean概念的jsp如何工作。这是我找到的一个页面:JSP Tut

如果主要目标是调用Web服务,请尝试直接从独立服务器调用它。但是,这还需要更多了解wsdl的外观。尝试一次关注一个。

你的问题实际上是两个问题,所以从其中一个问题开始,当你掌握它时,请转到下一个问题。我希望这会对你有所帮助,因为这个问题有点太大,无法在这里得到相关的答案,我相信。