Tomcat5.0+mysql Configuration Jdbc,dbcp,ssl

Source: Internet
Author: User
Tags foreach auth sql mysql string version web services access
MySQL Preparation environment:

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

Jdk

2.mysql-4.0.20d-win.zip

MySQL Database

3.mysqlcc-0.9.4-win32.zip

Mysqlgui Control

4.jakarta-tomcat-5.0.27.exe

Tomcat Server

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

Contains MySQL driver

Installation steps:

1. Install JDK

2. Install Tomcat

3. Install MySQL

4. Install MYSQLCC

5. Unzip the drive pack and copy the Mysql-connector-java-3.0.14-production-bin.jar

Under the Tomcat/common/lib

or download Mm.mysql-2.0.14-you-must-unjar-me.jar,

Copy the Mm.mysql-2.0.14-bin.jar after decompression

Tomcat5.0 Configuration This example uses the install password 198277

1. Configuring the Manager management application

In the Conf/server.xml

Add the following:

       
        
         
        <service name= "Catalina" ....    <context path= "/manager" debug= "0" privileged= "true"             docbase= "/usr/local/kinetic   /tomcat5/server/ Webapps/manager ">    </Context></Service>
       
        


Restricting IP access configuration

       
        
         
        <context path= "/manager" debug= "0" privileged= "true"         docbase= "/usr/local/kinetic   /tomcat5/server/ Webapps/manager ">         <valve classname=" Org.apache.   Catalina.valves.RemoteAddrValve "                allow=" 127.0.0.1 "/></context>
       
        


Test as: http://localhost:8080/manager/html

2, configure Jdbcrealm container management security, take the mysql-4.0 database as an example

A. Copy drive Mm.mysql-2.0.14-bin.jar to common/lib/

B. Building tables in the database YCG

       
        
         
        CREATE TABLE Users (  user_name           varchar NOT NULL primary key,  user_pass           varchar () not NULL); Table User_roles (  user_name           varchar () NOT NULL,  role_name           varchar () NOT NULL,  primary Key (user_name, role_name));
       
        


C. Modify Server.xml as follows (default database is root, no password, if visible as:

       
        
         
        Connectionurl= "Jdbc:mysql://localhost/authority?user=dbuser&password=dbpass")      <Realm  className= "Org.apache.catalina.   Realm. Jdbcrealm "debug=" "             drivername="    org.gjt.mm.mysql.Driver "          connectionurl=" Jdbc:mysql:    // Localhost/ycg?user=root "         connectionname=" "    connectionpassword=" "              usertable=" users "      Usernamecol= "user_name" usercredcol= "User_pass" userroletable= "User_roles" rolenamecol= "Role_"    Name "/>
       
        


D. Add the default configuration data for Tomcat to the database:



E. Start MySQL, start Tomcat, then Tomcat will read the user rule authentication from the database. Default Conf/tomcat-users.xml Expiration

3.DBCP of configuration

A. Set up

       
        
         
        <parameter>    <name>removeAbandoned</name>        <value>true</value>            </ Parameter>
       
        


Enables the disabled data connection to be re-enabled.

Matching settings

       
        
         
        <parameter>    <name>removeAbandonedTimeout</name>          <value>60</value>            </parameter>
       
        


Expiration time

If you want to write to the log

Set up

       
        
         
        <parameter>    <name>logAbandoned</name>         <value>true</value>            </ Parameter>
       
        


All three of the above defaults are false

B. Using MySQL as an example, configure the data connection pool

C. Configure a new user and database, you must set a password, a blank password will cause the connection to fail

E.

Specify root password:

       
        
         
        mysqladmin-u root-h localhost password "198277"
       
        


(need to modify the above Jdbcrealm settings connectionurl= "jdbc:mysql://localhost/ycg?user=root&password=198277")

Command MySQL to enter anonymous connection to server

Password access

       
        
shell> mysql-h host-u user-penter Password: ********//If Root does not have a password, the following is unsuccessful. (tried) mysql> GRANT all privileges in *.* to Javauser@localhost-> identified by ' Javadude ' with GRANT OPTION; Mysql> CREATE DATABASE javatest;mysql> use javatest;mysql> CREATE TABLE testdata (-> ID int not NULL AUT O_increment primary KEY,-> foo varchar,-> bar int); add to 


F. Calling in Web services. Configure web.xml such as:

       
        
         
        <web-app 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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"    version= "2.4" >  <description>mysql Test app</description>  <resource-ref>      < Description>db connection</description>      <res-ref-name>jdbc/TestDB</res-ref-name>      <res-type>javax.sql.DataSource</res-type>      <res-auth>Container</res-auth>  </resource-ref></web-app>
       
        


G. test.jsp for testing

       
        
         
        <%@ taglib uri= "http://java.sun.com/jsp/jstl/sql" prefix= "SQL"%><%@ taglib uri= "http://java.sun.com/jsp/" Jstl/core "prefix=" C "%><sql:query var=" rs "datasource=" JDBC/TESTDB ">select ID, foo, bar from Testdata</sql :query>


H. Creating a new Web application

Download jakarta-taglibs-standard-1.1.0

Copy Jstl.jar and Standard.jar to your Web app ' s Web-inf/lib

       
        
         
        dbtest/    web-inf/        web.xml        lib/            jstl.jar    Standard.jar
       
        


Copy to webapps/

I. Start Mysql,tomcat

Access:

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

Show:

       
        
         
        Resultsfoo HelloBar 12345
       
        


4.ssl configuration, taking jdk1.4.2 as an example

A. Entering the%java_home%\bin

Run command: Keytool-genkey-alias tomcat-keyalg RSA

Set the password to 198277,ketool with the Tomcat installation password of 198277 for example

Enter KeyStore Password: 198277

What is your first and last name?

[Unknown]: YCG

What is your organizational unit name?

[Unknown]: Nju

What is your organization's name?

[Unknown]: Nju

What is the name of your city or region?

[Unknown]: Nanjing

What is the name of your state or province?

[Unknown]: Jiangsu

What is the two-letter country code for that unit?

[Unknown]: ND

CN=YCG, Ou=nju, O=nju, l=nanjing, St=jiangsu, right?

[No]: Y

Input The master password

(press ENTER if same as KeyStore password): 198277

B. keystore files can be found in the current user directory of your D:\Documents and Settings\. Copy it to the conf/folder.

C. Found in the Server.xml

       
         
          
        <!--    <connector port= "8443"    maxthreads= "minsparethreads=   " "maxsparethreads=" Enablelookups= "false"    disableuploadtimeout= "true"   acceptcount= "debug=" 0 "scheme="   https " Secure= "true"   clientauth= "false" sslprotocol= "TLS"/>    -->
       
         


Remove comments

Add Configuration field: keystorefile= "/conf/.keystore" keystorepass= "198277"

Such as:

       
         
          
        <connector port= "8443" maxthreads= "minsparethreads=" "maxsparethreads=" "enablelookups="  false "   disableuploadtimeout=" true "    acceptcount=" debug= "0"  scheme= "https" secure= "true"     Clientauth= "false" sslprotocol= "TLS"  keystorefile= "/conf/.keystore"       keystorepass= "198277"/>
       
         


D. Testing as:

https://localhost:8443

E. Add SSL authentication in your own program as follows:

In the Web.xml Add to

       
          
           
        <security-constraint><web-resource-collection><web-resource-name>success</ web-resource-name><url-pattern>/</url-pattern>


F. The example of using a reference is

Modify Web.xml to

         
          
<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:sch emalocation= "http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4" > & Lt;description>mysql Test app</description><security-constraint><web-resource-collection> <web-resource-name>Success</web-resource-name><url-pattern>/</url-pattern>< Http-method>get


Access:

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

G. If the Jdbcrealm is combined with the 2 configuration to form authentication

Add USER_NAME:YCG to the User_roles table first Role_name:web-user

Add USER_NAME:YCG user_pass:198277 to the Users table

Then add the Web.xml in the

       
          
           
        <auth-constraint><role-name>web-user</role-name></auth-constraint><login-config> <auth-method>BASIC</auth-method> <realm-name>my member Area</realm-name></login-config >
       
          


Modified Web.xml such as:

         
          
<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xsi:sch emalocation= "http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4" > & Lt;description>mysql Test app</description><security-constraint><web-resource-collection> <web-resource-name>Success</web-resource-name><url-pattern>/</url-pattern>< Http-method>get


Test:

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

will be connected through SSL and form authentication. User passwords can be added in User_roles, and users.

5. Garbled Chinese problem:

MySQL default encoding ISO

Tomcat Request transfer encoded ISO

If you want to display Chinese

Add in *.jsp

       
          
           
        


If it is garbled in data transmission (such as the data read from the MySQL database with the servlet), use the following two transcoding function transcoding, if you do not know which code to convert to which encoding, try more.

       
          
           
        transcoding GBK to ISO public    string Toiso (String input) {        try  {                byte[] bytes = input.getbytes ("GBK");                return new String (bytes, "iso8859-1");        } catch (Exception ex)   {        } return        input;    }        transcoding IS0 Turn GBK public    string TOGBK (String input) {        try {            byte[] bytes =    input.getbytes ("iso8859-1"); return            new String (bytes, "GBK");        } catch (Exception ex) {        } return        input;    }
       
          


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.