Enterprise Application Development in Java EE
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ự AZ và
az)
Step 1: Tạo ứng dụng EJB
File New JavaEE Enterprise Application
IT Research Department
@BKAP 2015
Page 1 / 25
Enterprise Application Development in Java EE
IT Research Department
@BKAP 2015
Page 2 / 25
Enterprise Application Development in Java EE
Cấu trúc Project sau khi hoàn thành
IT Research Department
@BKAP 2015
Page 3 / 25
Enterprise Application Development in Java EE
Step 2: Tạo Stateless Session Bean cho ứng dụng
Session01_02-ejb New Other Enterprise JavaBeans SessionBean
IT Research Department
@BKAP 2015
Page 4 / 25
Enterprise Application Development in Java EE
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
S01_02SessionBeanLocal.java
package sessionBean;
import javax.ejb.Local;
/**
*
* @author admin
*/
@Local
public interface S01_02SessionBeanLocal {
int countWord(String st);
String convertToNoun(String name);
}
Step 4: Thực thi các phương thức nghiệp vụ trong Session Bean
IT Research Department
@BKAP 2015
Page 5 / 25
Enterprise Application Development in Java EE
S01_02SessionBean.java
package sessionBean;
import javax.ejb.Stateless;
/**
*
* @author admin
*/
@Stateless
public class S01_02SessionBean implements S01_02SessionBeanLocal {
@Override
public int countWord(String st) {
st = st.trim();
String[] tokens = st.split(" ");
return tokens.length;
}
@Override
public String convertToNoun(String name) {
name = name.trim();
name = name.replaceAll("\\s+", " ");
name = name.replaceAll("[^a-zA-Z ]+", "");
name = name.toLowerCase();
String[] tokens = name.split(" ");
name = "";
for (int i = 0; i < tokens.length; i++) {
name += String.valueOf(tokens[i].charAt(0)).toUpperCase() + tokens[i].substring(1);
if (i < tokens.length - 1)
{
name += " ";
}
}
return name;
}
}
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
IT Research Department
@BKAP 2015
Page 6 / 25
Enterprise Application Development in Java EE
IT Research Department
@BKAP 2015
Page 7 / 25
Enterprise Application Development in Java EE
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
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Standardized Page</title>
</head>
<body>
Standardized Noun
<form action="S01_02Servlet" method="Post">
Input Noun: <input type="text" name="noun" size="30"/>
<input type="submit" name="countWord" value="CountWord"/>
<input type="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
IT Research Department
@BKAP 2015
Page 8 / 25
Enterprise Application Development in Java EE
IT Research Department
@BKAP 2015
Page 9 / 25
Enterprise Application Development in Java EE
S01_02Servlet.java Call Enterprise Bean (Alt + Insert)
S01_02Servlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
IT Research Department
@BKAP 2015
Page 10 / 25
Enterprise Application Development in Java EE
import javax.servlet.http.HttpServletResponse;
import sessionBean.S01_02SessionBeanLocal;
/**
*
* @author admin
*/
public class S01_02Servlet extends HttpServlet {
@EJB
private S01_02SessionBeanLocal s01_02SessionBean;
/**
* 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>Standardized Noun</title>");
out.println("</head>");
out.println("<body>");
String noun = request.getParameter("noun");
if (request.getParameter("countWord")!=null) {
int count = s01_02SessionBean.countWord(noun);
out.println("
Number of Word: "+count+"
");
} else {
String covertnoun = s01_02SessionBean.convertToNoun(noun);
out.println("
Convert To Noun: "+covertnoun+"
");
}
out.println("</body>");
out.println("</html>");
}
}
//
/**
* 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)
IT Research Department
@BKAP 2015
Page 11 / 25
Enterprise Application Development in Java EE
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";
}
}
Ứng dụng sau khi hoàn thành
IT Research Department
@BKAP 2015
Page 12 / 25
Enterprise Application Development in Java EE
Step 6: Khai báo web.xml
web.xml Pages
IT Research Department
@BKAP 2015
Page 13 / 25
Enterprise Application Development in Java EE
Step 7: Build, Deploy and Run Application
Build and Deploy
Sau khi Deploy
IT Research Department
@BKAP 2015
Page 14 / 25
Enterprise Application Development in Java EE
Run and Test Application
IT Research Department
@BKAP 2015
Page 15 / 25
Enterprise Application Development in Java EE
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
IT Research Department
@BKAP 2015
Page 16 / 25
Enterprise Application Development in Java EE
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
IT Research Department
@BKAP 2015
Page 17 / 25
Enterprise Application Development in Java EE
Cấu trúc ứng dụng sau khi hoàn thành
IT Research Department
@BKAP 2015
Page 18 / 25
Enterprise Application Development in Java EE
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)
IT Research Department
@BKAP 2015
Page 19 / 25
Enterprise Application Development in Java EE
RemoteSessionBean.java
package sessionBean;
import javax.ejb.Stateless;
/**
*
* @author admin
*/
@Stateless
public class RemoteSessionBean implements RemoteSessionBeanRemote {
@Override
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;
IT Research Department
@BKAP 2015
Page 20 / 25
Enterprise Application Development in Java EE
}
}
Step 5: Tạo Client gọi Stateless Session Bean qua interface Remote
File New Project Java EE Enterprise Application Client
IT Research Department
@BKAP 2015
Page 21 / 25
Enterprise Application Development in Java EE
Step 6: Triển khai hàm main của Client
Gọi RemoteSessionBean (Alt + Insert)
IT Research Department
@BKAP 2015
Page 22 / 25
Enterprise Application Development in Java EE
Main.java
package s01_02client;
import java.util.Scanner;
import javax.ejb.EJB;
import sessionBean.RemoteSessionBeanRemote;
/**
*
* @author admin
*/
public class Main {
@EJB
private static RemoteSessionBeanRemote remoteSessionBean;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
IT Research Department
@BKAP 2015
Page 23 / 25
Enterprise Application Development in Java EE
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
IT Research Department
@BKAP 2015
Page 24 / 25
Enterprise Application Development in Java EE
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)
IT Research Department
@BKAP 2015
Page 25 / 25