配置Jetty的JNDI綁定

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

          配置 JNDI綁定

          一、 此處綁定的數(shù)據(jù)源是以 DBCP 為實(shí)現(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(該文件名為了增加可讀性最好與項(xiàng)目名相同)
          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í)際的數(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

          主站蜘蛛池模板: 新平| 武清区| 白银市| 平阴县| 桓台县| 嘉黎县| 肃宁县| 丹寨县| 古浪县| 三明市| 樟树市| 老河口市| 阿坝县| 灵川县| 大足县| 岳阳县| 金阳县| 山东省| 新乡县| 驻马店市| 望奎县| 华池县| 昂仁县| 南漳县| 庄河市| 攀枝花市| 永城市| 晋城| 黔江区| 时尚| 夏津县| 明星| 丹东市| 邵东县| 西乡县| 松原市| 绩溪县| 淅川县| 甘孜县| 舟山市| 泰安市|