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中默认使用的 ...
随机推荐
- 九度oj 题目1114:神奇的口袋
题目描述: 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中 ...
- 刷题总结——bzoj1725(状压dp)
题目: 题目描述 Farmer John 新买了一块长方形的牧场,这块牧场被划分成 N 行 M 列(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地. FJ ...
- Spoj-FACVSPOW Factorial vs Power
Consider two integer sequences f(n) = n! and g(n) = an, where n is a positive integer. For any integ ...
- intellij idea 使用用到的问题
1.github error setting certificate verify locations使用github时报错,解决方法: git config --system http.sslcai ...
- ElasticSearch 索引查询使用指南——详细版
我们通常用用_cat API检测集群是否健康. 确保9200端口号可用: curl 'localhost:9200/_cat/health?v' 绿色表示一切正常, 黄色表示所有的数据可用但是部分副本 ...
- wamp Apache和mysql服务无法启动的终极解决方法!!!!!!
用了几年的wampserver 突然宣告无法启动,Apache和mysql都崩溃了,在计算机的服务选项里面也是无法启动的,系统报了一个未知错误,重装了N个版本的PHP集成开发环境,都宣告失败! 我想应 ...
- MongoDB_副本集集群模式
主从模式: 在10.3.13.213 主节点, 10.3.2.33 从节点.mongodb 安装路径均为:/usr/local/server/mongodb 参考文章:http://www.lance ...
- BestCoder Round #29 1003 (hdu 5172) GTY's gay friends [线段树 判不同 预处理 好题]
传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- MySQL 游戏排行榜
今天在坛子上看到了,顺便写下来. 有两种方法: 1.效率不高,因为有子查询.但是简洁.而且我对SOCRES表做了INDEX.所以性能上也差不了多少. mysql> show create tab ...