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

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

Đ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

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 interface Local, Remote - Deploy project - Gọi phương thức từ Session Bean Phần I Bài tập step by step Bài Tạo project EJB với Session Bean Stateless có interface Local gồm phương thức sau: - countWord(String st): Trả số từ chuỗi (Mỗi từ cách khoảng trắng) - convertToNoun(String name): Trả chuỗi chuẩn dạng danh từ (Các từ cách khoảng trắng, ký tự đầu từ ký tự hoa, chứa ký tự AZ az) Step 1: Tạo ứng dụng EJB  File  New  JavaEE Enterprise Application IT Research Department @BKAP 2015 Page / 25 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 25 Enterprise Application Development in Java EE  Cấu trúc Project sau hoàn thành IT Research Department @BKAP 2015 Page / 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 / 25 Enterprise Application Development in Java EE  Cấu trúc ứng dụng sau hoàn thành Step 3: Khai báo phương thức nghiệp vụ 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 phương thức nghiệp vụ Session Bean IT Research Department @BKAP 2015 Page / 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 phương thức nghiệp vụ xây dựng  Tạo trang jsp để nhập liệu test 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 / 25 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 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 standardized.jsp gọi đến servlet kết nối với local bean Standardized Page Standardized Noun Input Noun:  Tạo Servlet Session01_02.war để gọi Local Bean  Session01_02.war  New  Other  Web  Servlet IT Research Department @BKAP 2015 Page / 25 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 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 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("Standardized Noun"); out.println(""); out.println(""); 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(""); 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) IT Research Department @BKAP 2015 Page 11 / 25 Enterprise Application Development in Java EE 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"; } }  Ứng dụng sau 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 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 Tạo ứng dụng EJB với Session Bean Stateless có interface Remote thực phương thức autolncrement(String valuecurrent, int fixedletter): phương thức trả giá trị tăng valuecurrent, fixedletter số ký tự cố định đầu VD: autolncrement(“HD00029”,2)  kết trả “HD00030” Step 1: Tạo Interface cho Stateless Session Bean  File  New Project  JavaJava Class Library IT Research Department @BKAP 2015 Page 16 / 25 Enterprise Application Development in Java EE Step 2: Sử dụng application 1, Tạo Stateless Session Bean với interface 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 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ụ Interface RemoteSessionBeanRemote.java Step 4: Triển khai phương thức nghiệp vụ Session Bean  Triển khai phương thức nghiệp vụ 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 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 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 Stateless Session Bean thực nghiệp vụ sau (Theo interface Local Remote): - Tìm từ dài xâu ký tự nhập vào Nếu có nhiều từ có độ dài chọn từ xâu (VD: Nguyen Duy Quang  Kết quả: Nguyen) - Từ dài vị trí (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 ... javax.servlet.http.HttpServletResponse; import sessionBean.S01_02SessionBeanLocal; /** * * @author admin */ public class S01_02Servlet extends HttpServlet { @EJB private S01_02SessionBeanLocal s01_02SessionBean; /** * Processes... Development in Java EE S01_02SessionBean.java package sessionBean; import javax.ejb.Stateless; /** * * @author admin */ @Stateless public class S01_02SessionBean implements S01_02SessionBeanLocal... name="convertToNoun" value="ConvertToNoun"/>  Tạo Servlet Session01_02.war để gọi Local Bean  Session01_02.war  New  Other  Web  Servlet IT Research Department @BKAP 2015 Page

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

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan