IT Research Department @BKAP 2015 Page 1 / 13Lab 06 RESTful Web Services 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 1 Xây dựng web service với JA
Trang 1IT Research Department @BKAP 2015 Page 1 / 13
Lab 06 RESTful Web Services
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 1
Xây dựng web service với JAX-RS API
Step 1: Tạo bảng Book trong database RESTFULDB:
Step 2: Tạo project Web Application: RestfulDemo
Trang 2Step 3: Tạo Connection RESTFULDB
Trang 3IT Research Department @BKAP 2015 Page 3 / 13
Step 4: Tạo Restful Web Service
RestfulDemo RC New Other RESTful Web Services from Database
Trang 5IT Research Department @BKAP 2015 Page 5 / 13
Trang 6 Ứng dụng sau khi hoàn thành
Step 5: Build, Deploy and Test Web Service
Trang 7IT Research Department @BKAP 2015 Page 7 / 13
Trang 8Step 6: Tạo Client để test Web Service
Tạo RESTful Java Client
RestfulDemo RC New Other Web Services RESTful Java Client
Trang 9IT Research Department @BKAP 2015 Page 9 / 13
Trang 10 Tạo trang jsp và Servlet để test các dịch vụ của web service
Book.jsp
Trang 11IT Research Department @BKAP 2015 Page 11 / 13
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 <code> GET </code> and <code> POST </code>
* 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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BookServlet</title>");
out.println("</head>");
out.println("<body>");
Trang 12String bookid = request.getParameter("bookID");
ClientTest ct = new ClientTest();
Student st = ct.find_XML(Student.class, bookid);
out.println("Book ID: "+st.getBookId()+"<br>");
out.println("Book Name: "+st.getBookName()+"<br>");
out.println("Price: "+st.getPrice()+"<br>");
out.println("Description: "+st.getDesciption()+"<br>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code> GET </code> 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 <code> POST </code> 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";
}// </editor-fold>
}
Build and Run ứng dụng
Trang 13IT Research Department @BKAP 2015 Page 13 / 13