1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành Web Services Lab 6

13 240 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

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

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 1

IT 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 2

Step 3: Tạo Connection RESTFULDB

Trang 3

IT Research Department @BKAP 2015 Page 3 / 13

Step 4: Tạo Restful Web Service

 RestfulDemo  RC  New  Other  RESTful Web Services from Database

Trang 5

IT 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 7

IT Research Department @BKAP 2015 Page 7 / 13

Trang 8

Step 6: Tạo Client để test Web Service

 Tạo RESTful Java Client

 RestfulDemo  RC  New  Other  Web Services  RESTful Java Client

Trang 9

IT 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 11

IT 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 12

String 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 13

IT Research Department @BKAP 2015 Page 13 / 13

Ngày đăng: 07/05/2018, 16:46

TỪ KHÓA LIÊN QUAN

w