服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

ejb技术之旅(二)


  设计实例的深入分析

  要求:

  本文假设读者对ejb技术有一定熟悉,对sql有一定了解就可以了。由于ejb涉及到的技术很多,尽管每次只讲述一种技术,但各种技术是相关的,所以希望读者谅解,因为这样不是很好组织。

  实例背景

  数据库: sql server 2000
  操作系统: windows 2000
  开发工具; jbuilder 7
  ejb服务器: weblogic 7.0

  机器ip地址,10.11.12.58

  实例综述:通过无状态session beans得到容器管理的entity beans中的数据,然后把无状态session beans得到的数据给jsp页面,从而客户可以看到所要的结果。通过这样一种过程使得您知道编写ejb组件是多么简单的事情。

  准备工作

  配置好sql server 2000的jdbc驱动(微软网站有下载,或者用bea提供的也可以!该例子中用的是微软的jdbc)、jbuilder 7 + weblogic 7集成环境,其他的也可以。

  开发过程

  1. 配置jdbc数据源:这个过程包括两个步骤,首先配置连接池(connection pools)。(为什么采用连接池:开发人员不想编写database方面的代码、更换数据库系统变得简单、限制数据库的连接数量、不需要为每个客户建立新连接。这种池的概念在j2ee中有很多地方出现了,比如ejb本身)配置的参数如下:

name: cacd
url: jdbc:microsoft:sqlserver://10.11.12.58:1433;user=sa;password=cacd;
driver classname: com.microsoft.jdbc.sqlserver.sqlserverdriver
initial capacity: 3
maximum capacity: 10

  其次,配置jdbc数据源,在tx data sources中配置的参数如下:

name: cacd
jndi name: cacd
pool name: cacd(要与前面的相匹配!)
(tx data sources与一般的data sources 区别何在?读给读者思考、讨论。)

  其中,在url中,你需要为sql server 2000配置一个用户名sa,密码为cacd,因为这里用的是type 4的jdbc,所以需要将sql server 2000的用户认证修改为:nt+sql server 2000混合认证。
  2. 数据库的建立:entity beans代表了database中的数据,所以需要数据库支持,但一般情况下,我们知道,可以根据容器管理的entity beans导出sql ddl。另一方面,可以根据sql ddl生成容器管理的entity beans。两种方法都可以。比如jbuilder 7两种方法都提供了。但我想,信息模型(数据库)的建立在编码之前就应该存在,所以建议采用第二种办法。当然有些时候第一种会较为合理些,因为并不是表中所有的字段都会映射到entity beans中。该例子中建立了这样这样一个table:

/*===================================================*/
/* table : techniquespec */
/*===================================================*/

create table techniquespec (
techniqueitem char(100) not null,
units char(10) null,
minvalue decimal(16,6) null,
maxvalue decimal(16,6) null,
types char(1) null,
signon char(1) null,
constraint pk_techniquespec primary key (techniqueitem)
)

  其中,开发人员在建表的过程中,不需要手工去写sql ddl语句,一般都可以借助于工具进行,比如powerdesigner、erwin等工具。用用就会了,不要对工具产生不好的情绪,但前提是你熟悉数据库理论。

  用户建好table后,可以填入数据,中文的也可以。

  3. 容器管理的entity beans的开发:jbuilder对ejb开发支持的比较好,提供了图形化的方式。由于我们已经建立好了database,前面的techniquespec表,我们可以借助于import schema from database,将sql dll引入进来。(由于这里不能贴出操作图片,所以只能用文字说明梗概,大家在设计过程中有什么问题,可以发邮件给我,我尽力而为。)在这个过程中,一定要注意jndi的名字和数据源中的jndi要一致。得到sql dll后,我们可以根据techniquespec表生成cmp 2.0 entity beans,在这里我们采用localhome访问entity beans,为什么采用?后续文章中都会详细阐述。现在想返回表techniquespec中列techniqueitem的所有内容。首先,真假一个findbytypes finder方法,ejb ql语句为: select object(p) from techniquespec as p,其中返回值为collection。下面给出代码。

  localhome接口:

package cacdsystem;
import javax.ejb.*;
import java.util.*;
public interface techniquespechome extends javax.ejb.ejblocalhome
{
 public techniquespec create(string techniqueitem)
  throws createexception;
 public collection findbytypes()
  throws finderexception;
 //添加的finder方法
 public techniquespec findbyprimarykey(string techniqueitem)
  throws finderexception;
}

  local接口:

package cacdsystem;
import javax.ejb.*;
import java.util.*;
import java.math.*;
public interface techniquespec extends javax.ejb.ejblocalobject
{
 public string gettechniqueitem();
 public void setunits(string units);
 public string getunits();
 public void setminvalue(bigdecimal minvalue);
 public bigdecimal getminvalue();
 public void setmaxvalue(bigdecimal maxvalue);
 public bigdecimal getmaxvalue();
 public void settypes(string types);
 public string gettypes();
 public void setsignon(string signon);
 public string getsignon();}

 bean类:

package cacdsystem;
import javax.ejb.*;
abstract public class techniquespecbean implements entitybean
{
 entitycontext entitycontext;
 public java.lang.string ejbcreate(java.lang.string techniqueitem)
  throws createexception
 {
  settechniqueitem(techniqueitem);
  return null;
 }
 
 public void ejbpostcreate(java.lang.string techniqueitem)
  throws createexception {}
 public void ejbremove() throws removeexception {}
 public abstract void settechniqueitem(java.lang.string techniqueitem);
 public abstract void setunits(java.lang.string units);
 public abstract void setminvalue(java.math.bigdecimal minvalue);
 public abstract void setmaxvalue(java.math.bigdecimal maxvalue);
 public abstract void settypes(java.lang.string types);
 public abstract void setsignon(java.lang.string signon);
 public abstract java.lang.string gettechniqueitem();
 public abstract java.lang.string getunits();
 public abstract java.math.bigdecimal getminvalue();
 public abstract java.math.bigdecimal getmaxvalue();
 public abstract java.lang.string gettypes();
 public abstract java.lang.string getsignon();
 public void ejbload(){}
 public void ejbstore(){}
 public void ejbactivate(){}
 public void ejbpassivate(){}
 public void unsetentitycontext()
 {
  this.entitycontext = null;
 }
 public void setentitycontext(entitycontext entitycontext)
 {
  this.entitycontext = entitycontext;
 }
}

  4.无状态session beans的开发: 通过ejb 图形设计器,生成一个session bean,代码如下。

  home接口:

package cacdsystem;
 import javax.ejb.*;
 import java.util.*;
 import java.rmi.*;
 public interface vochome extends javax.ejb.ejbhome
 {
  public voc create() throws createexception, remoteexception;
 }

  remote接口:

package cacdsystem;
 import javax.ejb.*;
 import java.util.*;
 import java.rmi.*;
 public interface voc extends javax.ejb.ejbobject
  {
   public collection getvoctechnique() throws remoteexception;
  }

  bean类:

package cacdsystem;
 import javax.ejb.*;
 import java.util.*;
 public class vocbean implements sessionbean
 {
  sessioncontext sessioncontext;
  techniquespechome techniquespechome;
  public void ejbcreate() throws createexception {}
  public void ejbremove() {}
  public void ejbactivate() {}
  public void ejbpassivate() {}
  public void setsessioncontext(sessioncontext sessioncontext)
  {
   this.sessioncontext = sessioncontext;
   try
   {
    javax.naming.context context = new javax.naming.initialcontext();
    techniquespechome = (techniquespechome)context.lookup("techniquespec");
   }
   catch (exception ex)
   {
    system.out.println("techniquespecs调用出错!");
    throw new ejbexception(ex);
    }
  }

  public java.util.collection getvoctechnique()
  {
   /**@todo complete this method*
   //*** 获得全部技术需求项函数!*/

   system.out.println("调用getvoctechnique()!");
   java.util.collection collection = null;
   java.util.list results = new arraylist();
   try
   {
    collection= techniquespechome.findbytypes();
    if(collection.size() > 0)
    {
     system.out.println(collection.size());
     iterator iter = collection.iterator();
     string tempstr = null;
     techniquespec techniqu = null;
     while(iter.hasnext())
     {
      techniqu = (techniquespec) iter.next();
      tempstr = techniqu.gettechniqueitem();
      tempstr = charasc.chintoiso(tempstr);
      results.add(tempstr);
     }
    }
   }
   catch (exception ex)
   {
    ex.printstacktrace();
    throw new ejbexception(ex);
   }
   return results;
  }
 }

  说明:
  
  (1)我们在session bean中添加了一个商务方法,getvoctechnique(),以获得techniqueitem中的全部内容。

  (2)setsessioncontext中,我们完成了ejb的一些初始化工作。(3) charasc.chintoiso是用于中文转换的静态方法,代码如下:

package cacdsystem;
 public class charasc
 {
  static private string results = null;
  public charasc(){}
  static public string chintoiso(string temp)
  {
   if(temp == null)
   {
    results = "";
   }
   else
   {
    try
    {
     results = new string(temp.getbytes("iso-8859-1"),"gb2312");
     results = results.trim();
     }
    catch(exception ee)
    {
     system.out.println("中文转换失败!");
    }
   }
   return results;
  }
  }

  5. jsp页面的开发:针对session bean,我编写了一个jsp页面。

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.text.*" %>
<%@page import="java.util.*"%>
<%@page import="java.sql.*" %>
<%@page import="javax.naming.*" %>
<%@page import="java.lang.*" %>
<%@page import="javax.rmi.portableremoteobject" %>
<%@page import="javax.ejb.* "%>
<%@page import="cacdsystem.*"%>

<html>
<head>
<title>产品技术需求提取</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<form id=form name=form1 method=post>
<input type=hidden id=pageflag name=pageflag value='input'>
<table cellspacing=0 cellpadding=0 width="98%" border=0 align=center >
<tr height=30 class=titlefont>
<td><font color='#0058a5'> 您所在位置:产品技术需求提取</font></td>
</tr>
<tr height=1 class=titlefont>
<td></td>
</tr>
</table>

<table width="98%" border="0" bgcolor='#999999' align=center>
<tr>
<td><b> <font color="#ffffff">产品技术需求提取</font></b></td>
</tr>
</table>

<table width="98%" border="1" align=center bgcolor="#f0f0f0" bordercolor='#ffffff' cellspacing=0 cellpadding=0 class=titlefont>
<tr bgcolor='#e0e8f8'>
<td> </td>
</tr>

<%
initialcontext ctx = new initialcontext();
vochome vochome =
(vochome)portableremoteobject.narrow(ctx.lookup("voc"),vochome.class);
voc voc = vochome.create();
collection collection = voc.getvoctechnique();
if(collection.size() > 0)
{
iterator iters = collection.iterator();
int i = 0;
while(iters.hasnext())
{
++i;
%>

<tr>
<td><input type='checkbox' name=<%= "select"+i %>
value=<%= i %>><%= (string)iters.next() %>
</td>
</tr>

<%
}
}
%>

<%
voc.remove();
%>

<tr bgcolor='#e0e8f8' align=middle>
<td>
<p align=left></p>
</td>
</tr>

<tr align=middle>
<td>
<input type=submit id=submit1 name=submit1 value="提 交">
<input type=reset id=submit1 name=submit value="重 置">
</td>
</tr>
</table>
</form>
</body>
</html>

  6. 系统deploy: 到现在为止,我们实现了entity bean、session bean、jsp,从而实现了该实例所需要的代码。为使我们测试代码的正确性,我们来deploy到服务器上。首先,由于在session bean中引用了entity bean,所以需要申明。

<session>
<display-name>voc</display-name>
<ejb-name>voc</ejb-name>
<home>cacdsystem.vochome</home>
<remote>cacdsystem.voc</remote>
<ejb-class>cacdsystem.vocbean</ejb-class>
<session-type>stateless</session-type>
<transaction-type>container</transaction-type>
<ejb-local-ref> //手工添加的配置描述符代码!
<description />
<ejb-ref-name>techniquespec</ejb-ref-name>
<ejb-ref-type>entity</ejb-ref-type>
<local-home>cacdsystem.techniquespechome</local-home>
<local>cacdsystem.techniquespec</local>
<ejb-link>techniquespec</ejb-link>
</ejb-local-ref>
</session>

  其次,需要将ejb的接口文件拷贝到web-inf/classes目录下。在这里由于是存在package cacdsystem中,需要把4个接口文件拷贝到web-inf/classes/cacdsystem中。第三,将ejb打包成.jar文件,jbuilder 7可以自动完成,记得要包括charasc.class文件。生成.jar后发布到服务器上可以通过jbuilder 7发布,也可以通过weblogic提供的builder工具,或者通过console发布,或者通过将.jar拷贝到applications目录即达到发布的目的。

  最后,我们也可以通过把整个ejb、jsp打包成.ear文件,jbuilder工具提供了wizard。

  总结

  整个过程实现了ejb组件开发,从而对ejb有了一定的认识。接下来的文章中,我会继续深入讲述ejb技术涉及到的技术。谢谢大家看完我的写文章,也希望大家写信和我交流。

扫描关注微信公众号