码迷,mamicode.com
首页 > 其他好文 > 详细

Jetty的JNDI数据源

时间:2014-09-28 21:11:35      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   os   ar   java   for   文件   

 

一、 此处绑定的数据源是以 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数据源

标签:des   style   http   io   os   ar   java   for   文件   

原文地址:http://www.cnblogs.com/allforone/p/3998713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!