Tomcat中使用jndi例子

來源:互聯網
上載者:User
說明:
       主要使用jndi對tomcat中的 env 以及 source 進行調用
問題:
        .注意 jdbc驅動需要放在 ${CATALINA_HOME}/common/lib 目錄下
         在tomcat的設定檔中需要注意 標籤的次序問題

#server.xml的配置如下

<Context path="/tomtest" reloadable="true" docBase="E:/admin/workspace/javaWS/tomtest" workDir="E:/admin/workspace/javaWS/tomtest/work/org/apache/jsp" >

    <!--Loader className="org.apache.catalina.loader.DevLoader" reloadable="true" debug="1"/-->

    <!--server level Environment-->

    <Environment name="greetings" type="java.lang.Integer" value="15"/>

    <Resource name="jdbc/ds" scope="Shareable" type="javax.sql.DataSource"/>

    <ResourceParams name="jdbc/ds">

       <!-- use dbcp pool-->

       <parameter>

        <name>factory</name>

        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

       </parameter>

       <!-- DBCP database connection settings -->

       <!-- MS SQL Server -->

       <parameter>

        <name>url</name>

        <value>jdbc:jtds:sqlserver://HOST:1433/Northwind</value>

       </parameter>

       <parameter>

        <name>driverClassName</name>

        <value>net.sourceforge.jtds.jdbc.Driver</value>

       </parameter>

       <parameter>

        <name>username</name>

        <value>sa</value>

       </parameter>

       <parameter>

        <name>password</name>

        <value>******</value>

       </parameter>      

       <!-- DBCP connection pooling options -->

       <parameter>

        <name>maxWait</name>

        <value>3000</value>

       </parameter>

       <parameter>

        <name>maxIdle</name>

        <value>100</value>

       </parameter>

       <parameter>

        <name>maxActive</name>

        <value>10</value>

       </parameter>

    </ResourceParams>

</Context>

#web.xml配置如下
<!--resource reference for datasource... it use "source" in server.xml-->
    <resource-env-ref>
      <description> data source entry test</description>
      <resource-env-ref-name>jdbc/ds</resource-env-ref-name>
      <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
    </resource-env-ref>
    <!-- Web level Environment-->
    <env-entry>
      <description>test entry</description>
      <env-entry-name>webEnv</env-entry-name>     
      <env-entry-value>i am environment in web.xml...</env-entry-value>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
#JSP測試使用如下
<%@page contentType="text/html;charset=gb2312"
    import="javax.naming.*,java.sql.*,javax.sql.*"
%>

<%
//建立一個context 作為尋找源 這裡為myenv ctx為初始源
Context ctx =new InitialContext();
//注意 tomcat中的對配置的尋找都在java:comp/env下面
Context myenv =(Context)ctx.lookup("java:comp/env");

//這裡得到一個 server級的環境變數
Integer greetings= (Integer)myenv.lookup("greetings");
out.println("serverEnv is: "+greetings+"<br/>");
//這裡的是web級的環境變數
out.println("WebEnv is:"+(String)myenv.lookup("webEnv")+"<br/>");

/*
    使用tomcat中配置的datasource 進行資料庫操作
*/
Connection conn=null;
DataSource ds=null;
Statement stat=null;
ResultSet res=null;

try{
    //Class.forName("net.sourceforge.jtds.jdbc.Driver");
   
    //得到tomcat配置的datasource引指
    ds=(DataSource)myenv.lookup("jdbc/ds");
   
    //使用datasource得到connection
    if(ds!=null){
        conn=ds.getConnection();
        out.println("had data source ...");
    }
   
    stat=conn.createStatement();           
    //執行語句並返回一個ResultSet
    res=stat.executeQuery("select distinct top 20 * from Orders");           
    //列印ResultSet
    out.print("<br/>");
    while(res.next())
    {
        out.print(res.getInt(1)+":"+res.getString(2)+"<br/>");
    }
   
}
catch(Exception e){
    out.println(e);
}
finally{
    if(res!=null)
        res.close();
    if(stat!=null)
        stat.close();
    if(conn!=null)
        conn.close();
   

}
//關閉context 據說context最好關了
ctx.close();
myenv.close();

%>

#顯示結果如下
serverEnv is: 15
WebEnv is:i am environment in web.xml...
conn
10248:VINET
10249:TOMSP
10250:HANAR
10251:VICTE
10252:SUPRD
10253:HANAR
10254:CHOPS
10255:RICSU
10256:WELLI
10257:HILAA
10258:ERNSH
10259:CENTC
10260:OTTIK
10261:QUEDE
10262:RATTC
10263:ERNSH
10264:FOLKO
10265:BLONP
10266:WARTH
10267:FRANK


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.