WebService入门

webservice入门篇

一、实验环境
     
   win2k + jdk1.6  + javee5.0  + Myeclipse5.1
         jdk和javee5.0均可从
http://java.sun.com/javase/downloads/index.jsp下载,安装文件名为jdk-6-windows-i586.exe,java_ee_sdk-5_02-windows.exe;没有myeclipse的也可以用eclipse代替,只要ide能执行ant脚本就可以.

二、第一个最简单的例子
          jsee5安装以后会在
系统中建立一个Application Serverpe9,这是sun自带的网络服务器,和tomcat、weblogic的性质类似。
      在D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws下有一个自带的web service入门例子,(D:\Sun\SDK\为我机器上javaee5的安装路径)
我们就先实验它,来理解webservice的本质
     1、把jdk的安装路径如D:\Java\jdk1.6.0\bin 加到系统的path环境变量中
     2、运行sun自带的网络服务器,具体方法为
         开始->程序->Sun Microsystems->Application Server PE 9->Start Default Server
        然后当弹出的cmd窗口中出现提示“按任意键继续时”输入回车,窗口会关闭,此时在
浏览器输入http://localhost:8080,应该出现如下内容:
       Sun Java System Application Server Platform Edition 9.0 
       Your server is up and running!
       说明服务器已经
启动

    3、在Myeclipse打开刚才的例子目录D:\Sun\SDK\samples\javaee\webservices\hello-jaxws下的build.xml文件,这个一个ant脚本,具体含义我们以后再讲,现在先执行它
    4、在build.xml文件中
单击右键,在弹出菜单中选择"run as"->"1 antbuild",此时build.xml里的内容会被执行,在Myeclipse的console中会输出:
        buildfile: D:\Sun\SDK\samples\javaee5 \webservices\hellojaxws\build.xml
        init:compile-deploy-service:
        [echo] d:/Sun/SDK
        get-artifacts-windows:
        get-artifacts-unix:
        get-artifacts:
        compile-client:
        [javac] Compiling 1 source file to D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
        run-client-windows:
        [exec] Hello result = Hello Administrator!
        run-client-unix:
        run-client:
        all:
        BUILD SUCCESSFUL
        Total time: 43 seconds

        其中
        [exec] Hello result = Hello Administrator!
        的输出结果说明客户端调用服务器的webservice已经成功。
        第一个例子就完成了。我们下面会对这个例子进行详细讲解.

 

三、例子源代码剖析
      
D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws(以后简称为hello-jaxws)
项目里的源文件只有三个:(1) hello-jaxws\src\endpoint\Hello.java   提供webservice的
服务器端实现类(2)hello-jaxws\src\client\Client.java  调用webservice的客户端测试类(3)hello-jaxws\build.xml  自动编译的ant脚本

 

下面我们把这三个文件的内容看一下
     1、服务器端的service实现类文件Hello.java,

     /* src\endpoint\Hello.java文件的内容 */
     package endpoint;
     import javax.jws.WebService;
     @WebService
     public class Hello
     {
        public String getHello(String name)
        {
             return "Hello " + name + "!";
        }
     }
    有这个文件可以看出,编写一个service的实现类,把编写普通的java类差不多,只不过又多了三点:
           ①要在该类前一个@WebService说明,
           ②引入javax.jws.WebService;
           ③在公开的方法前加@WebMethod说明,如果不加的话,所有的public方法都会自动变成service的对外接口.
    2、客户端测试类的实现文件
     package client;
     import javax.xml.ws.WebServiceRef;
     import endpoint.HelloService;
     import endpoint.Hello;
     public class Client
     {
         @WebServiceRef(wsdlLocation="http://localhost:8080/Hello/HelloService?WSDL")
         static HelloService service;
         public static void main(String[] args)
         {
                 Client client = new Client();
                 client.doHello();
         }
         public void doHello()
         {
             try
             {
                  Hello port = service.getHelloPort();
                  String ret = port.getHello(System.getProperty("user.name"));
                  System.out.println("Hello result = " + ret);
              }catch(Exception e){
                        e.printStackTrace();
              }
         }
    }
    客户端调用代码要点为:
    ①导入WebServiceRef包
       import javax.xml.ws.WebServiceRef;
    ②导入本地生成的stub类,另外编译时也要指明stub类的路径
       import endpoint.HelloService;
       import endpoint.Hello;
    ③指明服务器的wsdl路径
       @WebServiceRef(wsdlLocation="http://localhost:8080/myhello/HelloService?WSDL")
    ④声明一个静态的service对象
       static HelloService service;
    ⑤对要调用的远程方法声明一个代理对象,通过代理来调用真正的远程方法
       Hello port = service.getHelloPort();
       String ret = port.getHello(System.getProperty("user.name"));

    3、ant 脚本build.xml

   <!-- ant 脚本build.xml的内容 -->
   <?xml version="1.0" encoding="UTF-8"?>
   <project name="hello-jaxws" default="all" basedir=".">
   <!-- include user specific build properties -->

   <!-- 导入预先
j2ee 预先写好的 设置 文件--> <property file="../../../bp-project/build.properties"/>
       <property file="${user.home}/build.properties"/>
       <property file="../../../bp-project/app-server.properties"/> <!-- 设置发布目录和类的输出目录 -->
       <property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/>
       <property name="classesdir" value="./build"/> <!-- 设置java的类库路径 -->
       <path id="classpath">
               <pathelement location="${javaee.home}/lib/j2ee.jar"/> 
               <pathelement location="${classesdir}"/>
       </path> <!-- 项目的最终任务 -->
       <target name="all" depends="run-client"> <!--antcall target="restore"/--> </target>
       <!-- 测试 操作系统 环境-->
       <target name="init">
            <condition property="windows">
                   <os family="windows" />
            </condition>
            <condition property="unix">
                   <os family="unix" />
            </condition>
       </target>
  <!-- 编译服务器端并把它自动发布到sun的服务器上 -->
       <target name="compile-deploy-service" depends="init">
             <mkdir dir="${classesdir}"/>
             <echo message="${javaee.home}"/>
             <javac srcdir="./src" includes="endpoint/**" destdir="${autodeploydir}" classpath="${javaee.home}/lib/j2ee.jar"/>
             <waitfor maxwait="100" maxwaitunit="second">
             <or>
                  <available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
                  <available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
              </or>
          </waitfor>
          <condition property="deploy_succeeded">
                <available file="${autodeploydir}/endpoint/Hello.class_deployed"/>
          </condition>
          <condition property="deploy_failed">
                <available file="${autodeploydir}/endpoint/Hello.class_deployFailed"/>
          </condition>
     </target>
     <target name="get-artifacts" depends="compile-deploy-service,get-artifacts-windows,get-artifacts-unix"/>
<!-- 生成客户端所需的stub -->
     <target name="get-artifacts-windows" if="windows">
         <exec executable="${javaee.home}/bin/wsimport.bat">
             <arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL"/>
          </exec>
     </target>
     <target name="get-artifacts-unix" if="unix">
         <exec executable="${javaee.home}/bin/wsimport">
             <arg line="-keep -d ${classesdir} http://${javaee.server.name}:${javaee.server.port}/Hello/HelloService?WSDL"/>
         </exec>
     </target>
    <!-- 编译客户端 -->
     <target name="compile-client" depends="get-artifacts">
         <javac srcdir="./src/client" destdir="${classesdir}">
             <classpath refid="classpath"/>
         </javac>
     </target>

     <target name="run-client" depends="compile-client,run-client-windows,run-client-unix"/>

     <!-- 执行客户端 -->
     <target name="run-client-windows" if="windows">
         <exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}">
             <arg value="client.Client"/>
         </exec>
     </target>
     <target name="run-client-unix" if="unix">
          <exec executable="${javaee.home}/bin/appclient" dir="${classesdir}" failifexecutionfails="false">
               <arg value="client.Client"/>
          </exec>
     </target>

   <!-- 以下几个任务用与清理和卸载-->
    <!-- 删除生成的类文件-->
    <target name="clean">
          <delete dir="${classesdir}"/>
    </target>
    <!-- 删除和卸载服务器的webservice-->
    <target name="restore">
          <delete>
                  <fileset dir="${autodeploydir}/endpoint" includes="Hello*.*"/>
          </delete>
     </target>
     <target name="undeploy">
         <antcall target="restore"/>
     </target>
  </project>

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
使用 JavaScript 编写的 Squareshooter 游戏及其源代码   项目:使用 JavaScript 编写的 Squareshooter 游戏(附源代码) 这款游戏是双人游戏。这是一款使用 JavaScript 编写的射击游戏,带有门户和强化道具。在这里,每个玩家都必须控制方形盒子(作为射手)。这款射击游戏的主要目标是射击对手玩家以求生存。当它射击对手时,它会获得一分。 游戏制作 该游戏仅使用 HTML 和 JavaScript 开发。该游戏的 PC 控制也很简单。 对于玩家 1: T:朝你上次动作的方向射击 A:向左移动 D:向右移动 W:向上移动 S:向下移动 对于玩家2: L:朝你上次移动的方向射击 左箭头:向左移动 右箭头:向右移动 向上箭头:向上移动 向下箭头:向下移动 游戏会一直进行,直到您成功射击对手或对手射击您为止。游戏得分显示在顶部。所有游戏功能均由 JavaScript 设置,而布局和其他次要功能则由 HTML 设置。 如何运行该项目? 要运行此项目,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要运行此游戏,首先,通过单击 index.html 文件在浏览器中打开项目。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值