wayne

          EJB3 (6)

          Web服務客戶端可以通過bean的Web服務端點實現(xiàn)類來訪問無狀態(tài)會話Bean。在默認情況下,bean類中的所有公共方法對于Web服務客戶端都是可訪問的。@WebMethod注釋可以自定義Web服務方法,一旦在公共方法上使用該注釋,那么其他沒使用@WebMethod注釋的方法將不會對Web服務客戶端展現(xiàn)。
          首先新建一個EJB模塊,然后在EJB模塊上新建一個WebService,代碼如下:

          @WebService()
          @Stateless()
          public class Dog {
              
              @WebMethod(operationName 
          = "ganr")
              
          public String ganr() {
                  
          return "Wo-Wo-Wo";
              }


          }

          Dog類同時使用了@WebService和@Stateless注釋,web服務端點只能是無狀態(tài)會話bean,web服務本身就是無狀態(tài)的。我們還用@WebMethod注釋向Web服務客戶端公開了一個ganr方法,完成后打包部署。
          接著我們創(chuàng)建一個Web模塊,然后在Web項目上右鍵,選新建--Web服務客戶端,指定項目或者WSDL url。接著新建一個Servlet,然后右鍵調(diào)用Web服務操作,我們找到Dog服務的garn方法點確定,代碼自動生成:
          @WebServiceRef(wsdlLocation = "http://localhost:8080/DogService/Dog?wsdl")
              
          private DogService service;

          @WebServiceRef注釋聲明了一個到Web服務的引用
          try // Call Web Service Operation

                          ejb.Dog port 
          = service.getDogPort();
                          
          // TODO process result here
                          java.lang.String result = port.ganr();
                          out.println(
          "Result = " + result);
                      }
           catch (Exception ex) {
                          
          // TODO handle custom exceptions here
                      }

          service.getDogPort()方法獲取到服務的一個代理,也稱為端口。接著看jsp:
            <%
              
          try {
              ejb.DogService service 
          = new ejb.DogService();
              ejb.Dog port 
          = service.getDogPort();
              
          // TODO process result here
              java.lang.String result = port.ganr();
              out.println(
          "Result = "+result);
              }
           catch (Exception ex) {
              
          // TODO handle custom exceptions here
              }

              
          %>

          發(fā)現(xiàn)只有一點點不同,完成后部署web應用并運行測試。

          我們還可以通過一個現(xiàn)有的會話Bean創(chuàng)建Web服務,還是拿HelloWorld舉例,首先創(chuàng)建一個遠程接口:
          @Remote
          public interface HelloRemote {

              String sayHi(String name);
          }

          再創(chuàng)建一個無狀態(tài)會話bean,先已經(jīng)說過了,只能是無狀態(tài)會話bean:
          @Stateless
          public class HelloBean implements HelloRemote {

              
          public String sayHi(String name) {
                  
          return "Hi " + name; 
              }

          }

          然后新建一個Web服務Hello,只不過在彈出窗口中要選中“通過現(xiàn)有會話bean創(chuàng)建Web服務”單選框,并瀏覽指定HelloBean,代碼自動完成:
          @WebService()
          @Stateless()
          public class Hello {
              @EJB
              
          private HelloRemote ejbRef;
              
          // Add business logic below. (Right-click in editor and choose
              
          // "Web Service > Add Operation"

              @WebMethod(operationName 
          = "sayHi")
              
          public String sayHi(@WebParam(name = "name")String name) {
                  
          return ejbRef.sayHi(name);
              }

          }

          我們看到Hello服務里引用了一個HelloRemote接口,并發(fā)現(xiàn)遠程接口公開的方法也被Hello服務公開,完成后打包部署EJB模塊。接著在Web服務客戶端測試,這和之前的步驟一樣,不再贅述,直接看代碼吧:
          @WebServiceRef(wsdlLocation = "http://localhost:8080/HelloService/Hello?wsdl")
              
          private HelloService service;

              
          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  response.setContentType(
          "text/html;charset=UTF-8");
                  PrintWriter out 
          = response.getWriter();
                  
          try {
                      
          try // Call Web Service Operation

                          ejb.Hello port 
          = service.getHelloPort();
                          
          // TODO initialize WS operation arguments here
                          java.lang.String name = "Tom";
                          
          // TODO process result here
                          java.lang.String result = port.sayHi(name);
                          out.println(
          "Result = " + result);
                      }
           catch (Exception ex) {
                          
          // TODO handle custom exceptions here
                      }

           

          <%
              
          try {
              ejb.HelloService service 
          = new ejb.HelloService();
              ejb.Hello port 
          = service.getHelloPort();
               
          // TODO initialize WS operation arguments here
              java.lang.String name = "Tom";
              
          // TODO process result here
              java.lang.String result = port.sayHi(name);
              out.println(
          "Result = "+result);
              }
           catch (Exception ex) {
              
          // TODO handle custom exceptions here
              }

              
          %>

          發(fā)現(xiàn)一個問題,一個應用上不能新建兩個Web服務客戶端(Dog和Hello),只有一個能有效使用(只找到一份工件),這是為什么?

          posted on 2008-09-13 04:36 waynemao 閱讀(145) 評論(0)  編輯  收藏 所屬分類: Java ee


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          default

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汶川县| 南昌县| 象山县| 江川县| 那坡县| 东阳市| 永仁县| 勃利县| 潜江市| 景谷| 博爱县| 嘉峪关市| 济阳县| 石嘴山市| 呼和浩特市| 察哈| 历史| 凤山市| 昔阳县| 壶关县| 东辽县| 宜城市| 随州市| 宁武县| 山阳县| 修文县| 济南市| 饶阳县| 东乌| 罗田县| 石林| 鄂州市| 青河县| 文登市| 天水市| 常德市| 安多县| 泾川县| 图木舒克市| 谷城县| 英德市|