By GokiSoft.com|
09:35 09/01/2021|
Java Web + EJB (EAD)
[Share Code] Tìm hiểu về JavaBean + SessionBean - Lập trình EJB (EAD)
EJB
#Computer.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bean;
import java.io.Serializable;
/**
*
* @author teacher
*/
public class Computer implements Serializable{
String name, cpu, ram;
public Computer() {
}
public Computer(String name, String cpu, String ram) {
this.name = name;
this.cpu = cpu;
this.ram = ram;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}
@Override
public String toString() {
return "Computer{" + "name=" + name + ", cpu=" + cpu + ", ram=" + ram + '}';
}
}
#ComputerSessionBean.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package session;
import bean.Computer;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
/**
*
* @author teacher
*/
@Stateless
public class ComputerSessionBean implements ComputerSessionBeanLocal {
List<Computer> computerList = new ArrayList<>();
int index = 0;
@Override
public List<Computer> getComputerList() {
return computerList;
}
@Override
public void add(Computer computer) {
computerList.add(computer);
}
@Override
public Computer get(int index) {
return computerList.get(index);
}
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@Override
public void fakeData() {
index++;
computerList.add(new Computer("A1 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A2 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A3 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A4 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A5 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A6 >> " + index, "Core I7", "8 GB"));
computerList.add(new Computer("A7 >> " + index, "Core I7", "8 GB"));
}
}
#ComputerSessionBeanLocal.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package session;
import bean.Computer;
import java.util.List;
import javax.ejb.Local;
/**
*
* @author teacher
*/
@Local
public interface ComputerSessionBeanLocal {
List<Computer> getComputerList();
void add(Computer computer);
Computer get(int index);
void fakeData();
}
#CounterSessionBean.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package session;
import javax.ejb.Stateful;
/**
*
* @author teacher
*/
@Stateful
public class CounterSessionBean implements CounterSessionBeanLocal {
int count = 0;
@Override
public int getCount() {
return ++count;
}
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
}
#CounterSessionBeanLocal.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package session;
import javax.ejb.Local;
/**
*
* @author teacher
*/
@Local
public interface CounterSessionBeanLocal {
int getCount();
}
Java Web Application
#index.jsp
<%--
Document : index
Created on : Jan 9, 2021, 9:22:12 AM
Author : teacher
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Computer List</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Computer List</h2>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Ten</th>
<th>CPU</th>
<th>RAM</th>
</tr>
</thead>
<tbody>
<c:forEach items="${computerList}" var="item" varStatus="loop">
<tr>
<td>${loop.index + 1}</td>
<td>${item.name}</td>
<td>${item.cpu}</td>
<td>${item.ram}</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="fake.html"><button class="btn btn-danger">Fake Data</button></a>
</div>
</div>
</div>
</body>
</html>
#index.jsp
<%--
Document : index
Created on : Jan 9, 2021, 9:04:46 AM
Author : teacher
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Counter: ${count}!</h1>
</body>
</html>
#ComputerController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech.controller;
import bean.Computer;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import session.ComputerSessionBeanLocal;
/**
*
* @author teacher
*/
@Controller
@RequestMapping(value = "/computer")
public class ComputerController {
ComputerSessionBeanLocal computerSessionBean = lookupComputerSessionBeanLocal();
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap modelMap) {
List<Computer> computerList = computerSessionBean.getComputerList();
modelMap.put("computerList", computerList);
return "/computer/index";
}
@RequestMapping(value = "/fake", method = RequestMethod.GET)
public String fake(ModelMap modelMap) {
computerSessionBean.fakeData();
return "redirect:index.html";
}
private ComputerSessionBeanLocal lookupComputerSessionBeanLocal() {
try {
Context c = new InitialContext();
return (ComputerSessionBeanLocal) c.lookup("java:global/EJBExample01/EJBExample01-ejb/ComputerSessionBean!session.ComputerSessionBeanLocal");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
}
#CounterController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech.controller;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import session.CounterSessionBeanLocal;
/**
*
* @author teacher
*/
@Controller
@RequestMapping(value = "/counter")
public class CounterController {
CounterSessionBeanLocal counterSessionBean = lookupCounterSessionBeanLocal();
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap modelMap) {
int count = counterSessionBean.getCount();
modelMap.put("count", count);
return "/counter/index";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(ModelMap modelMap) {
int count = counterSessionBean.getCount();
modelMap.put("count", count);
return "/counter/index";
}
private CounterSessionBeanLocal lookupCounterSessionBeanLocal() {
try {
Context c = new InitialContext();
return (CounterSessionBeanLocal) c.lookup("java:global/EJBExample01/EJBExample01-ejb/CounterSessionBean!session.CounterSessionBeanLocal");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)