`
zhengdl126
  • 浏览: 2507961 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

Tomcat Web Server Administration Tool + 连接池连接问题

    博客分类:
  • java
阅读更多

附 apache-tomcat-5.5.28-admin.zip  下载

 

 

Tomcat 5.5 实用工具 Tomcat Web Server Administration Tool 运行于 Web 环境 ,通过浏览器 可以非常方便地进行 Tomcat 服务器的配置 ,如图所示:

 

 

 

但是, Tomcat 5.5 以后的 binary Core 安装版 不再提供 Tomcat Web Server Administration Tool ,如若要使用该实用工具,必须从 http://tomcat.apache.org/ 下载 Administration Web Application 压缩包,且需要在本地进行配置。

 

 

 

一、下载文件 apache-tomcat-5.5.27-admin.zip

 

 

 

 

二、将文件 apache-tomcat-5.5.27-admin.zip 解压到目录 F:\Tomcat\apache-tomcat-5.5.27

apache-tomcat-5.5.27 目录内容 为:

 

 

 

到此,实用工具 Tomcat Web Server Administration Tool 的应用程序 Administration Web Application 只是存在本地,还没有被发布到 Tomcat 服务器 中,所以通过访问 http://localhost:8080/admin 得到的结果如下:

Tomcat's administration web application is no longer installed by default. Download and install the "admin" package to use it.

 

现在开始 Tomcat 中配置 Administration Web Application

 

三、将文件 apache-tomcat-5.5.27\conf\Catalina\localhost\ admin.xml 拷贝到 Tomcat 服务器所在安装目录 E:\j2ee\Tomcat 5.5\conf\Catalina\localhost 目录。

admin.xml 具体内容:

<?xml version="1.0" encoding="UTF-8"?>

<!--

  Licensed to the Apache Software Foundation (ASF) under one or more

  contributor license agreements.  See the NOTICE file distributed with

  this work for additional information regarding copyright ownership.

  The ASF licenses this file to You under the Apache License, Version 2.0

  (the "License"); you may not use this file except in compliance with

  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

-->

<!--

    Context configuration file for the Tomcat Administration Web App

    $Id: admin.xml 565211 2007-08-13 00:09:38Z markt $

-->

<Context docBase="${catalina.home}/server/webapps/admin" privileged="true"

         antiResourceLocking="false" antiJARLocking="false">

  <!-- Uncomment this Valve to limit access to the Admin app to localhost

   for obvious security reasons. Allow may be a comma-separated list of

   hosts (or even regular expressions).

  <Valve className="org.apache.catalina.valves.RemoteAddrValve"

    allow="127.0.0.1"/>

  -->

</Context>

 

四、将 apache-tomcat-5.5.27\server\webapps 目录下的 admin 目录 整个拷贝到 Tomcat 服务器所在安装目录 E:\j2ee\Tomcat 5.5\server\webapps 目录。

 

五、编辑文件 E:\j2ee\Tomcat 5.5\conf\ tomcat-users.xml ,为该实用工具程序添加用户 zhans 信息。

tomcat-users.xml 具体内容:

<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

  <role rolename="tomcat"/>

  <role rolename="role1"/>

  <role rolename="manager"/>

  <role rolename="admin"/>

  <user username="tomcat" password="tomcat" roles="tomcat"/>

  <user username="role1" password="tomcat" roles="role1"/>

  <user username="both" password="tomcat" roles="tomcat,role1"/>

  <user username="admin" password="admin" roles="admin,manager"/>

<user username="zhans" password="nice" roles="admin,manager"/>

</tomcat-users>

注意: username/password 任意组合,但 roles= admin ”角色不可随意改动!

 

六、重启 Tomcat 服务器,访问 URL : http://localhost:8080/admin ,验证通过后,即可使用实用工具 Tomcat Web Server Administration Tool

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL

 

1 建立Context:   在Tomcat中新建一个Context,让其docBase指向程序所在目录(若您下载的是war文件,则指向该文件目录StrutsArticle )。

在tomcat\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如StrutsArticle.xml)打开添加内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
      name="jdbc/StrutsArticle "
      type="javax.sql.DataSource"
      password="123456"
      driverClassName="org.gjt.mm.mysql.Driver"
      maxIdle="2"
      maxWait="50"
      username="root"
      url="jdbc:mysql://localhost:3306/strutsarticle"
      maxActive="4"/>
</Context>


2 建立Data Source: 

进入http://localhost:81/admin, 选择Resources-Data sources进入配置数据源界面,选择
Data Source Actions ->选择Create New Data Source,进入配置详细信息界面

 

JNDI Name: -> jdbc/StrutsArticle
Data Source URL ->jdbc:mysql://localhost:3306/ strutsarticle
JDBC Driver Class-> org.gjt.mm.mysql.Driver

 

 

 

 

DBConnection.java

 

package com.tiandinet.StrutsArticle.Utils;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

public class DBConnection {
    public static synchronized Connection getConnection() throws Exception {
        try {
            Context initCtx = new javax.naming.InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("jdbc/StrutsArticle ");
            return ds.getConnection();
        } catch (Exception e) {
            throw e;
        }
    }
}

 

 

 

 

 

 

 

 

-------------------------------- 以下是参考资料

 

1)启动Tomcat服务器,打开浏览器,输入http://localhost:8080/admin (其中localhost是名称服务器或称为主机),
进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,

2)选择Resources-Data sources进入配置数据源界面,选择
Data Source Actions ->选择Create New Data Source,进入配置详细信息界面
主要内容例如下:
JNDI Name: ->jdbc/mysql
Data Source URL ->jdbc:mysql://localhost:3306/test
JDBC Driver Class-> org.gjt.mm.mysql.Driver

3)修改\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如testpool.xml)打开添加内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="123456"
      driverClassName="org.gjt.mm.mysql.Driver"
      maxIdle="2"
      maxWait="50"
      username="root"
      url="jdbc:mysql://localhost:3306/test"
      maxActive="4"/>

</Context>


内容同conf/server.xml中<GlobalNamingResources>
<Resource
      name="jdbc/mysql"
      type="javax.sql.DataSource"
      password="123456"
      driverClassName="org.gjt.mm.mysql.Driver"
      maxIdle="2"
      maxWait="50"
      username="root"
      url="jdbc:mysql://localhost:3306/test"
      maxActive="4"/>
</GlobalNamingResources>

少了这一步会报错:Cannot create JDBC driver of class '' for connect URL 'null'
4)修改web.xml

打开%TOMCAT_HOME%\conf\web.xml或yourwebapp/web-inf/web.xml,添加以下内容:
    <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。
到这里,配置工作就基本完成了!

 

5)引用JNDI时用"java:comp/env/jdbc/mysql";
建立文件测试 test.jsp:
<%@page contentType="text/html;charset=utf-8" %>
<%@page import="java.sql.*" %>
<%@page import="javax.sql.*" %>
<%@page import="javax.naming.*" %>
<html>
<head>
<title>Tomcat连接池测试</title>
</head>
<body>
<%
Context ctx=new InitialContext();
Connection conn=null;
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
Statement stmt=conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from testexample");
while(rs.next()){
out.println(rs.getInt(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
}
out.println("数据库操作成功!");
rs.close();
stmt.close();
conn.close();
   
%>
</body>
</html>

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics