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;
    }
}
View Code

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();
    }
}
View Code

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>

 

posted on 2015-01-26 08:41  逝者如斯(乎)  阅读(445)  评论(0)    收藏  举报