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

Thực hành EJB (Enterprise Java Bean) Lab 5

17 215 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 17
Dung lượng 0,91 MB

Nội dung

Enterprise Application Development in Java EE Lab 05 Singleton Session Beans Mục tiêu - Thao tác với Singleton Session Beans Phần I Bài tập step by step Bài Tạo project EJB với Singleton Session Beans có interface Remote cho phép đăng ký, đăng nhập User ChatRoom: UserChat Column Data Type Characteristic Description UserID Int PK, Auto Increment(1,1) Mã User UserName Nvarchar(50) Not Null Tên đăng nhập Password Nvarchar(50) Not Null Mã đăng nhập Ucreated Date Not Null Ngày đăng ký Mail Nvarchar(50) Allow Null Mail User Address Nvarchar(100) Allow Null Địa Phone Nvarchar(50) Allow Null Số điện thoại Project thực công việc sau: - Tạo class UserChat tương ứng với bảng UserChat CSDL - Tạo Singleton Session Bean Remote gồm phương thức sau: + registerUser: Đăng ký User + login: Đăng nhập User IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE Step 1: Tạo Table UserChat Database EJB procedure thực nghiệp vụ registerUser, login  Tạo Database EJB Table UserChat Create DataBase EJB go USE [EJB] GO /****** Object: Table [dbo].[UserChat] Script Date: 6/3/2016 11:31:31 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[UserChat]( [UserID] [int] IDENTITY(1,1) NOT NULL, [UserName] [nvarchar](50) NOT NULL, [Password] [nvarchar](50) NOT NULL, [UCreated] [date] NOT NULL, [Mail] [nvarchar](50) NULL, [Address] [nvarchar](100) NULL, [Phone] [nvarchar](50) NULL, [StatusLogin] [int] NULL, CONSTRAINT [PK_EUser] PRIMARY KEY CLUSTERED ( [UserID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO  Tạo Procedure  checkLogin USE [EJB] GO CREATE PROCEDURE [dbo].[checkLogin] @userName nvarchar(50), @password nvarchar(50), @checkNumber int OUTPUT, @numberOnline int OUTPUT AS BEGIN SELECT @checkNumber = COUNT(*) FROM UserChat u WHERE u.UserName = @userName and u.Password = @password if @checkNumber > BEGIN UPDATE UserChat SET StatusLogin = 1; SELECT @numberOnline = COUNT(*) FROM UserChat u WHERE u.StatusLogin = 1; END END IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE  registerUser USE [EJB] GO CREATE PROCEDURE [dbo].[registerUser] @userName nvarchar(50), @password nvarchar(50), @mail nvarchar(50), @address nvarchar(100), @phone nvarchar(50) AS BEGIN DECLARE @date date; SET @date = GETDATE(); INSERT INTO UserChat VALUES (@userName,@password,@date,@mail,@address,@phone,0) END Step 2: Tạo Project EJB tên Session05 với Session Bean Singleton - tên S05SingletonBean Interface Remote – tên S05InterfaceRemote (Như hướng dẫn Lab trước) IT Research Department @BKAP 2015 Page / 17 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ụ S05SingletonBeanRemote S05SingletonBeanRemote.java package bean; import javax.ejb.Remote; /** * * @author Quang */ @Remote public interface S05SingletonBeanRemote { public String registerUser(String userName, String password, String mail, String address, String phone); public int login(String userName, String password); } Step 4: Xây dựng java class DBConnection kết nối với DataBase SQL Server 2012 (Đặt package util) package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * * @author admin */ public class DBConnection { IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=EJB"; private static final String DB_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String user_db = "sa"; private static final String pass_db = "Gacon1984"; public static Connection getDBConnection() { Connection conn = null; try { Class.forName(DB_DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(URL, user_db, pass_db); } catch (SQLException ex) { ex.printStackTrace(); } return conn; } } Step 5: Thực thi phương thức nghiệp vụ S05SingletonBean S03_04StatelessBean.java package bean; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.Types; import javax.ejb.Singleton; import util.DBConnection; /** * * @author Quang */ @Singleton public class S05SingletonBean implements S05SingletonBeanRemote { @Override public String registerUser(String userName, String password, String mail, String address, String phone) { Connection conn = null; CallableStatement callableStatement = null; try { conn = DBConnection.getDBConnection(); String registerUser = "{call registerUser(?,?,?,?,?)}"; callableStatement = conn.prepareCall(registerUser); callableStatement.setString(1, userName); callableStatement.setString(2, password); callableStatement.setString(3, mail); callableStatement.setString(4, address); callableStatement.setString(5, password); callableStatement.executeUpdate(); IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE return "Success"; } catch (Exception e) { e.printStackTrace(); return "Error"; } } @Override public int login(String userName, String password) { Connection conn = null; CallableStatement callableStatement = null; int check = 0; int numberOnline = 0; try { conn = DBConnection.getDBConnection(); String checkLogin = "{call checkLogin(?,?,?,?)}"; callableStatement = conn.prepareCall(checkLogin); callableStatement.setString(1, userName); callableStatement.setString(2, password); callableStatement.registerOutParameter(3, Types.INTEGER); callableStatement.registerOutParameter(4, Types.INTEGER); callableStatement.execute(); check = callableStatement.getInt(3); numberOnline = callableStatement.getInt(4); } catch (Exception e) { e.printStackTrace(); } if (check > 0) { return numberOnline; } else { return 0; } } } Step 6: Build and Deploy Project Session05 Step 7: Tạo Project Web Application với Framework Struts2 làm client cho User đăng ký đăng nhập IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE Step 8: Tạo Entity Class tên UserChat table UserChat package entities (Có get,set, constructor) UserChat.java package entities; import java.util.Date; /** * * @author Quang */ public class UserChat { private int userID; private String userName; private String password; private Date uCreated; private String mail; private String address; private String phone; public UserChat() { } public int getUserID() { return userID; } public void setUserID(int userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getuCreated() { return uCreated; } public void setuCreated(Date uCreated) { this.uCreated = uCreated; IT Research Department @BKAP 2015 Page / 17 Enterprise Application Development in Java EE } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } Step 9: Tạo css cho roomchat Chat.css content { width: 600px; height: 400px; overflow: auto; border: 1px solid #991111; } msg-input { width: 400px; border: 1px solid #119911; } Step 10: Tạo trang jsp User đăng ký (register.jsp), đăng nhập (login.jsp) chat (roomchat.jsp)  register.jsp Chat Room Register User Registration IT Research Department @BKAP 2015 Page 10 / 17 Enterprise Application Development in Java EE  login.jsp Login Room Chat Login Đăng Ký  roomchat.jsp Room Chat Page Your name: ${userName} Number Online: ${numberOnline} IT Research Department @BKAP 2015 Page 11 / 17 Enterprise Application Development in Java EE Step 11: Tạo Action Struts thực đăng ký đăng nhập cho User (gọi qua Enterprise java Bean S05SingletonBeanRemote) package action; import bean.S05SingletonBeanRemote; import com.opensymphony.xwork2.ActionSupport; import entities.UserChat; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; /** * * @author Quang */ public class UserAction extends ActionSupport implements ServletRequestAware{ S05SingletonBeanRemote s05SingletonBean = lookupS05SingletonBeanRemote(); private UserChat userRegister; private UserChat userLogin; private HttpServletRequest request; public UserAction() { userRegister = new UserChat(); userLogin = new UserChat(); } public String register() { String result = null; try { result = s05SingletonBean.registerUser(userRegister.getUserName(), userRegister.getPassword(), userRegister.getMail(), userRegister.getAddress(), userRegister.getPhone()); if (result.equals("Success")) { return SUCCESS; } return ERROR; } catch (Exception e) { e.printStackTrace(); return ERROR; } } public String login(){ int numberOnline = 0; try { numberOnline = s05SingletonBean.login(userLogin.getUserName(), userLogin.getPassword()); if (numberOnline>0) { request.getSession().setAttribute("userName", userLogin.getUserName()); IT Research Department @BKAP 2015 Page 12 / 17 Enterprise Application Development in Java EE request.getSession().setAttribute("numberOnline", numberOnline); return SUCCESS; } return ERROR; } catch (Exception e) { e.printStackTrace(); return ERROR; } } private S05SingletonBeanRemote lookupS05SingletonBeanRemote() { try { Context c = new InitialContext(); return (S05SingletonBeanRemote) c.lookup("java:global/Session05/Session05ejb/S05SingletonBean!bean.S05SingletonBeanRemote"); } catch (NamingException ne) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne); throw new RuntimeException(ne); } } public UserChat getUserRegister() { return userRegister; } public void setUserRegister(UserChat userRegister) { this.userRegister = userRegister; } public UserChat getUserLogin() { return userLogin; } public void setUserLogin(UserChat userLogin) { this.userLogin = userLogin; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } @Override public void setServletRequest(HttpServletRequest hsr) { this.request = hsr; } } Step 12: Khai báo struts.xml IT Research Department @BKAP 2015 Page 13 / 17 Enterprise Application Development in Java EE register.jsp login.jsp register.jsp roomChat.jsp login.jsp  Cấu trúc Project sau hoàn thành IT Research Department @BKAP 2015 Page 14 / 17 Enterprise Application Development in Java EE Step 13: Build and Run Application IT Research Department @BKAP 2015 Page 15 / 17 Enterprise Application Development in Java EE Phần II Bài Tập Tự Làm Phát triển tiếp tập hướng dẫn Step by Step, Thêm chức sau: IT Research Department @BKAP 2015 Page 16 / 17 Enterprise Application Development in Java EE - Phát triển chức Log Out Room Chat, log out cập nhật lại Status User bảng Userchat IT Research Department @BKAP 2015 Page 17 / 17 ... Step 5: Thực thi phương thức nghiệp vụ S05SingletonBean S03_04StatelessBean .java package bean; import java. sql.CallableStatement; import java. sql.Connection; import java. sql.Types; import javax .ejb. Singleton;... private S05SingletonBeanRemote lookupS05SingletonBeanRemote() { try { Context c = new InitialContext(); return (S05SingletonBeanRemote) c.lookup( "java: global/Session 05/ Session0 5ejb/ S05SingletonBean!bean.S05SingletonBeanRemote");... hoàn thành Step 3: Khai báo phương thức nghiệp vụ S05SingletonBeanRemote S05SingletonBeanRemote .java package bean; import javax .ejb. Remote; /** * * @author Quang */ @Remote public interface S05SingletonBeanRemote

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

TỪ KHÓA LIÊN QUAN

w