<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java學(xué)習(xí)

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經(jīng)搬家了,新的地址是 http://www.javaly.cn 如果有對(duì)文章有任何疑問(wèn)或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂(lè)園)指出,我會(huì)盡力幫助解決。一起進(jìn)步

     

    使用JBoss和PostgreSQL-----快速開(kāi)發(fā)EJB和J2EE Web Application

    使用JBoss和PostgreSQL-----快速開(kāi)發(fā)EJB和J2EE Web Application


    Developing J2EE Web Application on Jboss and PostgreSQL(chinese version)


    作者:Han QW, 轉(zhuǎn)載請(qǐng)指明出處 如有不當(dāng)之處,敬請(qǐng)指出


    先安裝JSDK,再安裝JBoss.
    安裝JSDK,必須獲得一套對(duì)應(yīng)于用戶的操作系統(tǒng)的JDK,
    我的安裝的文件目錄是
    WINDOWS2000: d:s1studio_jdkj2sdk1.4.1
    linux: /root/s1studio_jdk/j2sdk1.4.1
    為了用EJB, 需要一個(gè)j2ee-1.3.jar或者j2ee-1.2.jar,
    如果安裝了Sun One Studio 或者 J2EE (www.sun.com )這個(gè)文件已經(jīng)有.
    把這個(gè)文件放在classpath路徑上.
    或者使用jboss-j2ee.jar, 安裝JBoss后在$JBossclient中可找到.
    建議安裝Sun One Studio, 用Sun One Studio編譯JAVA源程序,
    不用設(shè)置classpath, 省去不少過(guò)程.

    安裝JBoss:
    把JBoss的壓縮包解開(kāi),放在任一目錄上,
    我的安裝的文件目錄是
    /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:
    啟動(dòng)JBoss需要執(zhí)行一個(gè)腳本文件:
    linux:run.sh
    WINDOWS對(duì)應(yīng)的是:run.bat

    (1)
    在JBossin un.bat (for Windows)開(kāi)頭插入一行
    set JAVA_HOME = d:s1studio_jdkj2sdk1.4.1
    在JBossin un.sh (for Linux)開(kāi)頭插入一行
    JAVA_HOME="/root/s1studio_jdk/j2sdk1.4.1"

    或者
    (2)設(shè)置系統(tǒng)環(huán)境變量JAVA_HOME,指向JDK

    運(yùn)行JBoss, run.sh或者run.bat
    當(dāng)看到啟動(dòng)JBoss的信息時(shí),說(shuō)明啟動(dòng)了.
    服務(wù)器簡(jiǎn)單的測(cè)試:
    JBoss默認(rèn)的WEB端口為8080,我們可以在打開(kāi)一個(gè)瀏覽器輸入地址
    http://localhost:8080/jmx-console
    當(dāng)在瀏覽器看到JBoss的信息時(shí),說(shuō)明安裝配置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() ;%><html><head><title>Welcome</title></head><body bgcolor="#FFFFCC"><br> <%= msg %> <br><FORM ACTION="index.jsp" method="post"><P>Your ID:<INPUT TYPE="TEXT" NAME="id" size = "10"></P><P><INPUT TYPE="SUBMIT" NAME="SUBMIT"></P></FORM><br><br><%=(msg1 == "")? "":msg1 + "<br> <br> <br>Connect to Database OK." %></body></html>



    <!--不要將此以下5行存入文件
    **
    **ejb-jar.xml
    **
    -->不要將此以上5行存入文件, 下同.

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"><ejb-jar> <description>First</description> <display-name>First</display-name><enterprise-beans><!-- Session Beans --><session id="MyFirstSession"> <display-name>My First Session Bean</display-name> <ejb-name>FirstSession</ejb-name> <home>Dev.First.FirstSessionHome</home> <remote>Dev.First.FirstSession</remote> <ejb-class>Dev.First.FirstSessionBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type></session></enterprise-beans><assembly-descriptor></assembly-descriptor></ejb-jar>




    <!--
    **
    **jboss.xml
    **
    -->

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN" "http://www.jboss.org/j2ee/dtd/jboss.dtd"><jboss><enterprise-beans><session><ejb-name>FirstSession</ejb-name><jndi-name>ejb/FirstSession</jndi-name></session></enterprise-beans><resource-managers></resource-managers></jboss>



    <!--
    **
    **jboss-web.xml
    **
    -->

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web.dtd"><jboss-web> <resource-ref> <res-ref-name>jdbc/PostgresDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <jndi-name>java:/PostgresDS</jndi-name> </resource-ref></jboss-web>



    <!--
    **
    **web.xml
    **
    -->

    <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN""http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app><resource-ref> <description>Postgresql driver</description> <res-ref-name>jdbc/PostgresDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth></resource-ref></web-app>




    <!--
    **
    **application.xml
    **
    -->

    <?xml version="1.0" encoding="ISO-8859-1"?><application><display-name>First</display-name><module><web><web-uri>First.war</web-uri><context-root>/First</context-root></web></module><module><ejb>First.jar</ejb></module></application>



    編譯JAVA源程序,生成class文件.
    進(jìn)入JAVA源程序目錄, 運(yùn)行:
    javac -classpath %classpath%;%jboss%serverdefaultdeployFirst.earFirst.jar *.java
    或者
    javac -classpath %jboss%serverdefaultdeployFirst.earFirst.jar;%jboss%clientjboss-j2ee.jar *.java

    Copy 目錄First.ear to jbossserverdefaultdeploy.
    打開(kāi)瀏覽器輸入地址 http://localhost:8080/First

    到此, 在瀏覽器看到: Hello! This My Session Bean From Jboss.
    說(shuō)明這個(gè)EJB工作了.

    如果按按鈕, 沒(méi)反應(yīng)或出錯(cuò). 原因沒(méi)安裝配置數(shù)據(jù)庫(kù), 下面安裝配置Postgres數(shù)據(jù)庫(kù)


    For Windows2000
    下載 PgSQL731wina1.exe (http://www.postgresql.org),
    Finally you will see the next line, you need enter the password for Administrator
    最后你將看下一個(gè)行,你必須為用戶Administrator輸入password.
    ********************
    Enter password of user `.Administrator´:123456
    ********************

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

    從開(kāi)始菜單 > Programm > PostgresSQL > Adjust PostgresSQL Configuration file
    它將在Wordpad中打開(kāi)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

    接著,保存文件.

    起動(dòng)PostgresSQL服務(wù)器:
    開(kāi)始菜單>Programm>PostgresSQL>Utilies>Start PostgresSQL server
    起動(dòng)命令行:
    開(kāi)始菜單>Programm>PostgresSQL>Utilies>Command Shell


    執(zhí)行下列命令,準(zhǔn)備數(shù)據(jù),
    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=#

    到此, 數(shù)據(jù)準(zhǔn)備就緒.



    For RedHat:
    以root登陸, 執(zhí)行下列命令,準(zhǔn)備數(shù)據(jù),

    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

    到此, 數(shù)據(jù)準(zhǔn)備就緒.


    執(zhí)行shutdown.bat or shutdown.sh, 停止Jboss Server.

    找到JDBC drive.
    為 了在Jboss中使用連接池,需要拷貝jdbc drive 到Jboss/server/default/deploy , 在linux 我們能找到/usr/share/pgsql/pgjdbc2.jar,在wondows2000,我們能找到 PostgreSQLusrsharepostgresqljavapostgresql.jar
    把其中之一復(fù)制到Jboss/server/default/deploy


    配置Jboss

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

    (2) 打開(kāi)編輯Jboss/server/default/deploy/postgres-service.xml

    <attribute name="JndiName">PostgresDS</attribute>
    <attribute name="ManagedConnectionFactoryProperties">
    <properties>
    <config-property name="ConnectionURL" type="java.lang.String">jdbc:postgresql://localhost/test</config-property>
    <config-property name="DriverClass" type="java.lang.String">org.postgresql.Driver</config-property>
    <!--set these only if you want only default logins, not through JAAS -->
    <config-property name="UserName" type="java.lang.String">Administrator</config-property>
    <config-property name="Password" type="java.lang.String">123456</config-property>
    </properties>

    In my example, set Username Administrator, password 123456 for windows 2000
    set Username Postgres, no password for Linux.
    在我的例子中,
    windows2000, 用戶:Administrator,password:123456
    Linux(RH8.0), 用戶:Postgres, 沒(méi)有password
    因 為PostgresSQL和windows2000使用不同的default用戶名,所以在linux和window2000中這文件不同.當(dāng)然,你可以 加相同的PostgresSQL用戶名和password在linux和window2000中, 這樣這文件就相同了.

    保存文件.

    (3) 打開(kāi)編輯 $Jboss/server/default/conf/standardjbosscmp-jdbc.xml
    找到:
    <datasource>java:/DefaultDS</datasource>
    <datasource-mapping>Hypersonic SQL</datasource-mapping>
    加入:
    <datasource>java:/PostgresDS</datasource>
    <datasource-mapping>Postgres</datasource-mapping>
    保存文件.

    (4) open and edit $Jboss/server/default/conf/standardjaws.xml
    找到:
    <datasource>java:/DefaultDS</datasource>
    <type-mapping>Hypersonic SQL</type-mapping>
    <debug>false</debug>
    加入:
    <datasource>java:/PostgresDS</datasource>
    <type-mapping>Postgres</type-mapping>
    <debug>false</debug>
    保存文件.

    現(xiàn)在重起JBoss.
    打開(kāi)瀏覽器輸入地址 http://localhost:8080/First
    輸入ID,按按鈕.

    posted on 2008-10-06 15:54 找個(gè)美女做老婆 閱讀(521) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計(jì)

    公告

    本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲Av无码乱码在线播放| 亚洲专区在线视频| 国产成人免费视频| 亚洲国产成人精品无码区在线网站 | 国产日韩成人亚洲丁香婷婷| 久久精品乱子伦免费| 亚洲精品宾馆在线精品酒店| 亚洲伊人久久综合中文成人网| 84pao强力永久免费高清| 亚洲中文无码亚洲人成影院| 久久久久亚洲av成人无码电影| 57pao国产成永久免费视频| 亚洲爆乳无码专区www| 国产成人亚洲精品青草天美| 免费看国产精品3a黄的视频| 成人免费av一区二区三区| 性xxxx黑人与亚洲| 亚洲乱码日产一区三区| 午夜精品在线免费观看| 最近免费mv在线观看动漫| 国产区图片区小说区亚洲区| 亚洲日韩在线视频| 亚洲人成亚洲人成在线观看| 美女被免费视频网站a国产| 久久不见久久见免费视频7| 人人爽人人爽人人片av免费| 亚洲一区二区三区免费观看| 亚洲中久无码永久在线观看同| 一区二区无码免费视频网站| 高清永久免费观看| 在线观看亚洲专区| 亚洲人和日本人jizz| 日本亚洲视频在线| 免费人成视频x8x8入口| 免费A级毛片无码免费视| 未满十八18禁止免费无码网站| 人人爽人人爽人人片av免费| 亚洲av综合av一区二区三区| 亚洲欧洲国产精品久久| 亚洲精品成人无限看| 夜色阁亚洲一区二区三区|