本文详细讲述了如何快速开发EJB和J2EE Web应用,并提供了一系列文件列表以及源文件。
开发J2EE Web运用的预备工作:
先装置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
- packageDev.Dao;
- importjava.sql.Connection;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- importjava.sql.ResultSet;
- importjavax.naming.InitialContext;
- importjavax.sql.DataSource;
- publicclassMysqlDao{
- publicConnectiongetConnection()throwsException{
- InitialContextctx=newInitialContext();
- DataSourceds=(DataSource)ctx.lookup("java:/PostgresDS");
- Connectionconn=null;
- Statementstmt=null;
- try{
- conn=ds.getConnection();
- }catch(SQLExceptionsqlEx){
- System.out.println("Errorconnecttopool.");
- }
- returnconn;
- }
- publicStringgetName(Stringid)throwsException{
- Connectionconn=null;
- Statementstmt=null;
- ResultSetrs=null;
- Stringname="";
- try{
- conn=getConnection();
- if(conn!=null)System.out.println("Getconecttion."+conn.toString());
- stmt=conn.createStatement();
- if(stmt!=null)System.out.println("GetStatement."+stmt.toString());
- Stringsql="SELECT*fromuserswhereid='"+id+"'";
- System.out.println("SqlfromgetId():"+sql);
- rs=stmt.executeQuery(sql);
- if(rs!=null)System.out.println("Getresult.");
- if(rs.next()){
- name=rs.getString("name");
- }
- }catch(ExceptionsqlEx){
- System.out.println("ErrorfromgetName().");
- System.out.println("ErrorfromDAO.getName():"+sqlEx.getMessage());
- }finally{
- if(conn!=null){
- try{conn.close();}catch(ExceptionsqlEx){}
- }
- }
- returnname;
- }
- publicStringgetCountry(Stringid)throwsException{
- Connectionconn=null;
- Statementstmt=null;
- Stringname="";
- try{
- conn=getConnection();
- stmt=conn.createStatement();
- Stringsql="SELECT*fromuserswhereid='"+id+"'";
- System.out.println("SqlfromgetCountry():"+sql);
- java.sql.ResultSetrs=stmt.executeQuery(sql);
- if(rs.next())
- {
- name=rs.getString("Country");
- }
- }catch(SQLExceptionsqlEx){
- System.out.println("ErrorfromgetCountry().");
- }finally{
- if(conn!=null){
- try{conn.close();}catch(ExceptionsqlEx){}
- }
- }
- returnname;
- }
- }
- /*
- **
- **NewDelegate.java
- **
- */
- packageDev.Delegate;
- importjava.lang.*;
- importDev.First.*;
- publicclassNewDelegate{
- Dev.First.FirstSessionbean=null;
- publicNewDelegate(){
- try{
- javax.naming.InitialContextctx=newjavax.naming.InitialContext();
- Objectobjref=ctx.lookup("ejb/FirstSession");
- Dev.First.FirstSessionHometestBean=(Dev.First.FirstSessionHome)
- javax.rmi.PortableRemoteObject.narrow
- (objref,Dev.First.FirstSessionHome.class);
- bean=testBean.create();
- System.out.println("FromJSP");
- }catch(ExceptionNamingException){
- NamingException.printStackTrace();
- }
- }
- publicStringWelcome(){
- Stringmsg="";
- try{
- msg=bean.Welcome();
- }catch(ExceptionNamingException){
- NamingException.printStackTrace();
- }
- returnmsg;
- }
- publicStringgetName(Stringid){
- Stringname="";
- try{
- name=bean.getName(id);
- }catch(ExceptionNamingException){NamingException.printStackTrace();}
- returnname;
- }
- publicStringgetCountry(Stringid){
- Stringcountry="";
- try{
- country=bean.getCountry(id);
- }catch(ExceptionNamingException){NamingException.printStackTrace();}
- returncountry;
- }
- }
- /*
- **
- **FirstSession.java
- **
- */
- packageDev.First;
- importjava.lang.*;
- importjava.rmi.RemoteException;
- importjavax.ejb.CreateException;
- importjavax.ejb.EJBException;
- importjavax.ejb.SessionBean;
- importjavax.ejb.SessionContext;
- publicinterfaceFirstSessionextendsjavax.ejb.EJBObject{
- publicStringWelcome()
知优网 » 快速开发EJB和J2EE Web使用(ejb开发流程)