分类:
2008-04-30 23:13:41
Developing J2EE Web Application on Jboss and PostgreSQL(chinese version)
作者:Han QW, 转载请指明出处 如有不当之处,敬请指出
先安装JSDK,再安装JBoss.
安装JSDK,必须获得一套对应于用户的操作系统的JDK,
我的安装的文件目录是
WINDOWS2000: d:s1studio_jdkj2sdk1.4.1
linux: /root/s1studio_jdk/j2sdk1.4.1
为了用EJB, 需要一个j2ee-1.3.jar或者j2ee-1.2.jar,
如果安装了Sun One Studio 或者 J2EE ( )这个文件已经有.
把这个文件放在classpath路径上.
或者使用jboss-j2ee.jar, 安装JBoss后在$JBossclient中可找到.
建议安装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)
在JBossin un.bat (for Windows)开头插入一行
set JAVA_HOME = d:s1studio_jdkj2sdk1.4.1
在JBossin un.sh (for Linux)开头插入一行
JAVA_HOME="/root/s1studio_jdk/j2sdk1.4.1"
或者
(2)设置系统环境变量JAVA_HOME,指向JDK
运行JBoss, run.sh或者run.bat
当看到启动JBoss的信息时,说明启动了.
服务器简单的测试:
JBoss默认的WEB端口为8080,我们可以在打开一个浏览器输入地址
当在浏览器看到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.getParameter("id").equals("")){ String id = request.getParameter("id"); String name = ""; Dev.Dao.MysqlDao dao = new Dev.Dao.MysqlDao(); name = nn.getName(id); //access database through session bean //name = dao.getName(id); //access database directly if(name!= null && !name.equals("")){ msg1 ="Welcome " + name +" ! You are from "+ dao.getCountry(id)+ " ."; }else{ msg1 ="Please Check Your ID. : " + id; } } msg = nn.Welcome() ;%>