Prepare the environment: 1.j2sdk-00004_2-windows-i586.exe // JDK 2.MySQL-4.0.20d-win.zip //MySQLDatabase 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 // containsMySQLDriverInstallation steps: 1. Install JDK 2. install Tomcat 3. InstallMySQL 4. Install mysqlcc 5. decompress and copy the driver package.MySQL-Connector-java-3.0.14-production-bin.jar to Tomcat/common/lib Or download mm.MySQL-2.0.14-you-must-unjar-me.jar, extract and copy the MM.MySQL-2.0.14-bin. Jar In this example, Tomcat 198277 is used to configure the installation password. 1. Configure Manager to manage applications In 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> Restrict 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: http: // localhost: 8080/manager/html 2. Configure jdbcrealm Container Management SecurityMySQL-4.0 database as an Example A. Copy the driver mm.MySQL-2.0.14-bin. jar to common/lib/ B. Create a table in the database ycg 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) ); C. Modify server. XML as follows (the default database is root and has no password. If it is visible, for example: connectionurl = "JDBC:MySQL: // Localhost/authority? User = dbuser & Password = dbpass ") <Realm classname = "org. Apache. Catalina. realm. jdbcrealm" DEBUG = "99" 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 of Tomcat to the database: + ----------- + User_name role_name + ----------- + Admin Admin manager Both role1 Both Tomcat Role1 role1 Tomcat + ----------- + + ----------- + User_name user_pass + ----------- + Tomcat Both Tomcat Role1 Tomcat Administrator 198277 + ----------- + E. StartMySQL, Start Tomcat, Tomcat will then read user rule authentication from the database. The default CONF/tomcat-users.xml is invalid 3. DBCP Configuration A. Set <Parameter> <Name> removeabandoned </Name> <Value> true </value> </Parameter> Enables invalid data connections to be re-enabled. Configuration <Parameter> <Name> removeabandonedtimeout </Name> <Value> 60 </value> </Parameter> Expiration time If you want to write logs Set <Parameter> <Name> logabandoned </Name> <Value> true </value> </Parameter> The above three values are both false by default. B. UseMySQLFor example, configure the data connection pool C. to configure a new user and database, you must set a password. a blank password will cause connection failure. E. Specify the root password: mysqladmin-u root-H localhost password "198277" (You need to modify the above jdbcrealm settings connectionurl = "JDBC:MySQL: // Localhost/ycg? User = root & Password = 198277 ") CommandMySQLConnect to the server anonymously Password access Shell>MySQL-H host-u user-P Enter Password :******** // If the root user does not have a password, the following statements fail: (tried) 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 ); In CONF/server. XML, add <Context Path = "/dbtest" docbase = "dbtest" DEBUG = "5" reloadable = "true" crosscontext = "true"> <Logger classname = "org. Apache. Catalina. Logger. filelogger" Prefix = "localhost_dbtest_log." suffix = ". txt" Timestamp = "true"/> <Resource Name = "JDBC/testdb" Auth = "Container" Type = "javax. SQL. datasource"/> <Resourceparams name = "JDBC/testdb"> <Parameter> <Name> factory </Name> <Value> org. Apache. commons. DBCP. basicperformancefactory </value> </Parameter> <! -- Maximum number of DB connections 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. --> <Parameter> <Name> maxactive </Name> <Value> 100 </value> </Parameter> <! -- Maximum number of idle dB connections to retain in pool. Set to 0 for no limit. --> <Parameter> <Name> maxidle </Name> <Value> 30 </value> </Parameter> <! -- Maximum time to wait for a 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. --> <Parameter> <Name> maxwait </Name> <Value> 10000 </value> </Parameter> <! --MySQLDB username and password for DB connections --> <Parameter> <Name> username </Name> <Value> javauser </value> </Parameter> <Parameter> <Name> password </Name> <Value> javadude </value> </Parameter> <! -- Class name for the old mm.MySQLJDBC driver-uncomment this entry and comment next If you want to use this driver-we recommend using connector/J though <Parameter> <Name> driverclassname </Name> <Value> org. gjt. Mm.MySQL. Driver </value> </Parameter> --> <! -- Class name for the officialMySQLConnector/J driver --> <Parameter> <Name> driverclassname </Name> <Value> COM.MySQL. JDBC. Driver </value> </Parameter> <! -- The JDBC connection URL for connecting to yourMySQLDB. The autoreconnect = true argument to the URL makes sure that Mm.MySQLJDBC driver will automatically reconnect if mysqld closed Connection. mysqld by default closes idle connections after 8 hours. --> <Parameter> <Name> URL </Name> <Value> JDBC:MySQL: // Localhost: 3306/javatest? Autoreconnect = true </value> </Parameter> <Parameter> <Name> removeabandoned </Name> <Value> true </value> </Parameter> <Parameter> <Name> removeabandonedtimeout </Name> <Value> 60 </value> </Parameter> <Parameter> <Name> logabandoned </Name> <Value> true </value> </Parameter> </Resourceparams> </Context> F. Call. Configure web. XML in the Web service, for example: <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/j2ee Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" Version = "2.4"> <Description>MySQLTest 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 <% @ 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> <HTML> <Head> <Title> dB test </title> </Head> <Body> <H2> results </H2> <C: foreach Var = "row" items = "$ {Rs. Rows}"> Foo $ {row. Foo} <br/> Bar $ {row. Bar} <br/> </C: foreach> </Body> </Html> H. Create a 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 Test. jsp Copy to webapps/ I. StartMySQL, Tomcat Access: Http: // localhost: 8080/dbtest/test. jsp Display: Results Foo hello Bar 12345 4. Configure SSL. The following uses jdk1.4.2 as an example. A. Enter % java_home %/bin Run the command: keytool-genkey-alias tomcat-keyalg RSA Take tomcat installation password 198277 and ketool password 198277 as an example. Enter the keystore password: 198277 What is your first name and last name? [UNKNOWN]: ycg What is the name of your organization? [UNKNOWN]: nju What is your organization 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 this unit? [UNKNOWN]: Nd CN = ycg, ou = nju, O = nju, L = Nanjing, St = Jiangsu, c = Nd? [No]: Y Enter the master password of <Tomcat> (If the password is the same as the keystore password, press Enter): 198277 B. You can find the. keystore file in the current user directory of D:/Documents and Settings/. Copy It To The CONF/folder. C. Find in server. xml <! -- <Connection Port = "8443" Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75" Enablelookups = "false" disableuploadtimeout = "true" Acceptcount = "100" DEBUG = "0" Scheme = "HTTPS" secure = "true" Clientauth = "false" sslprotocol = "TLS"/> --> Remove comments Add the configuration field: keystorefile = "/CONF/. keystore" keystorepass = "198277" Example: <! -- Define a SSL coyote HTTP/1.1 Connector on port 8443 --> <Connection Port = "8443" Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75" Enablelookups = "false" disableuploadtimeout = "true" Acceptcount = "100" DEBUG = "0" Scheme = "HTTPS" secure = "true" Clientauth = "false" sslprotocol = "TLS" keystorefile = "/CONF/. keystore" Keystorepass = "198277"/> D. test: Https: // localhost: 8443 E. Add the SSL authentication method in your program as follows: Add <web-app> </Web-app> in Web. xml <Security-constraint> <Web-resource-collection> <Web-resource-Name> success </Web-resource-Name> <URL-pattern>/</url-pattern> <Http-method> Get </HTTP-method> <Http-method> post </HTTP-method> </Web-resource-collection> <User-data-constraint> <Transport-guarantee> confidential </transport-guarantee> </User-data-constraint> </Security-constraint> F. For example Modify web. XML <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/j2ee Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" Version = "2.4"> <Description>MySQLTest app </description> <Security-constraint> <Web-resource-collection> <Web-resource-Name> success </Web-resource-Name> <URL-pattern>/</url-pattern> <Http-method> Get </HTTP-method> <Http-method> post </HTTP-method> </Web-resource-collection> <User-data-constraint> <Transport-guarantee> confidential </transport-guarantee> </User-data-constraint> </Security-constraint> <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> Access: Https: // localhost: 8443/dbtest/test. jsp G. If it is combined with jdbcrealm configured in 2 for form Authentication First, add user_name: ycg role_name: Web-user to the user_roles table. Add user_name: ycg user_pass: 198277 to the users table Add <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> The modified web. XML is as follows: <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/j2ee Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" Version = "2.4"> <Description>MySQLTest app </description> <Security-constraint> <Web-resource-collection> <Web-resource-Name> success </Web-resource-Name> <URL-pattern>/</url-pattern> <Http-method> Get </HTTP-method> <Http-method> post </HTTP-method> </Web-resource-collection> <Auth-constraint> <Role-Name> Web-user </role-Name> </Auth-constraint> <User-data-constraint> <Transport-guarantee> confidential </transport-guarantee> </User-data-constraint> </Security-constraint> <Login-config> <Auth-method> basic </auth-method> <Realm-Name> my member area </realm-Name> </Login-config> <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> Test: Http: // localhost: 8080/dbtest/test. jsp The connection is connected through SSL and form authentication is performed. The user password can be added to user_roles and users. 5. ChineseGarbledProblem: MySQLDefault encoding ISO Tomcat request transmission code ISO To display Chinese Characters Add in *. jsp <Head> <% @ Page Language = "Java" Contenttype ="Text/Html; charset = gb18030" Pageencoding = "gb18030" %> </Head> ForGarbled(For exampleMySQLData read from the database) Use the following two transcoding functions for transcoding. If you do not know which encoding to convert to which encoding, try more. // Transcode 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 to GBK Public String togbk (string input ){ Try { Byte [] bytes = input. getbytes ("ISO8859-1 "); Return new string (bytes, "GBK "); } Catch (exception ex ){ } Return input; } |