1.如何加大tomcat连接数
在tomcat配置文件server.xml中的<Connector />配置中,和连接数相关的参数有:
minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10
maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75
acceptCount:允许的最大连接数,应大于等于maxProcessors,默认值为100
enableLookups:是否反查域名,取值为:true或false。为了提高处理能力,应设置为false
connectionTimeout:网络连接超时,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。
其中和最大连接数相关的参数为maxProcessors和acceptCount。如果要加大并发连接数,应同时加大这两个参数。
web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右。tomcat5中的配置示例:
程序代码: |
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" /> |
对于其他端口的侦听配置,以此类推。
2. tomcat中如何禁止列目录下的文件
在{tomcat_home}/conf/web.xml中,把listings参数设置成false即可,如下:
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
3.如何加大tomcat可以使用的内存
tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大。
Unix下,在文件{tomcat_home}/bin/catalina.sh的前面,增加如下设置:
JAVA_OPTS='-Xms【初始化内存大小】 -Xmx【可以使用的最大内存】'
需要把这个两个参数值调大。例如:
JAVA_OPTS='-Xms256m -Xmx512m'
表示初始化内存为256MB,可以使用的最大内存为512MB
4 如何添加默认访问页面
修改文件web.xml,在welcome-file里面加入需要的页面即可。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
5. 如何添加出错页面
修改文件web.xml,在location里面加入出错页面
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
6.如何配置虚拟主机
为了实现基于域名(IP地址)的虚拟主机,修改文件server.xml,加入以下内容:
程序代码: |
<Host appBase="C:/webapp1" name="www.test1.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Alias>www.test1.com</Alias> <Alias>192.168.1.110</Alias> </Host> <Host appBase="C:/webapp2" name="www.test2.com" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Alias>www.test2.com</Alias> <Alias>192.168.1.120</Alias> </Host> |
在confCatalina目录下建立文件夹,文件夹的名称为”www.test1.com”和“www.test2.com”,然后在这两个目录下建立文件ROOT.xml,其“www.test1.com“目录下ROOT.xml内容为:
程序代码: |
<?xml version='1.0' encoding='utf-8'?> <!-- Context configuration file for the test1 App --> <Context displayName=www.test1.com path="/" docBase="" useNaming="false"> debug="0" privileged="true" reloadable="true"> <Logger className="org.apache.catalina.logger.FileLogger" directory="C:/webapp1/WEB-INF/logs" prefix="test1_log." suffix=".txt" timestamp="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="test1_access_log." suffix=".txt" pattern="combined" resolveHosts="false"/> </Context> |
7. 如何配置mysql数据源
在本机建立数据库test,将mysql的JDBC驱动mysql-connector-java-3.0.9-stable-bin.jar拷贝到目录commonlib下,
修改文件web.xml,加入以下内容:
程序代码: |
<resource-ref> <description>MySQL Datasource example</description> <res-ref-name>jdbc/test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> |
在WEB应用的配置文件里加入以下内容:
程序代码: |
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/test"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <!-- Class name for mm.mysql JDBC driver --> <parameter> <name>driverClassName</name> <value>org.gjt.mm.mysql.Driver</value> </parameter> <!-- The JDBC connection url for 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. --> <parameter> <name>url</name> <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value> </parameter> <!-- MySQL dB username and password for dB connections --> <parameter> <name>username</name> <value>root</value> </parameter> <parameter> <name>password</name> <value></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> </ResourceParams><resource-ref> <description>MySQL Datasource example</description> <res-ref-name>jdbc/test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> |
在WEB应用的配置文件里加入以下内容:
程序代码: |
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/test"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <!-- Class name for mm.mysql JDBC driver --> <parameter> <name>driverClassName</name> <value>org.gjt.mm.mysql.Driver</value> </parameter> <!-- The JDBC connection url for 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. --> <parameter> <name>url</name> <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value> </parameter> <!-- MySQL dB username and password for dB connections --> <parameter> <name>username</name> <value>root</value> </parameter> <parameter> <name>password</name> <value></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> </ResourceParams> |
8. tomcat连接池的配置
程序代码: |
<Resource name="jdbc/testDB" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/testDB"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <parameter> <name>driverClassName</name> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> </parameter> <parameter> <name>url</name> <value>jdbc:microsoft:sqlserver://218.246.85.65:1433;DatabaseName=ZCMIS</v
alue> </parameter> <parameter> <name>username</name> <value>sa</value> </parameter> <parameter> <name>password</name> <value>sa</value> </parameter> <parameter> <name>maxActive</name> <value>30</value> </parameter> <parameter> <name>maxIdle</name> <value>10</value> </parameter> <parameter> <name>maxWait</name> <value>1000</value> </parameter> </ResourceParams> |
把下面的东西加到web.xml中
程序代码: |
<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> |
类的内部实现
程序代码: |
try{ Context initCtx = new InitialContext(); Context ctx = (Context) initCtx.lookup("java:comp/env"); //获取连接池对象 Object obj = (Object) ctx.lookup("jdbc/testDB"); javax.sql.DataSource ds = (javax.sql.DataSource)obj; Connection conn = ds.getConnection();
} catch(Exception e) { System.out.print(e.toString()); } |