使用jboss和postgresql-快速开发ejb和j2ee web application
先安装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
/*
**
**mysqldao.java
**
*/
package dev.dao;import java.sql.connection;import java.sql.sqlexception;import java.sql.statement;import java.sql.resultset;import javax.naming.initialcontext;import javax.sql.datasource;public class mysqldao { public connection getconnection() throws exception { initialcontext ctx = new initialcontext(); datasource ds = (datasource) ctx.lookup("java:/postgresds"); connection conn = null; statement stmt = null; try { conn = ds.getconnection(); } catch (sqlexception sqlex) { system.out.println("error connect to pool."); } return conn; } public string getname(string id) throws exception { connection conn = null; statement stmt = null; resultset rs = null; string name = ""; try { conn = getconnection(); if ( conn !=null )system.out.println("get conecttion. "+ conn.tostring()); stmt = conn.createstatement(); if ( stmt !=null )system.out.println("get statement. "+ stmt.tostring()); string sql = "select * from users where id = ´"+id+"´"; system.out.println("sql from getid(): "+sql); rs = stmt.executequery(sql); if ( rs !=null )system.out.println("get result. "); if (rs.next()){ name = rs.getstring("name"); } } catch (exception sqlex) { system.out.println("error from getname()."); system.out.println("error from dao.getname() :" + sqlex.getmessage()); }finally { if (conn != null) { try { conn.close(); } catch (exception sqlex) { } } } return name; } public string getcountry(string id) throws exception { connection conn = null; statement stmt = null; string name = ""; try { conn = getconnection(); stmt = conn.createstatement(); string sql = "select * from users where id = ´"+id+"´"; system.out.println("sql from getcountry(): "+sql); java.sql.resultset rs = stmt.executequery(sql); if (rs.next()) { name = rs.getstring("country"); } } catch (sqlexception sqlex) { system.out.println("error from getcountry()."); }finally { if (conn != null) { try { conn.close(); } catch (exception sqlex) { } } } return name; }}
/*
**
**newdelegate.java
**
*/
package dev.delegate;import java.lang.*;import dev.first.*;public class newdelegate { dev.first.firstsession bean = null; public newdelegate( ){ try { javax.naming.initialcontext ctx = new javax.naming.initialcontext(); object objref = ctx.lookup("ejb/firstsession"); dev.first.firstsessionhome testbean = (dev.first.firstsessionhome) javax.rmi.portableremoteobject.narrow (objref,dev.first.firstsessionhome.class); bean = testbean.create(); system.out.println("from jsp"); } catch (exception namingexception) { namingexception.printstacktrace(); } } public string welcome() { string msg = ""; try { msg = bean.welcome(); } catch (exception namingexception) { namingexception.printstacktrace(); } return msg; } public string getname(string id) { string name = ""; try { name = bean.getname(id); } catch (exception namingexception) { namingexception.printstacktrace();} return name; } public string getcountry(string id) { string country = ""; try { country = bean.getcountry(id); } catch (exception namingexception) { namingexception.printstacktrace();} return country; } }
/*
**
**firstsession.java
**
*/
package dev.first;import java.lang.*;import java.rmi.remoteexception;import javax.ejb.createexception;import javax.ejb.ejbexception;import javax.ejb.sessionbean;import javax.ejb.sessioncontext;public interface firstsession extends javax.ejb.ejbobject{ public string welcome() throws java.rmi.remoteexception; public string getname(string id) throws java.rmi.remoteexception; public string getcountry(string id) throws java.rmi.remoteexception;}
/*
**
**firstsessionhome.java
**
*/
package dev.first;import java.lang.*;import java.rmi.remoteexception;import javax.ejb.createexception;import javax.ejb.ejbexception;import javax.ejb.sessionbean;import javax.ejb.sessioncontext;public interface firstsessionhome extends javax.ejb.ejbhome{public firstsession create() throws javax.ejb.createexception, java.rmi.remoteexception;}
/*
**
**firstsessionbean.java
**
*/
package dev.first;import java.rmi.remoteexception;import javax.ejb.createexception;import javax.ejb.ejbexception;import javax.ejb.sessionbean;import javax.ejb.sessioncontext;public class firstsessionbean implements sessionbean{public void ejbcreate() throws createexception {}public string welcome(){ string msg="hello! this my session bean from jboss."; system.out.println(msg); return msg;}public string getname(string id){ string name = ""; system.out.println("from bean before getname :"+ name); try{ dev.dao.mysqldao dao = new dev.dao.mysqldao(); name = dao.getname(id); system.out.println("from bean after getname :"+ name); }catch(exception e){ system.out.println(e.getmessage());} return name;}public string getcountry(string id){ string country = ""; try{ dev.dao.mysqldao dao = new dev.dao.mysqldao(); country = dao.getcountry(id); }catch(exception e){ } return country;}public void setsessioncontext( sessioncontext acontext ) throws ejbexception {}public void ejbactivate() throws ejbexception {}public void ejbpassivate() throws ejbexception {}public void ejbremove() throws ejbexception {}}
/*don´t put the following lines into index.jsp
**
**index.jsp
**
*/don´t put the above lines into index.jsp
<%@page language="java" %><% string msg = ""; string msg1 = ""; dev.delegate.newdelegate nn = new dev.delegate.newdelegate(); if (request.getparameter("id") != null && request.getparameter("id") != ""&& !request
闽公网安备 35060202000074号