站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Tomcat5.0+MySql配置JDBC,DBCP,SSL

Tomcat5.0+MySql配置JDBC,DBCP,SSL

摘要: Tomcat5.0+MySql配置JDBC,DBCP,SSL
<tr> <td> 准备环境:

1.j2sdk-1_4_2-windows-i586.exe

//jdk

2.mysql-4.0.20d-win.zip

//mysql数据库

3.mysqlcc-0.9.4-win32.zip

//mysqlGUI控制

4.jakarta-tomcat-5.0.27.exe

//tomcat服务器

5.mysql-connector-java-3.0.14-production.zip

//内含mysql驱动

安装步骤:

1.安装jdk

2.安装tomcat

3.安装mysql

4.安装mysqlcc

5.将驱动包解压,拷贝mysql-connector-java-3.0.14-production-bin.jar

到tomcat/common/lib下

或者下载mm.mysql-2.0.14-you-must-unjar-me.jar,

解压后拷贝其中的mm.mysql-2.0.14-bin.jar

Tomcat5.0配置 本例使用安装密码 198277

1.配置manager 管理应用程序

在conf/server.xml 中

添加如下:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


...

    

    

</td></tr></table>


限制IP访问配置

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


         

</td></tr></table>


测试为:http://localhost:8080/manager/html

2、配置JDBCRealm容器管理安全,以mysql-4.0数据库为例

a.拷贝驱动mm.mysql-2.0.14-bin.jar到common/lib/下

b.在数据库ycg中建表

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
create table users

(

  user_name        

  varchar(15) not null primary key,

  user_pass        

  varchar(15) not null

);

create table user_roles

(

  user_name        

  varchar(15) not null,

  role_name        

  varchar(15) not null,

  primary key (user_name, role_name)

);
</td></tr></table>


c.修改server.xml如下(默认数据库为root,无密码,如果有形如:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
connectionURL="jdbc:mysql:

//localhost/authority?

user=dbuser&password=dbpass")

      
</td></tr></table>


d.在数据库中添加入tomcat的默认配置数据:



e.启动mysql,启动tomcat,此后tomcat将从数据库中读用户规则认证.默认的conf/tomcat-users.xml失效

3.DBCP的配置

a.设置

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    removeAbandoned

        true

            
</td></tr></table>


可使失效的数据连接重新启用.

配套设置

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    removeAbandonedTimeout

          60

            
</td></tr></table>


失效时间

如果要写入日志

设置

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    logAbandoned

         true

            
</td></tr></table>


以上三个默认都是false

b.以mysql为例,配置数据连接池

c.配置新的用户与数据库,必须设定密码,空密码将导致连接失败

e.

指定root密码:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
mysqladmin -u root -h localhost

password "198277"
</td></tr></table>


(需修改上面的jdbcrealm设置connectionURL="jdbc:mysql://localhost/ycg?user=root&password=198277")

命令mysql进入匿名连接到服务器

密码访问

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
shell> mysql -h host -u user -p

Enter password: ********

//如果root没有密码,以下是不成功的.(试过了)

 mysql> GRANT ALL PRIVILEGES ON *.*

 TO javauser@localhost

    ->   IDENTIFIED BY 'javadude'

 WITH GRANT OPTION;

mysql> create database javatest;

mysql> use javatest;

mysql> create table testdata

(

    ->   id int not null

 auto_increment primary key,

    ->   foo varchar(25),

    ->   bar int);

在conf/server.xml中

中添加



  

  

  

    

      factory

      org.apache.commons.dbcp.

   BasicDataSourceFactory

    

   

 in pool. Make sure you

         configure your mysqld

   max_connections large enough to handle

         all of your db connections.

   Set to 0 for no limit.

        

    

      maxActive

      100

    

   

 connections to retain in pool.

         Set to 0 for no limit.

        

    

      maxIdle

      30

    

   

 dB connection to become available

         in ms, in this example 10

   seconds. An Exception is thrown if

         this timeout is exceeded.

   Set to -1 to wait indefinitely.

        

    

      maxWait

      10000

    

   

 password for dB connections 

    

     username

     javauser

    

    

     password

     javadude

    

   

 mysql JDBC driver - uncomment

 this entry and comment next

         if you want to use this driver

   - we recommend using Connector/J though

    

       driverClassName

       org.gjt.mm.mysql.Driver

    

    

   

   

 MySQL Connector/J driver

    

       driverClassName

       com.mysql.jdbc.Driver

    

   

   

 connecting to your MySQL dB.

         The autoReconnect=true argument

   to the url makes sure that the

         mm.mysql JDBC Driver will

   automatically reconnect if mysqld closed the

         connection.  mysqld by default

   closes idle connections after 8 hours.

        

    

      url

      jdbc:mysql://localhost:3306

  /javatest?autoReconnect=true

    

            

              removeAbandoned

              true

            

             

              removeAbandonedTimeout

              60

            

            

              logAbandoned

              true

            

  

</td></tr></table>


f.在web服务中调用.配置web.xml 如:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


  MySQL Test App

  

      DB Connection

      jdbc/TestDB

      javax.sql.DataSource

      Container

  

</td></tr></table>


g.测试用test.jsp

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
<%@ taglib uri=

"http://java.sun.com/jsp/jstl/sql"

prefix="sql" %>

<%@ taglib uri=

"http://java.sun.com/jsp/jstl/core"

prefix="c" %>



select id, foo, bar from testdata





  <head>

    DB Test

  </head>

  <body>

  

Results

Foo ${row.foo}
Bar ${row.bar}
</body>
</td></tr></table>


h.新建web应用

下载jakarta-taglibs-standard-1.1.0

copy jstl.jar and standard.jar to your web app's WEB-INF/lib

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
DBTest/

    WEB-INF/

        web.xml

        lib/

            jstl.jar

            standard.jar

    test.jsp
</td></tr></table>


拷贝到webapps/ 下

i.启动mysql,tomcat

访问:

http://localhost:8080/DBTest/test.jsp

显示:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
Results

Foo hello

Bar 12345
</td></tr></table>


4.ssl的配置,以jdk1.4.2为例

a.进入%JAVA_HOME%\bin

运行命令:keytool -genkey -alias tomcat -keyalg RSA

以tomcat 安装密码为198277,ketool设置密码为198277为例

输入keystore密码: 198277

您的名字与姓氏是什么?

[Unknown]: ycg

您的组织单位名称是什么?

[Unknown]: nju

您的组织名称是什么?

[Unknown]: nju

您所在的城市或区域名称是什么?

[Unknown]: nanjing

您所在的州或省份名称是什么?

[Unknown]: jiangsu

该单位的两字母国家代码是什么

[Unknown]: nd

CN=ycg, OU=nju, O=nju, L=nanjing, ST=jiangsu, C=nd 正确吗?

[否]: y

输入的主密码

(如果和 keystore 密码相同,按回车): 198277

b.在你的D:\Documents and Settings\的当前用户目录下可以找到.keystore文件.将其拷贝到conf/文件夹下.

c.在server.xml 中找到

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    

    
</td></tr></table>


去掉注释

添加配置字段:keystoreFile="/conf/.keystore" keystorePass="198277"

如:
<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
</td></tr></table>


d.测试为:

https://localhost:8443

e.在自己的程序中添加ssl认证方式为:

在web.xml 中添加

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>




Success



/

GET

POST





<transport-guarantee>CONFIDENTIAL

</transport-guarantee>



</td></tr></table>


f.用上提为例就是

修改web.xml 为

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    MySQL Test App





Success



/

GET

POST





<transport-guarantee>CONFIDENTIAL

</transport-guarantee>





 

  

      DB Connection

      jdbc/TestDB

      javax.sql.DataSource

      Container

  

</td></tr></table>


访问:

https://localhost:8443/DBTest/test.jsp

g.如果与2配置的jdbcRealm结合起来进行表单认证

先在user_roles表中添加user_name:ycg role_name:web-user

在users表中添加user_name:ycg user_pass:198277

然后在web.xml中添加

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


web-user





 BASIC

 My Member Area

</td></tr></table>


修改后的web.xml如:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>


    MySQL Test App





Success



/

GET

POST





web-user





<transport-guarantee>CONFIDENTIAL

</transport-guarantee>







 BASIC

 My Member Area



 

  

      DB Connection

      jdbc/TestDB

      javax.sql.DataSource

      Container

  

</td></tr></table>


测试:

http://localhost:8080/DBTest/test.jsp

将通过ssl连接,并进行表单认证.用户密码可在user_roles,和users中添加.

5.中文乱码问题:

mysql 默认编码 iso

tomcat request 传输编码 iso

如果要显示中文

在*.jsp中添加

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
<head>

<%@ page

language="java"

contentType="text/html;

charset=GB18030"

pageEncoding="GB18030"

%>

</head>
</td></tr></table>


如果是数据传输中的乱码(如用servlet从mysql数据库读出的数据)用以下两个转码函数转码,如果不清楚由哪种编码转成哪种编码,就多尝试。

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
//转码GBK转ISO

    public String toISO(String input)

 {

        try

  {

                byte[] bytes = input.getBytes("GBK");

                return new String(bytes,"ISO8859-1");

        }catch(Exception ex)

  {

        }

        return input;

    }

   

    //转码IS0转GBK

    public String toGBK(String input)

 {

        try {

            byte[] bytes =

   input.getBytes("ISO8859-1");

            return new String(bytes,"GBK");

        }catch(Exception ex) {

        }

        return input;

    }
</td></tr></table>
</td> </tr> </table>
↑返回目录
前一篇: JSP基本语法
后一篇: Eclipse与插件(tomcatPlugin Lomboz easyStruts)安装