Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

web services - Adding a java class (as a webservice) to the Apache Tomcat server to call it from another java client class running on my PC

I am learning about webservices, and I've done a simple Calculator application using webservices, where the Calculator and its functions to perform the operations are in a "server" class, and the interface (the client) is in another project.

The server is as follows:

package webservice.server;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class Calculator {

public static void main(String[] args) {
    System.out.println("Server started at: http://localhost:12345/calc");
    Endpoint.publish("http://localhost:12345/calc", new Calculator());
}

public int compute(int x, int y, String operation) {
    return Calculator.calculate(x, y, operation);
}

public static int calculate(int x, int y, String operation) {
    int result;
    String op;

    if ("ADD".equals(operation)) {
        result = x + y;
        op = "+";
    } else if ("SUB".equals(operation)) {
        result = x - y;
        op = "-";
    } else if ("MULT".equals(operation)) {
        result = x * y;
        op = "*";
    } else if ("DIV".equals(operation)) {
        result = x / y;
        op = "/";
    } else {
        // defaults to SUB
        result = x - y;
        op = "-";
    }

    log(x, y, result, op);

    return result;
}

private static void log(int x, int y, int result, String op) {
    System.out.format("%d %s %d = %d%n", x, op, y, result);
}

}

With this server running, I run the following command in the Terminal if the IntelliJ IDEA IDE I'm using:

wsimport -keep -p webservice.client http://localhost:12345/calc?wsdl

, to create the client files, creating automatically the following structure (I'm using IntelliJ Community):

Client structure

Currently, I am able to start my Calculator class (my server), and after I can start my CalculatorClient class to perform the calculations I need. Here is the code of this client:

package webservice.client;

public class CalculatorClient {

/**
 * Starts the web service client.
 */
public static void main(String[] args) {
    CalculatorService client = new CalculatorService();
    Calculator calculatorService = client.getCalculatorPort();
    int result = calculatorService.compute(10, 20, "ADD");
    System.out.println("Returned value from server:  " + result);
}

}

With the following result:

Returned value from server:  30

Process finished with exit code 0

As you can see, the Calculator client sends two values and the operation to the server via the webservice created, and the server returns the value to the client, so it can be printed.

This runs perfectly, so now my question is: How can I add this Calculator server class to an Apache Tomcat service? Is that possible? I've downloaded and installed the Apache Tomcat to my PC to learn how can I create webservices like this, just functionalities that will be accessed from another client program, as the CalculatorClient does, and these functionalities will be running on a local PC.

In my Apache Tomcat server I can see the following: Apache Tomcat server applications list

How can I add my Calculator class (the server) here and start it or stop it as needed, so my CalculatorClient can access to it? Is that possible? If not, how can I create services in Java like my Calculator class and add them to this Apache Tomcat server?

Thanks for any help or suggestion!

Daniel.

question from:https://stackoverflow.com/questions/65933301/adding-a-java-class-as-a-webservice-to-the-apache-tomcat-server-to-call-it-fro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...