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

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

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

    配置Jetty的JNDI綁定

    Posted on 2008-04-27 13:13 wind_miao 閱讀(2224) 評論(2)  編輯  收藏 所屬分類: J2EE

    配置 JNDI綁定

    一、 此處綁定的數(shù)據(jù)源是以 DBCP 為實現(xiàn)。首先必須將數(shù)據(jù)庫驅(qū)動(這里用了MYSQL數(shù)據(jù)庫)和DBCP所需要的 Jar 包復(fù)制到 Jetty 根目錄的 lib 目錄下。DBCP主要需要以下3個文件:
    Commons-dbcp.jar
    Commons-pool.jar
    Commons-collections.jar
    二、 在Jetty根目錄的contexts下建立wind.xml(該文件名為了增加可讀性最好與項目名相同)
    wind.xml的內(nèi)容如下:
    --------------------------------------------------------------------------------------------------------------------------
    <?xml version="1.0"  encoding="GB2312"?>
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
    <!-- 配置一個WEB應(yīng)用 -->
    <Configure class="org.mortbay.jetty.webapp.WebAppContext">
      <Set name="contextPath">/wind</Set>
      <Set name="resourceBase">E:/StartPortableApps/jspTest</Set>

    <!-- 配置第一個環(huán)境變量 -->
    <New id="woggle" class="org.mortbay.jetty.plus.naming.EnvEntry">
      <Arg>woggle</Arg>
      <Arg type="java.lang.Integer">4000</Arg>
    </New>

    <!-- 配置第二個環(huán)境變量 -->
    <New id="wiggle" class="org.mortbay.jetty.plus.naming.EnvEntry">
      <Arg>wiggle</Arg>
      <Arg type="boolean">true</Arg>
    </New>

    <!-- 創(chuàng)建數(shù)據(jù)源 -->
    <New id="ds" class="org.apache.commons.dbcp.BasicDataSource">
      <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
      <Set name="url">jdbc:mysql://localhost:3306/test</Set>
      <Set name="username">root</Set>
      <Set name="password">wind</Set>
      <Set name="maxActive" type="int">100</Set>
      <Set name="maxIdle" type="int">30</Set>
      <Set name="maxWait" type="int">1000</Set>
      <Set name="defaultAutoCommit" type="boolean">true</Set>
      <Set name="removeAbandoned" type="boolean">true</Set>
      <Set name="removeAbandonedTimeout" type="int">60</Set>
      <Set name="logAbandoned" type="boolean">true</Set>
    </New>

    <!-- 將實際的數(shù)據(jù)源綁定到 jdbc/mydatasource 這個 JNDI 名 -->
    <New id="mydatasource" class="org.mortbay.jetty.plus.naming.Resource">
      <Arg>jdbc/mydatasource</Arg>
      <Arg><Ref id="ds"/></Arg>
    </New>
    </Configure>
    --------------------------------------------------------------------------------------------------------------------------
    三、 下面是測試該JNDI的jsp和servlet。
    (1)在E:/StartPortableApps/jspTest(wind.xml設(shè)置的虛擬目錄的絕對路徑)下創(chuàng)建:index.jsp
    <%@ page language="java" pageEncoding="GB2312"%>
    <%
     String path = request.getContextPath();
     String basePath = request.getScheme() + "://"
       + request.getServerName() + ":" + request.getServerPort()
       + path + "/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
     <head>
      <base href="<%=basePath%>">

      <title>My JSP 'index.jsp' starting page</title>

      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
      <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

     </head>

     <body>
      <form method="post" action="aa" name="f1"><p>&nbsp;<input type="submit" value="test" name="button1"></p></form>
     </body>
    </html>
    (2)TestServlet.java內(nèi)容如下:
    package lee;

    import java.io.IOException;
    import java.io.PrintStream;
    import java.sql.*;
    import javax.naming.InitialContext;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import javax.sql.DataSource;

    public class TestServlet extends HttpServlet
    {
        InitialContext ic;

        public TestServlet()
        {
        }

        public void destroy()
        {
            super.destroy();
        }

        protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            PrintStream out = new PrintStream(response.getOutputStream());
            try
            {
                out.println(ic.lookup("wiggle"));
                out.println(ic.lookup("woggle"));
                DataSource ds = (DataSource)ic.lookup("jdbc/mydatasource");
                Connection conn = ds.getConnection();
                Statement stmt = conn.createStatement();
                for(ResultSet rs = stmt.executeQuery("select * from echo_message"); rs.next(); out.println(rs.getString(2)));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        public void init(ServletConfig config)
            throws ServletException
        {
            super.init(config);
            try
            {
                ic = new InitialContext();
            }
            catch(Exception e)
            {
                throw new ServletException(e);
            }
        }
    }
    (3)web.xml內(nèi)容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
     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">
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>lee.TestServlet</servlet-class>
      </servlet>

      <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/aa</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    Feedback

    # re: 配置Jetty的JNDI綁定[未登錄]  回復(fù)  更多評論   

    2010-04-27 16:34 by xx
    兄弟,太感謝了,謝謝

    # re: 配置Jetty的JNDI綁定  回復(fù)  更多評論   

    2014-06-24 10:26 by ARC-蜜蜂詞
    非常感謝了,我配了好幾天的jetty連接池,就沒有去綁定的問題,以后多交流下

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


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

    posts - 1, comments - 3, trackbacks - 0, articles - 7

    Copyright © wind_miao

    主站蜘蛛池模板: 亚洲AV无码资源在线观看| 亚洲伊人tv综合网色| 亚洲日韩精品国产一区二区三区| 无码午夜成人1000部免费视频| 亚洲啪啪综合AV一区| 13小箩利洗澡无码视频网站免费| 久久久久亚洲AV成人无码| 两个人看的www免费视频中文| 国产亚洲无线码一区二区| 国产在线一区二区综合免费视频 | 69视频在线是免费观看| 亚洲国产超清无码专区| 天天操夜夜操免费视频| 国产精品亚洲va在线观看| 久久精品国产亚洲7777| 久久九九全国免费| 亚洲最新黄色网址| 成年女人免费v片| 最好2018中文免费视频| 久久被窝电影亚洲爽爽爽| 99视频在线免费看| 亚洲午夜无码久久久久软件| 手机看片久久国产免费| 久青草视频97国内免费影视| 亚洲色四在线视频观看| 全免费a级毛片免费看不卡| 牛牛在线精品免费视频观看| 亚洲国产精品一区二区第一页| 麻豆视频免费观看| 日韩免费在线中文字幕| 亚洲国产精品久久久久久| 处破痛哭A√18成年片免费| 黄色短视频免费看| 亚洲人成在线精品| 亚洲精品成人区在线观看| 99爱视频99爱在线观看免费| 久久人午夜亚洲精品无码区| 亚洲不卡av不卡一区二区| 在线免费一区二区| 日本一道本不卡免费| 国产大陆亚洲精品国产|