仓酷云

标题: JAVA网页设计Tomcat5.0+MySql设置JDBC,DBCP,SSL [打印本页]

作者: 变相怪杰    时间: 2015-1-18 11:09
标题: JAVA网页设计Tomcat5.0+MySql设置JDBC,DBCP,SSL
C++编译的是本地码,优点是启动快,而且可以精确控制资源因此可以开发很高效的程序.缺点是编程麻烦,而且容易留下安全隐患.跨平台靠源代码在各个平台间分别编译(一处编写到处编译)mysql筹办情况:

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中

增加以下:
  1. <Servicename="Catalina">...<Contextpath="/manager"debug="0"privileged="true"docBase="/usr/local/kinetic/tomcat5/server/webapps/manager"></Context></Service>
复制代码

限定IP会见设置
  1. <Contextpath="/manager"debug="0"privileged="true"docBase="/usr/local/kinetic/tomcat5/server/webapps/manager"><ValveclassName="org.apache.catalina.valves.RemoteAddrValve"allow="127.0.0.1"/></Context>
复制代码

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

2、设置JDBCRealm容器办理平安,以mysql-4.0数据库为例

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

b.在数据库ycg中建表
  1. createtableusers(user_namevarchar(15)notnullprimarykey,user_passvarchar(15)notnull);createtableuser_roles(user_namevarchar(15)notnull,role_namevarchar(15)notnull,primarykey(user_name,role_name));
复制代码

c.修正server.xml以下(默许数据库为root,无暗码,假如无形如:
  1. connectionURL="jdbc:mysql://localhost/authority?user=dbuser&password=dbpass")<RealmclassName="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.在数据库中增加进tomcat的默许设置数据:

JAVA网页设计Tomcat5.0+MySql设置JDBC,DBCP,SSL
登录/注册后可看大图


e.启动mysql,启动tomcat,今后tomcat将从数据库中读用户划定规矩认证.默许的conf/tomcat-users.xml生效

3.DBCP的设置

a.设置
  1. <parameter><name>removeAbandoned</name><value>true</value></parameter>
复制代码

可以使生效的数据毗连从头启用.

配套设置
  1. <parameter><name>removeAbandonedTimeout</name><value>60</value></parameter>
复制代码

生效工夫

假如要写进日记

设置
  1. <parameter><name>logAbandoned</name><value>true</value></parameter>
复制代码

以上三个默许都是false

b.以mysql为例,设置数据毗连池

c.设置新的用户与数据库,必需设定暗码,空暗码将招致毗连失利

e.

指定root暗码:
  1. mysqladmin-uroot-hlocalhostpassword"198277"
复制代码

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

命令mysql进进匿名毗连到服务器

暗码会见
  1. shell>mysql-hhost-uuser-pEnterpassword:********//假如root没有暗码,以下是不乐成的.(试过了)mysql>GRANTALLPRIVILEGESON*.*TOjavauser@localhost->IDENTIFIEDBYjavadudeWITHGRANTOPTION;mysql>createdatabasejavatest;mysql>usejavatest;mysql>createtabletestdata(->idintnotnullauto_incrementprimarykey,->foovarchar(25),->barint);在conf/server.xml中<host></host>中增加<Contextpath="/DBTest"docBase="DBTest"debug="5"reloadable="true"crossContext="true"><LoggerclassName="org.apache.catalina.logger.FileLogger"prefix="localhost_DBTest_log."suffix=".txt"timestamp="true"/><Resourcename="jdbc/TestDB"auth="Container"type="javax.sql.DataSource"/><ResourceParamsname="jdbc/TestDB"><parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter><!--MaximumnumberofdBconnectionsinpool.Makesureyouconfigureyourmysqldmax_connectionslargeenoughtohandleallofyourdbconnections.Setto0fornolimit.--><parameter><name>maxActive</name><value>100</value></parameter><!--MaximumnumberofidledBconnectionstoretaininpool.Setto0fornolimit.--><parameter><name>maxIdle</name><value>30</value></parameter><!--MaximumtimetowaitforadBconnectiontobecomeavailableinms,inthisexample10seconds.AnExceptionisthrownifthistimeoutisexceeded.Setto-1towaitindefinitely.--><parameter><name>maxWait</name><value>10000</value></parameter><!--MySQLdBusernameandpasswordfordBconnections--><parameter><name>username</name><value>javauser</value></parameter><parameter><name>password</name><value>javadude</value></parameter><!--Classnamefortheoldmm.mysqlJDBCdriver-uncommentthisentryandcommentnextifyouwanttousethisdriver-werecommendusingConnector/Jthough<parameter><name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value></parameter>--><!--ClassnamefortheofficialMySQLConnector/Jdriver--><parameter><name>driverClassName</name><value>com.mysql.jdbc.Driver</value></parameter><!--TheJDBCconnectionurlforconnectingtoyourMySQLdB.TheautoReconnect=trueargumenttotheurlmakessurethatthemm.mysqlJDBCDriverwillautomaticallyreconnectifmysqldclosedtheconnection.mysqldbydefaultclosesidleconnectionsafter8hours.--><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.在web服务中挪用.设置web.xml如:
  1. <web-appxmlns="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>MySQLTestApp</description><resource-ref><description>DBConnection</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
  1. <%@tagliburi="http://java.sun.com/jsp/jstl/sql"prefix="sql"%><%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%><sql:queryvar="rs"dataSource="jdbc/TestDB">selectid,foo,barfromtestdata</sql:query><html><head><title>DBTest</title></head><body><h2>Results</h2><c:forEachvar="row"items="${rs.rows}">Foo${row.foo}<br/>Bar${row.bar}<br/></c:forEach></body></html>
复制代码

h.新建web使用

下载jakarta-taglibs-standard-1.1.0

copyjstl.jarandstandard.jartoyourwebappsWEB-INF/lib
  1. DBTest/WEB-INF/web.xmllib/jstl.jarstandard.jartest.jsp
复制代码

拷贝到webapps/下

i.启动mysql,tomcat

会见:

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

显现:
  1. ResultsFoohelloBar12345
复制代码

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

a.进进%JAVA_HOME%in

运转命令:keytool-genkey-aliastomcat-keyalgRSA

以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:DocumentsandSettings确当前用户目次下能够找到.keystore文件.将其拷贝到conf/文件夹下.

c.在server.xml中找到
  1. <!--<Connectorport="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"

如:
  1. <Connectorport="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.测试为:

https://localhost:8443

e.在本人的程序中增加ssl认证体例为:

在web.xml中增加
  1. <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.用上提为例就是

修正web.xml为
  1. <web-appxmlns="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>MySQLTestApp</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>DBConnection</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>
复制代码

会见:

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

g.假如与2设置的jdbcRealm分离起来举行表单认证

先在user_roles表中增加user_name:ycgrole_name:web-user

在users表中增加user_name:ycguser_pass:198277

然后在web.xml中增加
  1. <auth-constraint><role-name>web-user</role-name></auth-constraint><login-config><auth-method>BASIC</auth-method><realm-name>MyMemberArea</realm-name></login-config>
复制代码

修正后的web.xml如:
  1. <web-appxmlns="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>MySQLTestApp</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>MyMemberArea</realm-name></login-config><resource-ref><description>DBConnection</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>
复制代码

测试:

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

将经由过程ssl毗连,并举行表单认证.用户暗码可在user_roles,和users中增加.

5.中文乱码成绩:

mysql默许编码iso

tomcatrequest传输编码iso

假如要显现中文

在*.jsp中增加
  1. <head><%@pagelanguage="java"contentType="text/html;charset=GB18030"pageEncoding="GB18030"%></head>
复制代码

假如是数据传输中的乱码(如用servlet从mysql数据库读出的数据)用以下两个转码函数转码,假如不分明由哪一种编码转成哪一种编码,就多实验。
  1. //转码GBK转ISOpublicStringtoISO(Stringinput){try{byte[]bytes=input.getBytes("GBK");returnnewString(bytes,"ISO8859-1");}catch(Exceptionex){}returninput;}//转码IS0转GBKpublicStringtoGBK(Stringinput){try{byte[]bytes=input.getBytes("ISO8859-1");returnnewString(bytes,"GBK");}catch(Exceptionex){}returninput;}
复制代码
用winrar打包j2ee的程序和用IDE打包应用程序是一样的。按照你的想法,你是不是也希望服务器都整合由一家公司提供呢?
作者: 不帅    时间: 2015-1-20 15:28
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
作者: 海妖    时间: 2015-1-26 22:24
让你能够真正掌握接口或抽象类的应用,从而在原来的Java语言基础上跃进一步,更重要的是,设计模式反复向你强调一个宗旨:要让你的程序尽可能的可重用。
作者: 乐观    时间: 2015-2-4 20:57
自从Sun推出Java以来,就力图使之无所不包,所以Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE,这也就是Sun ONE(Open Net Environment)体系。J2SE就是Java2的标准版,主要用于桌面应用软件的编程;J2ME主要应用于嵌入是系统开发,如手机和PDA的编程;J2EE是Java2的企业版,主要用于分布式的网络程序的开发,如电子商务网站和ERP系统。
作者: 精灵巫婆    时间: 2015-2-10 09:31
设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧
作者: 小妖女    时间: 2015-3-1 07:20
接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。
作者: 若天明    时间: 2015-3-10 12:55
当然你也可以参加一些开源项目,一方面可以提高自己,另一方面也是为中国软件事业做贡献嘛!开发者在互联网上用CVS合作开发,用QQ,MSN,E-mail讨论联系,天南海北的程序员分散在各地却同时开发同一个软件,是不是很有意思呢?
作者: 因胸联盟    时间: 2015-3-17 07:11
多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。
作者: 愤怒的大鸟    时间: 2015-3-24 01:24
是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2