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

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

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

    我的空間,寫(xiě)我所寫(xiě),禪我所藏

    與我一起遨游吧

     

    在Tomcat 5.5.20中配置JNDI的體會(huì)

    The Context element represents a web application, which is run within a particular virtual host. Each web application is based on a Web Application Archive (WAR) file, or a corresponding directory containing the corresponding unpacked contents, as described in the Servlet Specification (version 2.2 or later).??

    從上述描述看出,Context元素代表的是一個(gè)web application。

    You may define as many Context elements as you wish. Each such Context MUST have a unique context path, which is defined by the path attribute. In addition, you MUST define a Context with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.

    從上面的描述可以看出Context的數(shù)量自己定,但是必須定義一個(gè)path屬性為空的Context,用來(lái)出來(lái)匹配不上其它path的用戶請(qǐng)求。

    In addition to nesting Context elements inside a Host element, you can also store them:

    • in the individual $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
    • in the individual $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
    • in individual files (with a ".xml" extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory
    • if the previous file was not found for this application, in individual file at /META-INF/context.xml inside the application files

    以上的文字,是我寫(xiě)這篇文檔的根源。

    Context element的放置位置:有5個(gè)。

    1、conf/server.xml的host元素之下。

    2、conf/context.xml 中,被所有的應(yīng)用共享。

    3、conf/[enginename]/[hostname]/context.xml中,被所有的應(yīng)用共享。

    4、conf/[enginename]/[hostname]/ directory.xml中,就是以目錄名作為文件名的配置文件。

    5、/META-INF/context.xml 在應(yīng)用的目錄中。

    其中:1、2、3都是全局共享的。

    4、5都是針對(duì)獨(dú)立應(yīng)用的。

    最后一種5生效的唯一理由是前面的4的文件找不到,如果有4,則5的配置將會(huì)完全被屏蔽掉。

    This method allows dynamic reconfiguration of the web application, since the main conf/server.xml file cannot be reloaded without restarting Tomcat. Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. Instead, put them in the META-INF/context.xml directory of your WAR file or the conf directory as described above.

    以上的描述可以發(fā)現(xiàn),conf/server.xml file cannot be reloaded without restarting Tomcat。

    反之如果配置到其他地方卻可以自動(dòng)reload。自動(dòng)重載意味著什么我就不多說(shuō)了。

    為了聯(lián)系配置JNDI,我安裝了mysql 5.0.18

    在其中建立了一個(gè)測(cè)試的庫(kù),和一張表,插入了一點(diǎn)兒數(shù)據(jù)。

    JNDI的使用,分為兩種:全局和局部的。

    全局的:

    conf/server.xml中配置(注意,配置Resource的方法在Tomcat的以前版本中不是這樣的,需要按照這個(gè)簡(jiǎn)化語(yǔ)法來(lái)改,否則就和我一樣折騰半死也不明白為什么了。)

    < GlobalNamingResources >
    ...
    ????
    < Resource
    ??????
    name ="student"
    ??????type
    ="javax.sql.DataSource"
    ??????driverClassName
    ="com.mysql.jdbc.Driver"
    ??????password
    ="1"
    ??????maxIdle
    ="2"
    ??????maxWait
    ="5000"
    ??????username
    ="root"
    ??????url
    ="jdbc:mysql://localhost:3306/schoolproject"
    ??????maxActive
    ="4" />

    ...
    ??
    </ GlobalNamingResources >

    配置完成之后,就是使用它了。

    有兩個(gè)地方,根據(jù)前文描述,選擇一個(gè)。

    建立META-INF/context.xml(使用方法5,在局部配置時(shí)掩飾使用方法4)

    <?xml version="1.0" encoding="UTF-8"?>
    ??? < Context? path ="/test_deploy" >
    ????
    < ResourceLink? name ="student" ?type ="javax.sql.DataSource" ?global ="student" />
    </ Context >

    其中的name是程序中引用,其中的global是在全局Resource中定義的name。

    寫(xiě)個(gè)測(cè)試的jsp

    index.jsp

    <% @?page?language = " java " ? import = " javax.sql.*,java.sql.*,javax.naming.* " ?contentType = " text/html;charset=UTF-8 " %>
    < html >
    ????
    < head >
    ????????
    < title >
    ????????????test
    ????????
    </ title >
    ????
    < head >
    ????
    < body >
    ????????Hello?olojiang
    !< br />
    ????????
    <%
    ????????????Context?initCtx?
    = ? new ?InitialContext();
    ????????????DataSource?ds?
    = ?(DataSource)initCtx.lookup( " java:comp/env/student " );
    ????????????
    if (ds? != ? null ) {
    ????????????????Connection?conn?
    = ?ds.getConnection();
    ????????????????Statement?st?
    = ?conn.createStatement();
    ????????????????
    ????????????????ResultSet?rs?
    = ?st.executeQuery( " select?*?from?student " );
    ????????????????
    while (rs.next()) {
    ????????????????????out.print(rs.getString(
    1 )? + ? " ? " );
    ????????????????????out.print(rs.getString(
    2 )? + ? " ? " );
    ????????????????????out.print(rs.getString(
    3 )? + ? " ? " );
    ????????????????????out.println(rs.getInt(
    4 )? + ? " <br/> " );
    ????????????????}

    ????????????????st.close();
    ????????????????conn.close();
    ????????????}

    ????????
    %>
    ????
    </ body >
    </ html >

    ?完美輸出結(jié)果。

    局部的:

    conf\Catalina\localhost\DBTest.xml(使用方法4,前面演示了使用方法5)

    在其中書(shū)寫(xiě)可以有動(dòng)態(tài)重載的優(yōu)勢(shì),DBTest時(shí)目錄名稱(chēng)。

    < Context? path ="/DBTest" ?docBase ="DBTest" ?debug ="5" ?reloadable ="true" ?crossContext ="true" >
    ????
    < Resource
    ????????
    name ="jdbc/TestDB"
    ????????type
    ="javax.sql.DataSource"
    ????????driverClassName
    ="com.mysql.jdbc.Driver"
    ????????password
    ="1"
    ????????maxIdle
    ="2"
    ????????maxWait
    ="5000"
    ????????username
    ="root"
    ????????url
    ="jdbc:mysql://localhost:3306/schoolproject"
    ????????maxActive
    ="4" />
    </ Context >

    配置好了之后,比前面多處一步:需要在應(yīng)用的WEB-INF/web.xml中寫(xiě)引用。(使用全局的配置不需要作這一部也能成功,這里不作這步我也好像成功了,但是很多地方都寫(xiě)需要這步,不知為何?

    <? xml?version="1.0"?encoding="UTF-8" ?>
    < web-app? xmlns ="http://java.sun.com/xml/ns/j2ee"
    ????xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
    ????xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

    ????version
    ="2.4" >
    ??
    < description > MySQL?Test?App </ description >
    ??
    < resource-ref >
    ??????
    < description > DB?Connection </ description >
    ??????
    < res-ref-name > jdbc/TestDB </ res-ref-name >
    ??????
    < res-type > javax.sql.DataSource </ res-type >
    ??????
    < res-auth > Container </ res-auth >
    ??
    </ resource-ref >
    </ web-app >

    ?使用同樣的JSP測(cè)試之:

    <% @?page?language = " java " ? import = " javax.sql.*,java.sql.*,javax.naming.* " ?contentType = " text/html;charset=UTF-8 " %>
    < html >
    ????
    < head >
    ????????
    < title >
    ????????????test
    ????????
    </ title >
    ????
    < head >
    ????
    < body >
    ????????Hello?olojiang
    !< br />
    ????????
    <%
    ????????????Context?initCtx?
    = ? new ?InitialContext();
    ????????????DataSource?ds?
    = ?(DataSource)initCtx.lookup( " java:comp/env/jdbc/TestDB " );
    ????????????
    if (ds? != ? null ) {
    ????????????????Connection?conn?
    = ?ds.getConnection();
    ????????????????Statement?st?
    = ?conn.createStatement();
    ????????????????
    ????????????????ResultSet?rs?
    = ?st.executeQuery( " select?*?from?student " );
    ????????????????
    while (rs.next()) {
    ????????????????????out.print(rs.getString(
    1 )? + ? " ? " );
    ????????????????????out.print(rs.getString(
    2 )? + ? " ? " );
    ????????????????????out.print(rs.getString(
    3 )? + ? " ? " );
    ????????????????????out.println(rs.getInt(
    4 )? + ? " <br/> " );
    ????????????????}

    ????????????????st.close();
    ????????????????conn.close();
    ????????????}

    ????????
    %>
    ????
    </ body >
    </ html >

    運(yùn)行完美。

    兩種方法都說(shuō)了,自己走了很多彎路,配這么點(diǎn)兒東西折騰了一整天,忘后來(lái)者能順利些。

    不足處請(qǐng)多包涵。

    開(kāi)心ing!~

    posted on 2007-03-29 11:21 imcb 閱讀(980) 評(píng)論(1)  編輯  收藏

    評(píng)論

    # re: 在Tomcat 5.5.20中配置JNDI的體會(huì) 2008-07-24 11:08 yanglu

    博主是不是在eclipse下面運(yùn)行的呢?如果是的話關(guān)閉eclipse也能運(yùn)行嗎?  回復(fù)  更多評(píng)論   


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


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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(2)

    隨筆分類(lèi)

    隨筆檔案

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 免费在线观看的黄色网址| 免费人妻精品一区二区三区| 亚洲中文字幕无码永久在线 | 免费看大黄高清网站视频在线| a级片免费在线播放| 美女无遮挡免费视频网站| 亚洲精品中文字幕无乱码麻豆| 久久久久亚洲精品成人网小说| 亚洲av午夜成人片精品电影 | 在线精品亚洲一区二区| 亚洲成年人电影网站| 亚洲AV无码不卡在线播放| 亚洲AV成人潮喷综合网| 午夜一级免费视频| 无人在线观看完整免费版视频| 性xxxx视频免费播放直播| 久久久精品视频免费观看 | 黄+色+性+人免费| 99热免费在线观看| 国产成人AV免费观看| 国产精品视频全国免费观看| 一本到卡二卡三卡免费高| 美女扒开尿口给男人爽免费视频| 亚洲GV天堂无码男同在线观看| 亚洲国产视频久久| 亚洲妇女熟BBW| 亚洲专区一路线二| 亚洲国产91在线| 亚洲日韩一区二区一无码| 亚洲色偷偷综合亚洲AV伊人蜜桃| 亚洲视频在线观看2018| 狠狠色香婷婷久久亚洲精品| 亚洲制服丝袜中文字幕| 亚洲乱亚洲乱妇无码| 久久亚洲精品无码av| 美女扒开尿口给男人爽免费视频| 七次郎成人免费线路视频| 国产福利免费视频 | 亚洲专区先锋影音| 911精品国产亚洲日本美国韩国 | 美女视频黄a视频全免费|