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中的配置示例:
對于其他端口的偵聽配置,以此類推。
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,加入以下內容:
在confCatalina目錄下建立文件夾,文件夾的名稱為”www.test1.com”和“www.test2.com”,然后在這兩個目錄下建立文件ROOT.xml,其“www.test1.com“目錄下ROOT.xml內容為:
7. 如何配置mysql數據源
在本機建立數據庫test,將mysql的JDBC驅動mysql-connector-java-3.0.9-stable-bin.jar拷貝到目錄commonlib下,
修改文件web.xml,加入以下內容:
在WEB應用的配置文件里加入以下內容:
在WEB應用的配置文件里加入以下內容:
8. tomcat連接池的配置
把下面的東西加到web.xml中
類的內部實現
在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()); } |