1. 使用NetBeans开发Session Bean

#1. 创建项目:File-->New Project-->Java EE-->EJB Module

#2. 在项目中创建Session Bean: 右击项目-->New-->Session Bean-->SessionType(Stateless/Stateful)-->Create Interface(Remote/Local)

2. 开发远程调用无状态Session Bean

#1. 开发EJB(Net Beans创建EJB Module, 项目名称:Hello)

Hello.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface Hello {
public String hello(String name);
}

HelloBean.java

package org.crazyit.service;

import javax.ejb.*;

@Stateless(mappedName = "Hello")
public class HelloBean implements Hello { public String hello(String name) {
return "Hello " + name + ", current time is " + new java.util.Date();
}
}

#2. 客户端调用EJB (Net Beans创建Java Project: EJBClient)

hello.java

package org.crazyit.service;

import javax.ejb.*;

@Remote
public interface Hello {
public String hello(String name);
}

EjbClient.java

package lee;

import javax.rmi.*;
import javax.naming.*;
import java.util.Properties; import org.crazyit.service.*; public class EjbClient { public void test() throws NamingException {
//获取WebLogic中JNDI服务的Context
Context ctx = getInitialContext();
Hello hello = (Hello) ctx.lookup("Hello#org.crazyit.service.Hello");
//调用WebLogic容器中EJB的业务方法
System.out.println(hello.hello("Ben"));
} //工具方法,用来获取WebLogic中JNDI服务的Context
private Context getInitialContext() {
// 参加(4)
} public static void main(String[] args) throws Exception {
EjbClient client = new EjbClient();
client.test();
}
}

3. 开发本地调用无状态Session Bean(Net Beans创建Enterprise Application, 项目名称:CatServiceEAR)

#1. CatServiceEAR-ejb

Cat.java

package org.crazyit.business;

public class Cat {

    private String name;
private int age; //无参数的构造器
public Cat() {
} //初始化全部属性的构造器
public Cat(String name, int age) {
this.name = name;
this.age = age;
} //name属性的setter和getter方法
public void setName(String name) {
this.name = name;
} public String getName() {
return this.name;
} //age属性的setter和getter方法
public void setAge(int age) {
this.age = age;
} public int getAge() {
return this.age;
}
}

Person.java

package org.crazyit.business;

public class Person {

    private Integer id;
private String name; //无参数的构造器
public Person() {
} //初始化全部属性的构造器 public Person(Integer id, String name) {
this.id = id;
this.name = name;
} //id属性的setter和getter方法
public void setId(Integer id) {
this.id = id;
} public Integer getId() {
return this.id;
} //name属性的setter和getter方法
public void setName(String name) {
this.name = name;
} public String getName() {
return this.name;
} public boolean equals(Object target) {
if (this == target) {
return true;
}
if (target.getClass() == Person.class) {
Person p = (Person) target;
if (p.getId() == this.getId()) {
return true;
}
}
return false;
} public int hashCode() {
return this.getId();
}
}

CatService.java

package org.crazyit.service;

import javax.ejb.*;
import org.crazyit.business.*; @Local
public interface CatService {
Cat[] getCats(Person owner);
}

CatServiceBean.java

package org.crazyit.service;

import java.util.*;
import javax.ejb.*; import org.crazyit.business.*; @Stateless(mappedName = "CatService")
public class CatServiceBean implements CatService { static Map<Person, Cat[]> catsInfo; static {
catsInfo = new HashMap<Person, Cat[]>();
catsInfo.put(new Person(1, "孙悟空"), new Cat[]{
new Cat("Kitty", 2),
new Cat("Garfield", 4),});
catsInfo.put(new Person(2, "猪八戒"), new Cat[]{
new Cat("Tom", 2),
new Cat("机器猫", 4),});
} public Cat[] getCats(Person owner) {
return catsInfo.get(owner);
}
}

#2. CatServiceEAR-war

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="org.crazyit.service.*, org.crazyit.business.*" %>
<%@page import="javax.naming.*" %> <%
InitialContext ctx = new InitialContext();
CatService catService = (CatService)ctx.lookup("CatService#org.crazyit.service.CatService"); Cat[] cats = catService.getCats(new Person(1, "Ben"));
for (int i = 0; i < cats.length; i++){
out.println("The age of " + cats[i].getName() + " is: " + cats[i].getAge() + "<br/>");
}
%>

4. Annotation与部署描述文件(Net Beans创建EJB Module, 项目名称:CatServiceXML)

CatService.jar

package org.crazyit.service;

import org.crazyit.business.*;

public interface CatService {
Cat[] getCats(Person owner);
}

CatServiceBean.java

package org.crazyit.service;

import java.util.*;

import org.crazyit.business.*;

public class CatServiceBean implements CatService {

    static Map<Person, Cat[]> catsInfo;

    static {
catsInfo = new HashMap<Person, Cat[]>();
catsInfo.put(new Person(1, "孙悟空"), new Cat[]{
new Cat("Kitty", 2),
new Cat("Garfield", 4),});
catsInfo.put(new Person(2, "猪八戒"), new Cat[]{
new Cat("Tom", 2),
new Cat("机器猫", 4),});
} public Cat[] getCats(Person owner) {
return catsInfo.get(owner);
}
}

Source Packages/META-INF/ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<enterprise-beans>
<session>
<ejb-name>catService</ejb-name>
<mapped-name>catService</mapped-name>
<business-local>org.crazyit.service.CatService</business-local>
<ejb-class>org.crazyit.service.CatServiceBean</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
</ejb-jar>

5. 开发远程调用有状态Session Bean

#1. 开发EJB(Net Beans创建EJB Module, 项目名称:ShopService)

ShopService.java

package org.crazyit.service;

import javax.ejb.*;
import java.util.*; @Remote
public interface ShopService {
void addItem(String item);
Map<String, Integer> showDetail();
}

ShopServiceBean.java

package org.crazyit.service;

import java.util.*;
import javax.ejb.*; @Stateful(mappedName = "ShopService")
public class ShopServiceBean implements ShopService { private Map<String, Integer> buyInfo = new HashMap<String, Integer>(); public void addItem(String item) {
//该物品已经购买过
if (buyInfo.containsKey(item)) {
buyInfo.put(item, buyInfo.get(item) + 1);
}
else {
buyInfo.put(item, 1);
}
} public Map<String, Integer> showDetail() {
return buyInfo;
}
}

#2. 开发Web应用(Net Beans创建Java Web Project 项目名称:ShopServiceTest)

shop.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>选择物品购买</title>
</head>
<body>
<form method="post" action="processBuy.jsp">
书籍:<input type="checkbox" name="item" value="book"><br/>
电脑:<input type="checkbox" name="item" value="computer"><br/>
汽车:<input type="checkbox" name="item" value="car"><br/>
<input type="submit" value="购买">
</form>
</body>
</html>
processBuy.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,org.crazyit.service.*,javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购买的物品列表</title>
</head>
<body>
<%
ShopService ss = (ShopService) session.getAttribute("ss");
if (ss == null) {
Context ctx = new InitialContext();
//通过JNDI查找EJB的引用
Object stub = ctx.lookup("ShopService#org.crazyit.service.ShopService");
ss = (ShopService) stub;
session.setAttribute("ss", ss);
} String[] buys = request.getParameterValues("item"); for (String item : buys) {
ss.addItem(item);
}
%>
您所购买的物品:<br/>
<%
Map<String, Integer> buyInfo = ss.showDetail(); for (String item : buyInfo.keySet()) {
out.println(item + "的数量为:" + buyInfo.get(item) + "<br />");
}
%>
<hr/>
<a href="shop.jsp">再次购买</a>
</body>
</html>

JavaEE(8) - 本地和远程调用的有状态以及无状态Session EJB的更多相关文章

  1. storm drpc分布式本地和远程调用模式讲解

    一.drpc 的介绍 1.rpc RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. 2.drpc drp ...

  2. java RMI 远程调用

    1.背景 在学习代理模式的过程中接触到了远程调用,jdk有自己的RMI实现,所以这边自己实现了RMI远程调用,并记录下心得. 感受最深的是RMI和现在的微服务有点相似,都是通过"注册中心&q ...

  3. openoffice excel word 转换pdf 支持本地调用和远程调用

    OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows.Linux.MacOS X (X11)和 Solaris 等操作系统上执行.它与各个主要的办公室软件套件兼容.OpenO ...

  4. 远程调用cmd更新本地jar

    最近遇到一个项目需求需要实现远程更新,但是本地项目无法更新自己,这让博主很是头疼,既然自己无法更新自己的话,那就自建新的项目,通过本地项目来调用新项目接口来更新本地项目. 代码如下: /** * 重启 ...

  5. JAVAEE——BOS物流项目08:配置代理对象远程调用crm服务、查看定区中包含的分区、查看定区关联的客户

    1 学习计划 1.定区关联客户 n 完善CRM服务中的客户查询方法 n 在BOS项目中配置代理对象远程调用crm服务 n 调整定区关联客户页面 n 实现定区关联客户 2.查看定区中包含的分区 n 页面 ...

  6. 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】

    一.远程调用概述 1.远程调用的定义 在一个程序中就像调用本地中的方法一样调用另外一个远程程序中的方法,但是整个过程对本地完全透明,这就是远程调用.spring已经能够非常成熟的完成该项功能了. 2. ...

  7. 架构师之路-在Dubbo中开发REST风格的远程调用

    架构师之路:从无到有搭建中小型互联网公司后台服务架构与运维架构 http://www.roncoo.com/course/view/ae1dbb70496349d3a8899b6c68f7d10b 概 ...

  8. 【Rest】在Dubbo中开发REST风格的远程调用(RESTful Remoting)

    目录 概述 REST的优点 应用场景 快速入门 标准Java REST API:JAX-RS简介 REST服务提供端详解 HTTP POST/GET的实现 Annotation放在接口类还是实现类 J ...

  9. Spring集成RMI实现远程调用

    前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...

随机推荐

  1. 使用dom4j创建和解析xml

    之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码 package xml; import java.io.File; import java ...

  2. Effective C++:规定20: 宁pass-by-reference-to-const更换pass-by-value

    (一) 假设传递参数当函数被调用pass-by-value,然后函数的参数是基于实际参数的副本最初值,调用,也得到该函数返回的结束值复印件. 请看下面的代码: class Person { publi ...

  3. Android 高仿微信即时聊天 百度云为基础的推

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天最终有幸利用百 ...

  4. PL SQLDEVELOPMENT导出数据库脚本

    Tools--export Tables--选择表--SQL Inserts-- watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHprMTU2MjExMD ...

  5. CSS: 解决Div float后,父Div无法高度自适应的问题

    在用CSS+DIV的布局中,常常会发现,当一个DIV float之后,假设他的高度超过了其父DIV的高度时,其父DIV的高度并不会对应的进行调整.要解决问题(也叫做闭合(清除)浮动),我们有四种办法: ...

  6. NET WEB

    .NET WEB程序员需要掌握的技能 2015-12-28 08:50 by 敏捷的水, 3997 阅读, 66 评论, 收藏, 编辑 本来这个是我给我们公司入职的新人做一个参考,由于 @张善友 老师 ...

  7. HDU 1069 Monkey and Banana(DP 长方体堆放问题)

    Monkey and Banana Problem Description A group of researchers are designing an experiment to test the ...

  8. HR筒子说:程序猿面试那点事

    小屁孩曾经有过4年的招聘经验,期间见识了各种类型的程序猿:有大牛.有菜牛:有功成名就,有苦苦挣扎不知方向.等后来做了一枚程序猿之后发现,HR眼中的程序猿和程序猿中的HR都是不一样的.有感与此,从HR的 ...

  9. NET中小型企业项目开发框架系列(一个)

    当时的前端,我们开发了基于Net一组结构sprint.NET+NHibernate+MVC+WCF+EasyUI等中小型企业级系统开发平台,如今把整个开发过程中的步步进展整理出来和大家分享,这个系列可 ...

  10. Web APi之认证

    Web APi之认证(Authentication)两种实现方式后续[三](十五)   前言 之前一直在找工作中,过程也是令人着实的心塞,最后还是稳定了下来,博客也停止更新快一个月了,学如逆水行舟,不 ...