本文详细讲述了如何快速开发EJB和J2EE Web应用,并提供了一系列文件列表以及源文件。

开发J2EE Web运用的预备工作:

快速开发EJB和J2EE Web使用(ejb开发流程)  J2EE Web应用 快速开发 第1张

先装置JSDK,再装置JBoss.
装置JSDK,有必要取得一套对应于用户的操作体系的JDK,
我的装置的文件目录是
Windows2000: d:\s1studio_jdk\j2sdk1.4.1
Linux: /root/s1studio_jdk/j2sdk1.4.1
为了用EJB, 需求一个J2EE-1.3.jar或许j2ee-1.2.jar,
假如装置了Sun One Studio 或许 J2EE (www.sun.com )这个文件已经有.
把这个文件放在classpath途径上.
或许运用jboss-j2ee.jar, 装置JBoss后在$JBoss\client中可找到.
主张装置Sun One Studio, 用Sun One Studio编译Java源程序,
不必设置classpath, 省去不少进程.

装置JBoss:
把JBoss的压缩包解开,放在任一目录上,
我的装置的文件目录是
/dose/jboss-3.0.4_tomcat-4.1.12 (REDHAT8.0)
E:\jboss-3.0.4_tomcat-4.1.12 (WINDOWS2000)
WINDOWS2000, linux共用同一套JBoss.

装备JBoss:
发动JBoss需求履行一个脚本文件:
linux:run.sh
WINDOWS对应的是:run.bat

(1)
在JBoss\bin\run.bat (for Windows)最初刺进一行
set JAVA_HOME = d:\s1studio_jdk\j2sdk1.4.1
在JBoss\bin\run.sh (for Linux)最初刺进一行
JAVA_HOME="/root/s1studio_jdk/j2sdk1.4.1"

或许
(2)设置体系环境变量JAVA_HOME,指向JDK

运转JBoss, run.sh或许run.bat
当看到发动JBoss的信息时,阐明发动了.
服务器简略的测验:
JBoss默许的WEB端口为8080,咱们能够在翻开一个浏览器输入地址
http://localhost:8080/jmx-console
当在浏览器看到JBoss的信息时,阐明装置装备JBoss成功了.

树立下面的目录和文件(留意大小写).

FIRST.EAR
|
|-----META-INF (application.XML)
|
|-----First.jar
| |-----META-INF (ejb-jar.xml,jboss.xml)
| `-----Dev
| |-----First(FirstSession.java, FirstSessionHome.java, FirstSessionBean.java)
| |-----Delegate(NewDelegate.java)
| `-----Dao(MySQLDao.java)
|
`-----First.war(index.JSP)
|
`-----WEB-INF (jboss-web.xml, web.xml)
|-----classes
`-----lib

编译JAVA源程序,生成class文件.
进入JAVA源程序目录, 运转:
javac -classpath %classpath%;%jboss%\server\default\deploy\First.ear\First.jar *.java
或许
javac -classpath %jboss%\server\default\deploy\First.ear\First.jar;%jboss%\client\jboss-j2ee.jar *.java

Copy 目录First.ear to jboss\server\default\deploy.
翻开浏览器输入地址 http://localhost:8080/First

到此, 在浏览器看到: Hello! This My Session Bean From Jboss.
阐明这个EJB工作了.

假如按按钮, 没反应或犯错. 原因没装置装备数据库, 下面装置装备Postgres数据库

For Windows2000
下载 PgSQL731wina1.exe (http://www.postgresql.org ),
Finally you will see the next line, you need enter the password for Administrator
最终你将看下一个行,你有必要为用户Administrator输入password.
********************
Enter password of user `.\Administrator´:123456
********************

记下此password, 我的口令是123456.

从开始菜单 > Programm > PostgresSQL > Adjust PostgresSQL Configuration file
它将在Wordpad中翻开PostgresSQL Configuration文件, 找到下列行,

#
# Connection Parameters
#
#tcpip_Socket = false
#ssl = false

#max_connections = 32
#superuser_reserved_connections = 2

#port = 5432

修正修改:
#
# Connection Parameters
#
tcpip_socket = true
#ssl = false

#max_connections = 32
#superuser_reserved_connections = 2

port = 5432

接着,保存文件.

起动PostgresSQL服务器:
开始菜单>Programm>PostgresSQL>Utilies>Start PostgresSQL Server
起动指令行:
开始菜单>Programm>PostgresSQL>Utilies>Command Shell


履行下列指令,预备数据,
Administrator@SAN /
$ dir

$ cd bin

$ createdb test

$ psql test

test=# create table users
test-# (name varchar(20),
test(# id varchar(20),
test(# country varchar(20));
test=# insert into users values (´Sam´, ´123´, ´China´);
test=# insert into users values (´Tom´, ´321´, ´USA´);
test=# insert into users values (´Sean´, ´231´, ´France´);

test=# select * from users;
name | id | country
------+-----+---------
Sam | 123 | China
Tom | 321 | USA
Sean | 231 | France
(3 rows)

test=#

到此, 数据预备就绪.

For RedHat:
以root登陆, 履行下列指令,预备数据,

mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

Open and edit /usr/local/pgsql/data/pg_hba.conf

local all trust
host all 127.0.0.1 255.255.255.255 trust

just delete #, and save.

[root@localhost root]# su - postgres
-bash-2.05b$ /usr/bin/postmaster -i -D /usr/local/pgsql/data >logfile 2>&1 &
-bash-2.05b$ /usr/bin/createdb test
-bash-2.05b$ /usr/local/pgsql/bin/psql test
test=# .......the following same as Windows2000

到此, 数据预备就绪.

履行shutdown.bat or shutdown.sh, 中止Jboss Server.

找到JDBC drive.
为了在Jboss中运用连接池,需求仿制jdbc drive 到Jboss/server/default/deploy , 在linux 咱们能找到/usr/share/pgsql/pgjdbc2.jar,在wondows2000,咱们能找到PostgreSQL\usr\share\postgresql\java\postgresql.jar
把其中之一仿制到Jboss/server/default/deploy

装备Jboss

(1) 仿制 $Jboss/examples/jca/postgres-service.xml 到 $Jboss/server/default/deploy/

(2) 翻开修改Jboss/server/default/deploy/postgres-service.xml

PostgresDS
Properties">

jdbc:postgresql://localhost/test
org.postgresql.Driver

Administrator
123456

在我的比如中,
windows2000, 用户:Administrator,password:123456
Linux(RH8.0), 用户:Postgres, 没有password
由于PostgresSQL和windows2000运用不同的default用户名,所以在linux和window2000中这文件不同.当然,你能够加相同的PostgresSQL用户名和password在linux和window2000中, 这样这文件就相同了.

保存文件.

(3) 翻开修改 $Jboss/server/default/conf/standardjbosscmp-jdbc.xml
找到:
java:/DefaultDS
Hypersonic SQL
参加:
java:/PostgresDS
Postgres
保存文件.

(4) open and edit $Jboss/server/default/conf/standardjaws.xml
找到:
java:/DefaultDS
Hypersonic SQL
false
参加:
java:/PostgresDS
Postgres
false
保存文件.

现在重启JBoss.
翻开浏览器输入地址http://localhost:8080/First br> 输入ID,按按钮

#p#

附件:源文件代码

MysqlDao.java

  1. packageDev.Dao;
  2. importjava.sql.Connection;
  3. importjava.sql.SQLException;
  4. importjava.sql.Statement;
  5. importjava.sql.ResultSet;
  6. importjavax.naming.InitialContext;
  7. importjavax.sql.DataSource;
  8. publicclassMysqlDao{
  9. publicConnectiongetConnection()throwsException{
  10. InitialContextctx=newInitialContext();
  11. DataSourceds=(DataSource)ctx.lookup("java:/PostgresDS");
  12. Connectionconn=null;
  13. Statementstmt=null;
  14. try{
  15. conn=ds.getConnection();
  16. }catch(SQLExceptionsqlEx){
  17. System.out.println("Errorconnecttopool.");
  18. }
  19. returnconn;
  20. }
  21. publicStringgetName(Stringid)throwsException{
  22. Connectionconn=null;
  23. Statementstmt=null;
  24. ResultSetrs=null;
  25. Stringname="";
  26. try{
  27. conn=getConnection();
  28. if(conn!=null)System.out.println("Getconecttion."+conn.toString());
  29. stmt=conn.createStatement();
  30. if(stmt!=null)System.out.println("GetStatement."+stmt.toString());
  31. Stringsql="SELECT*fromuserswhereid='"+id+"'";
  32. System.out.println("SqlfromgetId():"+sql);
  33. rs=stmt.executeQuery(sql);
  34. if(rs!=null)System.out.println("Getresult.");
  35. if(rs.next()){
  36. name=rs.getString("name");
  37. }
  38. }catch(ExceptionsqlEx){
  39. System.out.println("ErrorfromgetName().");
  40. System.out.println("ErrorfromDAO.getName():"+sqlEx.getMessage());
  41. }finally{
  42. if(conn!=null){
  43. try{conn.close();}catch(ExceptionsqlEx){}
  44. }
  45. }
  46. returnname;
  47. }
  48. publicStringgetCountry(Stringid)throwsException{
  49. Connectionconn=null;
  50. Statementstmt=null;
  51. Stringname="";
  52. try{
  53. conn=getConnection();
  54. stmt=conn.createStatement();
  55. Stringsql="SELECT*fromuserswhereid='"+id+"'";
  56. System.out.println("SqlfromgetCountry():"+sql);
  57. java.sql.ResultSetrs=stmt.executeQuery(sql);
  58. if(rs.next())
  59. {
  60. name=rs.getString("Country");
  61. }
  62. }catch(SQLExceptionsqlEx){
  63. System.out.println("ErrorfromgetCountry().");
  64. }finally{
  65. if(conn!=null){
  66. try{conn.close();}catch(ExceptionsqlEx){}
  67. }
  68. }
  69. returnname;
  70. }
  71. }
  1. /*
  2. **
  3. **NewDelegate.java
  4. **
  5. */
  6. packageDev.Delegate;
  7. importjava.lang.*;
  8. importDev.First.*;
  9. publicclassNewDelegate{
  10. Dev.First.FirstSessionbean=null;
  11. publicNewDelegate(){
  12. try{
  13. javax.naming.InitialContextctx=newjavax.naming.InitialContext();
  14. Objectobjref=ctx.lookup("ejb/FirstSession");
  15. Dev.First.FirstSessionHometestBean=(Dev.First.FirstSessionHome)
  16. javax.rmi.PortableRemoteObject.narrow
  17. (objref,Dev.First.FirstSessionHome.class);
  18. bean=testBean.create();
  19. System.out.println("FromJSP");
  20. }catch(ExceptionNamingException){
  21. NamingException.printStackTrace();
  22. }
  23. }
  24. publicStringWelcome(){
  25. Stringmsg="";
  26. try{
  27. msg=bean.Welcome();
  28. }catch(ExceptionNamingException){
  29. NamingException.printStackTrace();
  30. }
  31. returnmsg;
  32. }
  33. publicStringgetName(Stringid){
  34. Stringname="";
  35. try{
  36. name=bean.getName(id);
  37. }catch(ExceptionNamingException){NamingException.printStackTrace();}
  38. returnname;
  39. }
  40. publicStringgetCountry(Stringid){
  41. Stringcountry="";
  42. try{
  43. country=bean.getCountry(id);
  44. }catch(ExceptionNamingException){NamingException.printStackTrace();}
  45. returncountry;
  46. }
  47. }
  1. /*
  2. **
  3. **FirstSession.java
  4. **
  5. */
  6. packageDev.First;
  7. importjava.lang.*;
  8. importjava.rmi.RemoteException;
  9. importjavax.ejb.CreateException;
  10. importjavax.ejb.EJBException;
  11. importjavax.ejb.SessionBean;
  12. importjavax.ejb.SessionContext;
  13. publicinterfaceFirstSessionextendsjavax.ejb.EJBObject{
  14. publicStringWelcome()
转载请说明出处
知优网 » 快速开发EJB和J2EE Web使用(ejb开发流程)

发表评论

您需要后才能发表评论