如何从java webservice中的xml文件中读取值并将它们存储为Webservice中的应用程序变量?

时间:2014-02-01 07:02:19

标签: java xml web-services

我正在从Web服务中的xml文件中检索一些值。每次调用它都会读取xml文件,这会使它变慢。我需要读一次它们并在每个请求上使用相同的值。 我可以将xml值存储为像Asp.net这样的应用程序变量吗?另外,请分享一些最佳的代码片段,以便在web-method中读取xml文件。

Webservice代码: -

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>

1 个答案:

答案 0 :(得分:0)

在客户端和服务器端,您都可以创建请求/会话/应用程序范围EJB s - 某些类whitch的insances将保存您需要的数据。有关详细信息,因为它不是单个帖子的主题,请参阅Java EE specificaton