如何在JavaWebService中从xml文件中读取值,并将它们像应用程序变量一样存储在webservice中?
我正在从web服务中的xml文件中检索一些值。每次调用时,它都会读取xml文件,这会使其速度变慢。我需要读取它们一次,并在每个请求中使用相同的值。 我可以像Asp.net一样将xml值存储为应用程序变量吗?另外,请分享一些在web方法中读取xml文件的最佳代码片段 # Web服务代码:-如何在JavaWebService中从xml文件中读取值,并将它们像应用程序变量一样存储在webservice中?,java,xml,web-services,Java,Xml,Web Services,我正在从web服务中的xml文件中检索一些值。每次调用时,它都会读取xml文件,这会使其速度变慢。我需要读取它们一次,并在每个请求中使用相同的值。 我可以像Asp.net一样将xml值存储为应用程序变量吗?另外,请分享一些在web方法中读取xml文件的最佳代码片段 # Web服务代码:- import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * * @author
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
*
* @author abc
*/
@WebService(serviceName = "TestXmlReader")
public class TestXmlReader {
/**
* This is a sample web service operation
*/
int counts=1;
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " ! "+counts;
// for example Each time I get counts as 1....
//so I need to increment it on each request from client
///I need to read xml file values here and store them as application variables
//so I dont have to read them from xml on each request
}
}
#
Client.jsp
<%--
Document : index
Created on : Feb 1, 2014, 10:23:28 AM
Author : abc
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%-- start web service invocation --%><hr/>
<%
try {
com.abc.TestXmlWebServiceClient.TestXmlReader_Service service = new com.abc.TestXmlWebServiceClient.TestXmlReader_Service();
com.abc.TestXmlWebServiceClient.TestXmlReader port = service.getTestXmlReaderPort();
// TODO initialize WS operation arguments here
java.lang.String name = "Testing XML Service";
// TODO process result here
java.lang.String result = port.hello(name);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%><hr/>
</body>
</html>
JSP页面
在客户端和服务器端,您都可以创建请求/会话/应用程序范围的EJB
s-某个类的存储空间,该类存储您需要的数据。有关更多信息,因为这不是一篇文章的主题,请参阅I need to hold values in Webservice。。类似于init方法或相同的方法。有没有这样的方法。。。好的,让我详细解释一下。我有一个xml配置文件,需要从中获取一些值,并将其保存到webservice重新启动。同时,当xml值被更改时,Web服务应该使用旧的存储值。如果这不符合您的需要,那么使用某种简单的缓存怎么样?好的,我是如何解决的,我在第一个请求中将xml值存储到静态类中,并在每个请求中检查它,如果它变为null,那么静态类将再次填充。但是,在web服务启动时,是否有任何事件可以填充该静态类?这将是一种方法,只要您的所有WS客户机都应该使用相同的值(假定为常量),非常感谢您的帮助。是的,所有客户都会使用相同的值。我用这个伟大的概念解决了我的问题。。1) ...... 2)