简体   繁体   中英

How to Read values from xml file in java webservice and store them like application variables in Webservice?

I am retrieving some values from xml file in web service. On Each call it goes to read xml file which makes it slow. I need to read them once and use same values on each request. Can I store xml values as application variables like Asp.net? Also please share some best code snippet to read xml file in web-method.

#

Webservice Code:-

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>

Both on client and server side you can create request/session/application scoped EJB s - insances of some class whitch would hold the data you need. For more information, as it is not a topic for single post, refer to Java EE specificaton

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