简体   繁体   中英

Send Soap Envelop Using Jsp

I just start learning JSP and I need to send a soap message, I created the message as string . I just want to send it to a url , I can't find any simple example, I created a jsp page and a class like this :

<!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>  

I am using a Bean to get my class, I found this class on the internet:

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();
                }
        }
}

But I don't have the wsdl . I know how the message looks like and the second thing is how to use this class in the jsp page above.

Thanks

To use JSP, you will need to deploy it using a application server like Glassfish . The class you've downloaded is used as a stand-alone to create a SOAP envelope (without examining the code too much).

Try going through some tutorials for JSP to get the hang of how the jsp with backing bean concept works before starting calling webservices. Here is one page I found: JSP Tut

If the main goal is calling a webservice, try calling it directly from a stand-alone. However, this also requires some more knowledge of how the wsdl looks like. Try focusing on one at a time.

Your question is actually two questions, so start with one of them and when you got the hang of it, get to the next. I hope this helps you a bit, because the question is a bit too big to get a relevant answer here, I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM