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

Thực hành EJB (Enterprise Java Bean) Lab 1 + 2

25 260 1

Đ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 25
Dung lượng 1,85 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

IT Research Department @BKAP 2015 Page 1 / 25 Lab 01 + 02 Introduction to Business Components Enterprise Java Bean Mục tiêu - Tạo project EJB - Tạo Session Bean Stateless theo 2 inte

Trang 1

IT Research Department @BKAP 2015 Page 1 / 25

Lab 01 + 02 Introduction to Business Components

Enterprise Java Bean

Mục tiêu

- Tạo project EJB

- Tạo Session Bean Stateless theo 2 interface Local, Remote

- Deploy project

- Gọi các phương thức từ Session Bean

Phần I Bài tập step by step

Bài 1 Tạo project EJB với Session Bean Stateless có interface là Local gồm các phương thức sau:

- countWord(String st): Trả về số từ trong chuỗi (Mỗi từ cách nhau bởi khoảng trắng)

- convertToNoun(String name): Trả về một chuỗi chuẩn ở dạng danh từ (Các từ cách nhau bởi 1 khoảng trắng, ký tự đầu của mỗi từ là ký tự hoa, chỉ chứa các ký tự AZ và az)

Step 1: Tạo ứng dụng EJB

 File  New  JavaEE Enterprise Application

Trang 3

IT Research Department @BKAP 2015 Page 3 / 25

 Cấu trúc Project sau khi hoàn thành

Trang 4

Step 2: Tạo Stateless Session Bean cho ứng dụng

 Session01_02-ejb  New  Other  Enterprise JavaBeans  SessionBean

Trang 5

IT Research Department @BKAP 2015 Page 5 / 25

 Cấu trúc ứng dụng sau khi hoàn thành

Step 3: Khai báo phương thức nghiệp vụ trong Session Bean Local

Trang 6

String[] tokens = st.split(" ");

return tokens.length;

for (int i = 0; i < tokens.length; i++) {

name += String.valueOf(tokens[i].charAt(0)).toUpperCase() + tokens[i].substring(1);

Step 5: Xây dựng Client gọi Local Bean để test các phương thức nghiệp vụ đã xây dựng

 Tạo trang jsp để nhập dữ liệu test các phương thức nghiệp vụ đã xây dựng

 Session01_02.war  Web Page  New  Other  Web  JSP

Trang 7

IT Research Department @BKAP 2015 Page 7 / 25

Trang 8

 Tạo form nhập danh từ cần đếm số từ hay cần chuẩn hóa trong standardized.jsp và

gọi đến servlet kết nối với local bean

<%@ pagecontentType="text/html"pageEncoding="UTF-8"%>

<!DOCTYPEhtml>

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Standardized Page</title>

</head>

<body>

<h1>Standardized Noun</h1>

<formaction="S01_02Servlet"method="Post">

Input Noun: <inputtype="text"name="noun"size="30"/>

<br><br>

<inputtype="submit"name="countWord"value="CountWord"/>

<inputtype="submit"name="convertToNoun"value="ConvertToNoun"/>

</form>

</body>

</html>

 Tạo Servlet trong Session01_02.war để gọi Local Bean

 Session01_02.war  New  Other  Web  Servlet

Trang 9

IT Research Department @BKAP 2015 Page 9 / 25

Trang 10

 S01_02Servlet.java  Call Enterprise Bean (Alt + Insert)

Trang 11

IT Research Department @BKAP 2015 Page 11 / 25

* @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 */

String noun = request.getParameter("noun");

if (request.getParameter("countWord")!=null) {

int count = s01_02SessionBean.countWord(noun);

out.println("<h1>Number of Word: "+count+"</h1>");

} else {

String covertnoun = s01_02SessionBean.convertToNoun(noun);

out.println("<h1>Convert To Noun: "+covertnoun+"</h1>");

* @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)

Trang 12

throws ServletException, IOException {

* @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 {

public String getServletInfo() {

return "Short description";

}

}

 Ứng dụng sau khi hoàn thành

Trang 13

IT Research Department @BKAP 2015 Page 13 / 25

Step 6: Khai báo web.xml

 web.xml  Pages

Trang 14

Step 7: Build, Deploy and Run Application

 Build and Deploy

 Sau khi Deploy

Trang 15

IT Research Department @BKAP 2015 Page 15 / 25

 Run and Test Application

Trang 16

Bài 2 Tạo ứng dụng EJB với Session Bean là Stateless có interface là Remote thực hiện phương thức autolncrement(String valuecurrent, int fixedletter): phương thức trả về giá trị tăng tiếp theo của valuecurrent, fixedletter là số ký tự cố định ở đầu

VD: autolncrement(“HD00029”,2)  kết quả trả về là “HD00030”

Step 1: Tạo Interface cho Stateless Session Bean

 File  New Project  Java  Java Class Library

Trang 17

IT Research Department @BKAP 2015 Page 17 / 25

Step 2: Sử dụng application của bài 1, Tạo Stateless Session Bean với interface là Remote

 Session01_02-ejb  New  Other  Enterprise JavaBeans  Session Bean

Trang 18

 Cấu trúc ứng dụng sau khi hoàn thành

Trang 19

IT Research Department @BKAP 2015 Page 19 / 25

Step 3: Khai báo phương thức nghiệp vụ trên Interface

RemoteSessionBeanRemote.java

Step 4: Triển khai phương thức nghiệp vụ trên Session Bean

 Triển khai phương thức nghiệp vụ trên RemoteSessionBean.java (Alt + Insert)

Trang 20

public String autoIncrement(String valuecurrent, int fixedletter) {

int count = valuecurrent.length();

String nextcurrent = valuecurrent.substring(0, fixedletter);

valuecurrent = valuecurrent.substring(fixedletter);

int y = Integer.parseInt(valuecurrent);

y = y + 1;

String stint = Integer.toString(y);

int count0 = count - (stint.length() + fixedletter);

for (int i = 0; i < count0; i++) {

nextcurrent += "0";

}

nextcurrent += stint;

return nextcurrent;

Trang 21

IT Research Department @BKAP 2015 Page 21 / 25

}

}

Step 5: Tạo Client gọi Stateless Session Bean qua interface Remote

 File  New Project  Java EE  Enterprise Application Client

Trang 22

Step 6: Triển khai hàm main của Client

 Gọi RemoteSessionBean (Alt + Insert)

Trang 23

IT Research Department @BKAP 2015 Page 23 / 25

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Trang 24

System.out.println("Enter Current Value");

String currentvalue = in.nextLine();

System.out.println("Enter Fixed Letter");

int fixedletter = in.nextInt();

String nextvalue = remoteSessionBean.autoIncrement(currentvalue, fixedletter);

System.out.println("Next Value: "+nextvalue);

}

 Cấu trúc ứng dụng sau khi hoàn thành

Trang 25

IT Research Department @BKAP 2015 Page 25 / 25

Step 7: Build and Run Client

Phần II Bài tập tự làm

Bài tập: Tạo ứng dụng EJB có SessionBean là Stateless Session Bean thực hiện nghiệp vụ sau (Theo interface là Local và Remote):

- Tìm từ dài nhất trong xâu ký tự nhập vào Nếu có nhiều từ có độ dài bằng nhau thì chọn

từ đầu tiên trong xâu (VD: Nguyen Duy Quang  Kết quả: Nguyen)

- Từ dài nhất ở vị trí nào (VD: Nguyen  1, Duy  2, Quang  3)

- Nhập vào tên người theo thứ tự Họ-Đệm-Tên, chuyển thành Tên-Họ-Đệm (VD: Nguyen Duy Quang  Quang Nguyen Duy)

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

TỪ KHÓA LIÊN QUAN

w