Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 13 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
13
Dung lượng
0,92 MB
Nội dung
Enterprise Application Development in Java EE Lab06 RESTful WebServices Mục tiêu - Xây dựng web service với JAX-RS API Phần I Bài tập step by step Bài Xây dựng web service với JAX-RS API Step 1: Tạo bảng Book database RESTFULDB: Step 2: Tạo project Web Application: RestfulDemo IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE Step 3: Tạo Connection RESTFULDB IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE Step 4: Tạo Restful Web Service RestfulDemo RC New Other RESTful WebServices from Database IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành Step 5: Build, Deploy and Test Web Service IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE Step 6: Tạo Client để test Web Service Tạo RESTful Java Client RestfulDemo RC New Other WebServices RESTful Java Client IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 13 Enterprise Application Development in Java EE Tạo trang jsp Servlet để test dịch vụ web service Book.jsp IT Research Department @BKAP 2015 Page 10 / 13 Enterprise Application Development in Java EE BookServlet.java package servlet; import bkap.ClientTest; import bkap.ws.Student; 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; /** * * @author Quang */ public class BookServlet extends HttpServlet { /** * 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 BookServlet"); out.println(""); out.println(""); IT Research Department @BKAP 2015 Page 11 / 13 Enterprise Application Development in Java EE String bookid = request.getParameter("bookID"); ClientTest ct = new ClientTest(); Student st = ct.find_XML(Student.class, bookid); out.println("Book ID: "+st.getBookId()+""); out.println("Book Name: "+st.getBookName()+""); out.println("Price: "+st.getPrice()+""); out.println("Description: "+st.getDesciption()+""); 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"; }// } Build and Run ứng dụng IT Research Department @BKAP 2015 Page 12 / 13 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 13 / 13 ... Enterprise Application Development in Java EE Step 6: Tạo Client để test Web Service Tạo RESTful Java Client RestfulDemo RC New Other Web Services RESTful Java Client IT Research Department... Enterprise Application Development in Java EE Step 4: Tạo Restful Web Service RestfulDemo RC New Other RESTful Web Services from Database IT Research Department @BKAP 2015 Page / 13... Page / 13 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành Step 5: Build, Deploy and Test Web Service IT Research Department @BKAP 2015 Page / 13 Enterprise Application