一、 此处绑定的数据源是以 DBCP 为实现。首先必须将数据库驱动(这里用了MYSQL数据库)和DBCP所需要的 Jar 包复制到 Jetty 根目录的 lib 目录下。DBCP主要需要以下3个文件:
Commons-dbcp.jar
Commons-pool.jar
Commons-collections.jar
二、 在Jetty根目录的contexts下建立wind.xml(该文件名为了增加可读性最好与项目名相同)
wind.xml的内容如下:
--------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0"  encoding="GB2312"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<!-- 配置一个WEB应用 -->
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <Set name="contextPath">/wind</Set>
  <Set name="resourceBase">E:/StartPortableApps/jspTest</Set>

<!-- 配置第一个环境变量 -->
<New id="woggle" class="org.mortbay.jetty.plus.naming.EnvEntry">
  <Arg>woggle</Arg>
  <Arg type="java.lang.Integer">4000</Arg>
</New>

<!-- 配置第二个环境变量 -->
<New id="wiggle" class="org.mortbay.jetty.plus.naming.EnvEntry">
  <Arg>wiggle</Arg>
  <Arg type="boolean">true</Arg>
</New>

<!-- 创建数据源 -->
<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>

<!-- 将实际的数据源绑定到 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设置的虚拟目录的绝对路径)下创建: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内容如下:
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内容如下:
<?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>

Jetty的JNDI数据源的更多相关文章

  1. jetty使用jndi数据源

    之前将项目正常的数据源统一切换成jndi访问的形式(是将c3p0以mbean形式安装到jboss做的数据连接池), 本地测试用的jetty服务器,为了统一数据库访问部分,我也查看文档找到了jetty提 ...

  2. eclipse内嵌jetty(run-jetty-run插件) 配置jndi数据源

    运行环境 java 6,eclipse juno,ssh(spring,hibernate,springmvc ) 1.离线安装 下载地址:http://pan.baidu.com/s/1qX67wO ...

  3. jetty jndi数据源

    applicationContext.xml <?xml version="1.0" encoding="utf-8"?> <beans de ...

  4. Tomcat下使用c3p0配置jndi数据源

    下载c3p0包: 下载地址:https://sourceforge.net/projects/c3p0/files/?source=navbar 解压后得到包:c3p0-0.9.2.jar,mchan ...

  5. mysql连接超时与jndi数据源配置

    昨天有运营说添加活动不能用了,我就看了一下后台日志,发现访问数据库是报错: at java.lang.Thread.run(Thread.java:722) Caused by: com.mysql. ...

  6. 为tomcat动态添加jndi数据源信息

    我们在开发项目的时候,总要和数据库打交道,如何获取数据源,以什么样的方式来获取,成为了我们即简单又熟悉而且不得不注意的一个问题. 那么在这里我说三种获取数据源的常用方式: 一.通过配置文件来获取 首先 ...

  7. jboss EAP 6.2+ 通过代码控制JNDI数据源

    通过Jboss提供的API,可以操控JBoss,效果跟在管理控制台手动操作完全一样,下面是示例代码: 一.pom.xml添加依赖项 <dependency> <groupId> ...

  8. Spring JDBCTemplate使用JNDI数据源

    xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...

  9. Tomcat 6 JNDI数据源详解

    数据库连接池这个概念应该都不陌生,在Java中连接池也就是数据库的连接池,它是一种采用连接复用的思想避免多次连接造成资源的浪费机制. 最常见的连接池就是DBCP和C30P了,在tomcat中默认使用的 ...

随机推荐

  1. [luoguP4035] [JSOI2008]球形空间产生器(高斯消元)

    传送门 设球心的坐标为未知量 用最后一个点来表示球面到球心的距离,那么它和前n个式子相等 移项乱搞 最后高斯消元 #include <cmath> #include <cstdio& ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法】

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  3. 远程管理 KVM 虚机

    上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机.其实 virt-manager 也可以管理其他宿主机上的虚机.只需要简单的将宿主机添加进来 填入宿主机的相关信息,确定即 ...

  4. *Codeforces963C. Cutting Rectangle

    $n \leq 200000$种互不相同的矩形,给长宽和数量,都$\leq 1e12$,问有多少种大矩形只沿平行长和宽切正好切成这些矩形. 首先可以发现在一个合法情况下,有些矩形的位置是可以乱挪的,比 ...

  5. bootstrap-datatables

    刚写到datatimepicker的时候想到这个问题. 这可以说是我接触到的第一个功能如此齐全的一款依赖型插件.我把依赖于别人库的插件这么称呼. 首先上官网:http://datatables.clu ...

  6. MongoDB_单机版环境搭建

    linux上安装MogoDB http://www.runoob.com/mongodb/mongodb-linux-install.html 在https://www.mongodb.com/dow ...

  7. C++ 中new

    operator new在C++中的各种写法 (2011-09-21 14:59:33) 标签: 杂谈   乍一看,在C++中动态分配内存很简单:new是分配,delete是释放,就这么简单.然而,这 ...

  8. cssnext下一代的css

    前端技术更新迭代的速度令人咂舌,互联网+的风头刚起那几年,前端技术大多还停留在jquery阶段,按需加载还停留在seajs和requirejs的阶段,css3和H5也不过才崭露头角,但经过几年的飞速发 ...

  9. Codeforces 518 D Ilya and Escalator

    Discription Ilya got tired of sports programming, left university and got a job in the subway. He wa ...

  10. Raft算法详解

    一致性算法Raft详解 背景 熟悉或了解分布性系统的开发者都知道一致性算法的重要性,Paxos一致性算法从90年提出到现在已经有二十几年了,而Paxos流程太过于繁杂实现起来也比较复杂,可能也是以为过 ...