gpt4 book ai didi

java - 使用Jsp发送 SOAP 信封

转载 作者:行者123 更新时间:2023-12-01 06:17:27 30 4
gpt4 key购买 nike

我刚刚开始学习JSP,我需要发送一条soap消息,我将消息创建为 string 。我只想将其发送到 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 来获取我的类(class),我在互联网上找到了这个类(class):

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 页面中使用这个类。

谢谢

最佳答案

要使用 JSP,您需要使用像 Glassfish 这样的应用程序服务器来部署它。 。您下载的类将用作独立的类来创建 SOAP 信封(无需过多检查代码)。

在开始调用 Web 服务之前,尝试阅读一些 JSP 教程,以了解具有支持 bean 概念的 jsp 的工作原理。这是我找到的一页:JSP Tut

如果主要目标是调用 Web 服务,请尝试直接从独立程序调用它。然而,这还需要对 wsdl 的外观有更多的了解。尝试一次专注于一个。

你的问题实际上是两个问题,所以从其中一个开始,当你掌握了它的窍门后,再进入下一个。我希望这对您有所帮助,因为我相信这个问题太大了,无法在这里得到相关答案。

关于java - 使用Jsp发送 SOAP 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20853991/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com