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>
最新文章
- SVN:Previous operation has not finished; run &#39;cleanup&#39; if it was interrupted
- Android什么时候进行View中Background的加载
- Windows 10 下mysql 安装后无法启动问题
- 51Node 1065----最小正子段和
- stm32——NFC芯片--PN532的使用
- GUID分区与MBR分区
- Ibatis学习总结2--SQL Map XML 配置文件
- SQL Server Logical/Physical Reads
- ${pageContext.request.contextPath} JSP取得绝对路径
- 为啥 Objective-C 使用中括号来调用类方法?
- git workflows
- sql语句分组统计出年月日下数据记录数目
- OpenJudge 2980 大整数乘法
- [O]打印时闪退问题
- Struts2学习第一天——struts2基本流程与配置
- 不一样的 SQL Server 日期格式化
- (网页)JavaScript周末总结(一)
- Shell第二篇:正则表达式和文本处理工具
- Mongodb: Sort operation used more than the maximum 33554432 bytes of RAM
- the pitfull way to create a uClinux image including gdb.