本例程所使用的运行环境及资源: 1. Tomcat 5.5 2. Mysql 4.1 3. mysql jdbc驱动:3.1.13 (以上程序可以直接到相关官方网站下载到) step1: 首先创建一个监听器servlet,这个监听器用来初始化共享资源,本例中主要用来在应用中创建数据源,把这个类置于WEB-I
本例程所使用的运行环境及资源:
1. Tomcat 5.5
2. Mysql 4.1
3. mysql jdbc驱动:3.1.13
(以上程序可以直接到相关官方网站下载到)
step1:首先创建一个监听器servlet,这个监听器用来初始化共享资源,本例中主要用来在应用中创建数据源,把这个类置于WEB-INF/classes/kinglong/jmediasoft/servlets/目录下,代码如下:
| @@######@@ |
step2:配置web.xml相关代码,代码如下:
| @@######@@ |
| @@######@@ |
step4:重新启动Tomcat应用,用http://localhost:8080/test/test.jsp的链接来执行此页面,我的输出如下。这是我的comicinfo表中的唯一一条记录。黑体是表的字段,后面是对应的字段值。
(责任编辑 火凤凰 sunsj@51cto.com TEL:(010)68476636-8007)
<p>/** */ /** <br>* @(#)ResourceManagerListener.java<br>*<br>* 资源初始化监听器<br>*<br>package kinglong.jmediasoft.servlets;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1051">
<img src="https://img.php.cn/upload/ai_manual/001/503/042/68b6cb08a59ca496.jpeg" alt="Voicepods">
</a>
<div class="aritcle_card_info">
<a href="/ai/1051">Voicepods</a>
<p>Voicepods是一个在线文本转语音平台,允许用户在30秒内将任何书面文本转换为音频文件。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Voicepods">
<span>93</span>
</div>
</div>
<a href="/ai/1051" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Voicepods">
</a>
</div>
<p>import javax.servlet. * ;<br>import javax.servlet.http. * ;<br>import com.mysql.jdbc.jdbc2.optional. * ;</p><p>public class ResourceManagerListener implements ServletContextListener { <br>private DataSource ds = null ; <br>public void contextInitialized(ServletContextEvent sce) {<br>ServletContext application = sce.getServletContext();<br>String jdbcurl = application.getInitParameter( " jdbcURL " );<br>String user = application.getInitParameter( " user " );<br>String password = application.getInitParameter( " password " );<br>try {<br>ds = new MysqlConnectionPoolDataSource();<br>ds.setUrl(jdbcurl);<br>ds.setUser(user);<br>ds.setPassword(password);<br>} catch (Exception e) {<br>application.log( " 无法创建数据源: " + e.getMessage()); <br>return ; <br>} <br>application.setAttribute( " dataSource " ,ds);//将数据源变量置于应用作用域中<br>} <br><br>public void contextDestroyed(ServletContextEvent sce) {<br>ServletContext application = sce.getServletContext();<br>application.removeAttribute( " dataSource " );<br>ds = null ;<br>} <br>} </p><p>< context-param > <br>< param-name > jdbcURL </ param-name> <br>< param-value > jdbc:mysql://localhost:3306/comic </ param-value> </ context-param> <br>< context-param > <br>< param-name > user </ param-name> <br>< param-value > root </ param-value> <br>< context-param > <br>< param-name > password </ param-name> <br>< param-value > root </ param-value> <br>< listener > <br>< listener-class > </p><p>kinglong.jmediasoft.servlets.ResourceManagerListener</p>
<p>< sql:query var ="comicinfo" dataSource ="$ { dataSource}" </p><p>sql ="SELECT * FROM comicinfo" /> </p><p>< html > <br>< head > <br>< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" > <br>< title > 数据库测试页面 </ title=""> </p><p>< body > </p><p>< table > <br>< c:forEach items ="$ { comicinfo.rows}" var ="row" > <br>< c:forEach items ="$ { row}" var ="column" > <br>< tr > <br>< td align ="right" >< b > $ { fn:escapeXml(column.key)}: </ b></ td> <br>< td align ="left" > $ { fn:escapeXml(column.value)} </p>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号