Jetty的JNDI数据源
一、 此处绑定的数据源是以 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> <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数据源的更多相关文章
- jetty使用jndi数据源
之前将项目正常的数据源统一切换成jndi访问的形式(是将c3p0以mbean形式安装到jboss做的数据连接池), 本地测试用的jetty服务器,为了统一数据库访问部分,我也查看文档找到了jetty提 ...
- eclipse内嵌jetty(run-jetty-run插件) 配置jndi数据源
运行环境 java 6,eclipse juno,ssh(spring,hibernate,springmvc ) 1.离线安装 下载地址:http://pan.baidu.com/s/1qX67wO ...
- jetty jndi数据源
applicationContext.xml <?xml version="1.0" encoding="utf-8"?> <beans de ...
- Tomcat下使用c3p0配置jndi数据源
下载c3p0包: 下载地址:https://sourceforge.net/projects/c3p0/files/?source=navbar 解压后得到包:c3p0-0.9.2.jar,mchan ...
- mysql连接超时与jndi数据源配置
昨天有运营说添加活动不能用了,我就看了一下后台日志,发现访问数据库是报错: at java.lang.Thread.run(Thread.java:722) Caused by: com.mysql. ...
- 为tomcat动态添加jndi数据源信息
我们在开发项目的时候,总要和数据库打交道,如何获取数据源,以什么样的方式来获取,成为了我们即简单又熟悉而且不得不注意的一个问题. 那么在这里我说三种获取数据源的常用方式: 一.通过配置文件来获取 首先 ...
- jboss EAP 6.2+ 通过代码控制JNDI数据源
通过Jboss提供的API,可以操控JBoss,效果跟在管理控制台手动操作完全一样,下面是示例代码: 一.pom.xml添加依赖项 <dependency> <groupId> ...
- Spring JDBCTemplate使用JNDI数据源
xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...
- Tomcat 6 JNDI数据源详解
数据库连接池这个概念应该都不陌生,在Java中连接池也就是数据库的连接池,它是一种采用连接复用的思想避免多次连接造成资源的浪费机制. 最常见的连接池就是DBCP和C30P了,在tomcat中默认使用的 ...
随机推荐
- oracle用户密码错误导致用户锁定
解决方法:使用DBA用户将其解锁: SQL> alter user ecology account unlock; 用户已更改. 用户密码限制设置: 查看FAILED_LOGIN_ATTEMPT ...
- 【Android】android:ellipsize的使用以及一个点解决方法
EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...
- hihoCoder1381 - Little Y's Tree
Portal Description 给出一个\(n(n\leq10^5)\)个点的带边权的树.进行\(Q\)次询问:每次删除树上的\(k\)条边,求剩下的\(k+1\)个连通块中最远点对距离的和.\ ...
- 刷题总结——mayan游戏(NOIP2011提高组day2T3)
题目: 题目背景 NOIP2011提高组 DAY1 试题. 题目描述 Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个 7 行 5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即 ...
- [HNOI2008]越狱 (组合数学)
题目描述 监狱有连续编号为 1-N 的 N 个房间,每个房间关押一个犯人,有 M 种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. 输入输出 ...
- 关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- 洛谷 [P2964] 硬币的游戏
博弈论+dp 依旧是博弈论的壳子,但问的是最大值,所以要dp 设 dp[i][j] 表示该取 i 号硬币,上一次取了 j 个的先手能取的最大值, 因为每次从小到大枚举复杂度太高,所以我们要从 dp[i ...
- 【BZOJ1412】狼和羊的故事(最小割)
题意:将一个由0,1,2构成的矩阵里的1与2全部分割最少需要选取多少条边 n,m<=100 思路:裸的最小割模型 相邻的格子连容量为1的边(其实可以少连很多遍,1与1,2与2之间的边是没有意义的 ...
- 星球大战 BZOJ 1015
星球大战 [问题描述] 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- Java面试题集(二)
51.设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,写出程序. 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题. public class ThreadTest1 ...