以下是本人原创,如若转载和使用请注明转载地址。本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址


一、根据我的上一篇博客

MyEclipse构建Web Service(Xfire框架)

下面是我实现的网页版的计算器,本来想着用php去调用webservice的接口,由于时间原因,现在用java去调用webservice接口。

再根据原先实现过的吧B/S 架构的计算器作业,现将网页说明博客如下:

Ext实现简单计算器

二、编写自己的类

根据上一篇博客的说明我们建立一个用于实现计算器的webservice类ExpWebService.java,其代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package server;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;
 
/**
 *
* 项目名称:CalculatorWebService 
* 类名称:ExpWebService 
* 类描述: 
* 创建人:王少帅 
* 创建时间:2013-12-12 下午02:58:32      
* @version
 */
public class ExpWebService {
     
    /**
     *
    * expCalculator(使用jexl自带的方式处理exp得到计算器的值) 
    * @param name
    * @return String
    * @Exception
     */
    public String expCalculator(String exp) throws Exception {
        // TODO Auto-generated method stub
        Expression e = ExpressionFactory.createExpression(exp);//使用提供的expression提供的方法
        JexlContext jc = JexlHelper.createContext();
        Object result = e.evaluate(jc);//计算出结果
        return result.toString();
    }
     
}

三、客户端去调用相应的webservice,这里是CalculatorWebService

我们先new->webservice client 之后新建项目叫Calculator01,将服务器端的wsdl的url添加好即可,现在我们用到页面去实现计算器的功能,这里我们客户端(其实也在服务器端,以为是B/S 架构的),我们去调用CalculatorWebService提供的方法ExpWebService来求出计算结果。

下面是包的结构:

之后我们看到我们的action中类CalculatorAction.java中的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.wss.action;
 
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts2.ServletActionContext;
 
import server.CalculatorWebServiceClient;
import server.CalculatorWebServicePortType;
 
/**
 *
* 项目名称:Calculator01 
* 类名称:CalculatorAction 
* 类描述:  接收客户端的计算式来计算结果
* 创建人:王少帅 
* 创建时间:2013-12-12 下午03:50:32      
* @version
 */
public class CalculatorAction {
    public String exp;
 
    public String getExp() {
        return exp;
    }
 
    public void setExp(String exp) {
        this.exp = exp;
    }
     
    public void execute() throws Exception{
 
        //这里使用webserviceClient来调用webservice的方法
        CalculatorWebServiceClient calculatorWebServiceClient = new CalculatorWebServiceClient();
        CalculatorWebServicePortType service  = calculatorWebServiceClient.getCalculatorWebServiceHttpPort();
         
        String result = service.expCalculator(exp); //调用expCalculator方法来实现
         
        HttpServletResponse response = ServletActionContext.getResponse();//返回页面进行查看
 
        response.getWriter().write(result);
         
    }
     
}

四、自动生成的文件分析

如图自动生成的文件:

下面是CalculatorWebServiceClient.java的文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package server;
 
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
 
public class CalculatorWebServiceClient {
 
    private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0;
 
    public CalculatorWebServiceClient() {
        create0();
        Endpoint CalculatorWebServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint"), new QName("http://server", "CalculatorWebServicePortTypeLocalBinding"), "xfire.local://CalculatorWebService");
        endpoints.put(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint"), CalculatorWebServicePortTypeLocalEndpointEP);
        Endpoint CalculatorWebServiceHttpPortEP = service0 .addEndpoint(new QName("http://server", "CalculatorWebServiceHttpPort"), new QName("http://server", "CalculatorWebServiceHttpBinding"), "http://localhost:8080/CalculatorWebService/services/CalculatorWebService");
        endpoints.put(new QName("http://server", "CalculatorWebServiceHttpPort"), CalculatorWebServiceHttpPortEP);
    }
 
    public Object getEndpoint(Endpoint endpoint) {
        try {
            return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
        } catch (MalformedURLException e) {
            throw new XFireRuntimeException("Invalid URL", e);
        }
    }
 
    public Object getEndpoint(QName name) {
        Endpoint endpoint = ((Endpoint) endpoints.get((name)));
        if ((endpoint) == null) {
            throw new IllegalStateException("No such endpoint!");
        }
        return getEndpoint((endpoint));
    }
 
    public Collection getEndpoints() {
        return endpoints.values();
    }
 
    private void create0() {
        TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
        HashMap props = new HashMap();
        props.put("annotations.allow.interface", true);
        AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
        asf.setBindingCreationEnabled(false);
        service0 = asf.create((server.CalculatorWebServicePortType.class), props);
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server", "CalculatorWebServicePortTypeLocalBinding"), "urn:xfire:transport:local");
        }
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server", "CalculatorWebServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
        }
    }
 
    public CalculatorWebServicePortType getCalculatorWebServicePortTypeLocalEndpoint() {
        return ((CalculatorWebServicePortType)(this).getEndpoint(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint")));
    }
 
    public CalculatorWebServicePortType getCalculatorWebServicePortTypeLocalEndpoint(String url) {
        CalculatorWebServicePortType var = getCalculatorWebServicePortTypeLocalEndpoint();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }
 
    public CalculatorWebServicePortType getCalculatorWebServiceHttpPort() {
        return ((CalculatorWebServicePortType)(this).getEndpoint(new QName("http://server", "CalculatorWebServiceHttpPort")));
    }
 
    public CalculatorWebServicePortType getCalculatorWebServiceHttpPort(String url) {
        CalculatorWebServicePortType var = getCalculatorWebServiceHttpPort();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }
 
    public static void main(String[] args) {
         
 
        CalculatorWebServiceClient client = new CalculatorWebServiceClient();
         
        //create a default service endpoint
        CalculatorWebServicePortType service = client.getCalculatorWebServiceHttpPort();
         
        //TODO: Add custom client code here
                //
                //service.yourServiceOperationHere();
         
        System.out.println("test client completed");
                System.exit(0);
    }
 
}

下面这个是CalculatorWebServicePortType.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package server;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
 
@WebService(name = "CalculatorWebServicePortType", targetNamespace = "http://server")
@SOAPBinding(use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface CalculatorWebServicePortType {
 
    @WebMethod(operationName = "expCalculator", action = "")
    @WebResult(name = "out", targetNamespace = "http://server")
    public String expCalculator(
            @WebParam(name = "exp", targetNamespace = "http://server") String exp);
 
}

这个是ExpCalculator.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package server;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
 
/**
 * <p>Java class for anonymous complex type.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="exp" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "exp"
})
@XmlRootElement(name = "expCalculator")
public class ExpCalculator {
 
    @XmlElement(required = true, nillable = true)
    protected String exp;
 
    /**
     * Gets the value of the exp property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getExp() {
        return exp;
    }
 
    /**
     * Sets the value of the exp property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setExp(String value) {
        this.exp = value;
    }
 
}

这个是ExpCalculatorResponse.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package server;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
 
/**
 * <p>Java class for anonymous complex type.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="out" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "out"
})
@XmlRootElement(name = "expCalculatorResponse")
public class ExpCalculatorResponse {
 
    @XmlElement(required = true, nillable = true)
    protected String out;
 
    /**
     * Gets the value of the out property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getOut() {
        return out;
    }
 
    /**
     * Sets the value of the out property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setOut(String value) {
        this.out = value;
    }
 
}

这个是ObjectFactory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package server;
 
import javax.xml.bind.annotation.XmlRegistry;
 
 
/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the server package.
 * <p>An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {
 
 
    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: server
     *
     */
    public ObjectFactory() {
    }
 
    /**
     * Create an instance of {@link ExpCalculator }
     *
     */
    public ExpCalculator createExpCalculator() {
        return new ExpCalculator();
    }
 
    /**
     * Create an instance of {@link ExpCalculatorResponse }
     *
     */
    public ExpCalculatorResponse createExpCalculatorResponse() {
        return new ExpCalculatorResponse();
    }
 
}

这个是package-info.java

1
2
@javax.xml.bind.annotation.XmlSchema(namespace = "http://server", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package server;

感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址,并且请尊重劳动成果,谢谢!










计算器之webservice实现的更多相关文章

  1. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. [LeetCode] 227. Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. Antlr4 入门

    一.ANTRL 是什么 ANTLR 是用JAVA写的语言识别工具,它用来声明语言的语法,简称为“元语言”(meta-language). ANTLR 语法识别一般分为二个阶段: 1.词法分析阶段 (l ...

  4. C语言及程序设计[套餐]课程主页

    课程链接:http://edu.csdn.net/combo/detail/30,提供全部的视频和课件下载. 三部分的课程主页.提供了为每一课时配套的自測.演示样例下载,以及程序阅读.程序填空.实践项 ...

  5. 整理一些《纸书科学计算器》的小Tips

    本文最开始是在2016年的文章 Win10应用<纸书科学计算器>更新啦! 发表之后撰写的,当时那篇文章收到了不少人点赞,应用在国内市场的日下载量也突然上涨,让我感到受宠若惊,这里要感谢Wp ...

  6. RabbitMQ系列(六)--面试官问为什么要使用MQ,应该怎么回答

    如果简历中有写到使用过RabbitMQ或者其他的消息中间件,可能在MQ方面的第一个问题就是问:为什么要使用MQ 面试官期望的回答 1.项目中有什么业务场景需要用到MQ 2.但是用了MQ,会带来很多问题 ...

  7. C# 代码中 计算某个函数 或WebService 请求花费时间

    /// 计算请求所花费的时间 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start( ...

  8. WebService技术(一)

    前言:学习笔记,以供参考 1.认识 WebService就是一种跨编程语言和跨操作系统平台的远程调用技术. Webservice就是一个独立运行的应用程序,提供了可以进行远程调用的API接口. Web ...

  9. 彻底理解webservice SOAP WSDL

    WebServices简介 先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含 ...

随机推荐

  1. javaweb作業中的幾個要點

    1.DDoS攻击原理DDoS是指分布式拒绝服务(Distributed Denial of Service):试图通过恶意请求使系统或者网络超载进而无法继续提供服务.对于一个网站来说,这意味着,该网站 ...

  2. Outing

    Outing 题目描述 Organising a group trip for the elderly can be a daunting task... Not least because of t ...

  3. hiho 1015 KMP

    input 1<=T<=20 string1 1<=strlen(string1)<=1e4 string2 2<=strlen(string2)<=1e6 out ...

  4. linux视频学习7(ssh, linux启动过程分析,加解压缩,java网络编程)

    回顾数据库mysql的备份和恢复: show databases; user spdb1; show tables; 在mysql/bin目录下 执行备份: ./mysqldump -u root - ...

  5. 444A/CF

    题目链接[http://codeforces.com/problemset/problem/444/A] 题意:给出一个无向图,找出一个联通子图,定义密度#=v(顶点值的和)/e(边值的和). 条件: ...

  6. 几个SQL语句笔试题

    1.表A和表B具有完全相同的结构,查出表A中有但表B中没有的数据: create table A( id int , name ), password ) ); create table B( id ...

  7. WinRAR5.31 注册码

    RAR registration dataState Grid Corporation Of China50000 PC usage licenseUID=5827a0bd1c43525d0a5d64 ...

  8. 提高MySQL查询速度

    参考百度知道 关于mysql处理百万级以上的数据时如何提高其查询速度的方法 最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当m ...

  9. HDU 1686 Oulipo(KMP+计算匹配成功次数)

    一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...

  10. js 基础笔记三

    词法结构: 1:区分大小写 2:特殊字符的区分,unicode转义 3:注释, //  ;  /* */ ; 4 : 标识字符和保留字 数据类型: 1原始类型 数字,字符串,布尔值.特殊的原始值(nu ...