Chinaunix首页 | 论坛 | 博客
  • 博客访问: 805803
  • 博文数量: 780
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 09:11
文章分类

全部博文(780)

文章存档

2011年(1)

2008年(779)

我的朋友
最近访客

分类:

2008-09-12 09:18:57

本文介绍如果利用监听器servlet来设置mysql应用数据源。

本例程所使用的运行环境及资源:

1. Tomcat 5.5
                                                                                     
2. Mysql 4.1

3.  mysql jdbc驱动:3.1.13

(以上程序可以直接到相关官方网站到)

step1:
     首先创建一个监听器servlet,这个监听器用来初始化共享资源,本例中主要用来在应用中创建数据源,把这个类置于WEB-INF/classes/kinglong/jmediasoft/servlets/目录下,代码如下:

/**
 * @(#)ResourceManagerListener.java
 *
 * 资源初始化监听器
 *
 * 
@author  ekinglong
 * 
@version  1.00 2006/10/5
 
*/


package  kinglong.jmediasoft.servlets;

import  javax.servlet. * ;
import  javax.servlet.http. * ;
import  com.mysql.jdbc.jdbc2.optional. * ;

public   class  ResourceManagerListener  implements  ServletContextListener {
 
 
private  DataSource ds = null ;
 
 
public   void  contextInitialized(ServletContextEvent sce) {
  ServletContext application
= sce.getServletContext();
  String jdbcurl
= application.getInitParameter( " jdbcURL " );
  String user
= application.getInitParameter( " user " );
  String password
= application.getInitParameter( " password " );
  
try {
   ds
= new  MysqlConnectionPoolDataSource();
   ds.setUrl(jdbcurl);
   ds.setUser(user);
   ds.setPassword(password);
  }
catch (Exception e) {
   application.log(
" 无法创建数据源: " + e.getMessage()); 
   
return ;  
  }
  
  application.setAttribute(
" dataSource " ,ds);//将数据源变量置于应用作用域中
 }

 
 
public   void  contextDestroyed(ServletContextEvent sce) {
  ServletContext application
= sce.getServletContext();
  application.removeAttribute(
" dataSource " );
  ds
= null ;
 }

}


step2:配置web.xml相关代码,代码如下:


   
< context-param >
     
< param-name > jdbcURL param-name >
     
< param-value > jdbc:mysql://localhost:3306/comic param-value >
   
context-param >
    
   
< context-param >
     
< param-name > user param-name >
     
< param-value > root param-value >
   
context-param >
   
   
< context-param >
     
< param-name > password param-name >
     
< param-value > root param-value >
   
context-param >
  
 
< listener >
     
< listener-class > kinglong.jmediasoft.servlets.ResourceManagerListener listener-class >
   
listener >  


step3:建立测试页面,测试页面代码如下:

<% @ page contentType = " text/html; charset=gb2312 "  language = " java "  import = " java.sql.* "  errorPage = ""   %>
<% @ taglib prefix = " c "  uri = "   "   %>
<% @ taglib prefix = " sql "  uri = "   "   %>
<% @ taglib prefix = " fn "  uri = "   "   %>  

 
< sql:query  var ="comicinfo"  dataSource ="$ { dataSource}"  sql ="SELECT * FROM comicinfo"   />  

< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312" >
< title > 数据库测试页面 title >
head >
< body >

< table >
< c:forEach  items ="$ { comicinfo.rows}"  var ="row" >
 
< c:forEach  items ="$ { row}"  var ="column" >
  
< tr >
  
< td  align ="right" >< b > $ { fn:escapeXml(column.key)}: b > td >
  
< td  align ="left" > $ { fn:escapeXml(column.value)} td >
  
tr >
 
c:forEach >
c:forEach >
table >  

body >
html >

step4:重新启动Tomcat应用,用的链接来执行此页面,我的输出如下。这是我的comicinfo表中的唯一一条记录。黑体是表的字段,后面是对应的字段值。

【责编:Peng】

--------------------next---------------------

阅读(443) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~