Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 22 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
22
Dung lượng
2,01 MB
Nội dung
Enterprise Application Development in Java EE Lab01 - 02 Introduction to WebServices SOAP, WSDL and UDDI Mục tiêu - Tạo JAX-WS Web Service - Tạo Web Service - Tạo dịch vụ cho Web Service - Deploy Test Web Service - Tạo Client Phần I Bài tập step by step Bài Tạo Web Service có dịch vụ sau: Hiển thị chào người dùng Cộng hai số Step 1: Tạo Project Java Web File New Project Java Web Web Application IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Step 2: Tạo Web Service từ project DemoWS DemoWS RC New Other WebServices Web Service IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành Step 3: Tạo dịch vụ cho Web Service IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Tạo dịch vụ nhận tham số tên người dùng hiển thị câu chào người dùng IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Tạo dịch vụ tính tổng số nguyên IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE DemoWebServiceLab1.java package bkap.ws; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.ejb.Stateless; /** * * @author Quang */ @WebService(serviceName = "DemoWebServiceLab1") @Stateless() public class DemoWebServiceLab1 { /** * Web service operation * * @param user * @return */ @WebMethod(operationName = "helloUser") public String helloUser(@WebParam(name = "user") String user) { String st = "Hello " + user; return st; } /** * Web service operation * * @param a * @param b * @return */ @WebMethod(operationName = "addNum") public int addNum(@WebParam(name = "a") int a, @WebParam(name = "b") int b) { int kq = a + b; return kq; } } Step 4: Build, Deploy and Test Web Service IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Bài Tạo loại Client sử dụng dịch vụ Web Service Java Class in Java SE Application IT Research Department @BKAP 2015 Page / 22 Enterprise Application Development in Java EE Servlet in Web Application Step 1: Tạo Client Java Class File New Project Java Java Application IT Research Department @BKAP 2015 Page 10 / 22 Enterprise Application Development in Java EE CalculatorClientApp RC New Other WebServices Web Service Client IT Research Department @BKAP 2015 Page 11 / 22 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành IT Research Department @BKAP 2015 Page 12 / 22 Enterprise Application Development in Java EE Kéo phương thức addNum vào lớp CalculatorClientApp.java CalculatorClientApp.java package calculatorclientapp; IT Research Department @BKAP 2015 Page 13 / 22 Enterprise Application Development in Java EE import java.util.Scanner; /** * * @author Quang */ public class CalculatorClientApp { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Nhap vao so a: "); int a = sc.nextInt(); System.out.println("Nhap vao so b: "); int b = sc.nextInt(); int tong = addNum(a, b); System.out.println("Tong cua so a va b la: " + tong); } private static int addNum(int a, int b) { bkap.ws.DemoWebServiceLab1_Service service = new bkap.ws.DemoWebServiceLab1_Service(); bkap.ws.DemoWebServiceLab1 port = service.getDemoWebServiceLab1Port(); return port.addNum(a, b); } } Build and Run ứng dụng Step 2: Tạo Client Servlet in Web Application File New Project Java Web Web Application IT Research Department @BKAP 2015 Page 14 / 22 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 15 / 22 Enterprise Application Development in Java EE HelloClientWeb Source Packages New Other Web Servlet IT Research Department @BKAP 2015 Page 16 / 22 Enterprise Application Development in Java EE Add Web Service: HelloClientWeb RC New Other WebServices Web Service Client IT Research Department @BKAP 2015 Page 17 / 22 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 18 / 22 Enterprise Application Development in Java EE Kéo dịch vụ helloUser vào HelloServlet.java IT Research Department @BKAP 2015 Page 19 / 22 Enterprise Application Development in Java EE HelloServlet.java package bkap.servlet; import bkap.ws.DemoWebServiceLab1_Service; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.WebServiceRef; /** * * @author Quang */ public class HelloServlet extends HttpServlet { @WebServiceRef(wsdlLocation = "WEBINF/wsdl/localhost_56420/DemoWebServiceLab1/DemoWebServiceLab1.wsdl") private DemoWebServiceLab1_Service service; /** * Processes requests for both HTTP GET and POST * methods * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here You may use following sample code */ out.println(""); out.println(""); out.println(""); out.println("Servlet HelloServlet"); out.println(""); IT Research Department @BKAP 2015 Page 20 / 22 Enterprise Application Development in Java EE out.println(""); out.println("Hello Page"); String st = helloUser("Quangnd"); out.println(st); out.println(""); out.println(""); } } // /** * Handles the HTTP GET method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// private String helloUser(java.lang.String user) { // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe // If the calling of port operations may lead to race condition some synchronization is required bkap.ws.DemoWebServiceLab1 port = service.getDemoWebServiceLab1Port(); return port.helloUser(user); } IT Research Department @BKAP 2015 Page 21 / 22 Enterprise Application Development in Java EE } Build and Run IT Research Department @BKAP 2015 Page 22 / 22 ... Other Web Services Web Service Client IT Research Department @BKAP 2 015 Page 11 / 22 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành IT Research Department @BKAP 2 015 Page... synchronization is required bkap.ws.DemoWebServiceLab1 port = service.getDemoWebServiceLab1Port(); return port.helloUser(user); } IT Research Department @BKAP 2 015 Page 21 / 22 Enterprise Application Development... Department @BKAP 2 015 Page 14 / 22 Enterprise Application Development in Java EE IT Research Department @BKAP 2 015 Page 15 / 22 Enterprise Application Development in Java EE HelloClientWeb Source