Tomcat的配置--轉(zhuǎn)載
-
增加一個(gè)虛擬目錄
在
server.xml文件中增加
<Context path="/oicq" docBase="myweb" debug="0" reloadable="true">
</Context>
myweb說(shuō)明其相對(duì)webapps的位置,是物理存在的目錄;
/oicq說(shuō)明其相對(duì)web URL的路徑,是一個(gè)虛擬的路徑,如:
http://localhost/oicq
-
配置 JSP及Servlet
JSP文件直接放在myweb下;
編譯好的
JavaBean、Servlet放在WEB-INF下的classes目錄,而且包的路徑要與目錄路徑一致。
-
配置服務(wù)器的端口
在
server.xml文件的第56行,修改port = “8080” 為你所希望使用的端口號(hào),如:80
-
配置中文亂碼問(wèn)題
在
server.xml文件中,修改標(biāo)簽Connector,在最后加入如下Connector標(biāo)簽最后加入的屬性
???? <Connector port="8090"
?????????????? maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
?????????????? enableLookups="false" redirectPort="8443" acceptCount="100"
?????????????? debug="0" connectionTimeout="20000"
?????????????? disableUploadTimeout="true" URIEncoding='GB2312'/>
???? <Connector className="org.apache.coyote.tomcat5.CoyoteConnector"
?????????????? port="8009" minProcessors="5" maxProcessors="75"
?????????????? enableLookups="true" redirectPort="8443"
?????????????? acceptCount="10" debug="0" connectionTimeout="0"
?????????????? useURIValidationHack="false" protocol="AJP/1.3"
?????????????? protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"
?????????????? URIEncoding='GB2312'/>
-
web.xml文件的設(shè)置
默認(rèn)
(
歡迎)文件的設(shè)置
在
h:\tomcat4\conf\web.xml中,<welcome-file-list>與IIS中的默認(rèn)文件意思相同。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
報(bào)錯(cuò)文件的設(shè)置
<error-page>
?<error-code>404</error-code>
<location>/notFileFound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
?<location>/null.jsp</location>
</error-page>
如果某文件資源沒(méi)有找到,服務(wù)器要報(bào)404錯(cuò)誤,按上述配置則會(huì)調(diào)用H:\tomcat4\webapps\ROOT\notFileFound.jsp。
如果執(zhí)行的某個(gè)JSP文件產(chǎn)生NullPointException ,則會(huì)調(diào)用H:\tomcat4\webapps\ROOT\null.jsp
典型的JSP錯(cuò)誤頁(yè)面應(yīng)該這樣寫(xiě):
<%@ page isErrorPage=”true”%>
出錯(cuò)了:(</p> 錯(cuò)誤信息: <%= exception.getMessage() %><br>
Stack Trace is : <pre><font color="red">
<%
java.io.CharArrayWriter cw = new java.io.CharArrayWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(cw,true);
exception.printStackTrace(pw);
out.println(cw.toString());
%>
</font></pre>
會(huì)話超時(shí)的設(shè)置
設(shè)置session 的過(guò)期時(shí)間,單位是分鐘;
<session-config> <session-timeout>30</session-timeout> </session-config>
過(guò)濾器的設(shè)置
? <filter>
? <filter-name>FilterSource</filter-name>
? <filter-class>project4. FilterSource </filter-class>
</filter>
?<filter-mapping>
?<filter-name>FilterSource</filter-name>
<url-pattern>/WwwServlet</url-pattern>
?(<url-pattern>/haha/*</url-pattern>)
</filter-mapping>
</filter>
過(guò)濾:
1) 身份驗(yàn)證的過(guò)濾Authentication Filters
2) 日志和審核的過(guò)濾Logging and Auditing Filters
3) 圖片轉(zhuǎn)化的過(guò)濾Image conversion Filters
4) 數(shù)據(jù)壓縮的過(guò)濾Data compression Filters
5) 加密過(guò)濾Encryption Filters
6) Tokenizing Filters
7) 資源訪問(wèn)事件觸發(fā)的過(guò)濾Filters that trigger resource access events
8) XSL/T 過(guò)濾XSL/T filters
9) 內(nèi)容類型的過(guò)濾Mime-type chain Filter 注意監(jiān)聽(tīng)器的順序,如:先安全過(guò)濾,然后資源,然后內(nèi)容類型等,這個(gè)順序可以自己定,但最好要合理。
監(jiān)聽(tīng)器的設(shè)置
<listener>
<listener-class>project4.SALListenerServlet</listener-class>
</listener>
監(jiān)聽(tīng)器分四種,分別是:
ServletContextListener :對(duì)上下文(全局)對(duì)象的創(chuàng)建和銷毀進(jìn)行監(jiān)聽(tīng)
ServletContextAttributeListener:對(duì)上下文對(duì)象某一屬性的增加、替換、刪除進(jìn)行監(jiān)聽(tīng)
HttpSessionListener:對(duì)Session的創(chuàng)建和銷毀進(jìn)行監(jiān)聽(tīng)
HttpSessionAttributeListener:對(duì)Session某一屬性的增加、替換、刪除進(jìn)行監(jiān)聽(tīng)
Servlet
的設(shè)置
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>project4.HelloServlet</servlet-class>
<init-param>
<param-name>age</param-name>
<param-value>26</param-value>
</init-param>
<init-param>
<param-name>ip</param-name>
<param-value>192.168.5.65</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
解釋:
<servlet>內(nèi)的<servlet-name> 是一個(gè)邏輯名,可以是任何有效的標(biāo)識(shí)名,可以將上述配置中的兩個(gè)<servlet-name>HelloServlet</servlet-name>同時(shí)改成<servlet-name>qq</servlet-name>,得到的效果相同,注意要"同時(shí)改",這樣可以繼續(xù)保持對(duì)應(yīng)的關(guān)系。
<init-param>是Servlet初始參數(shù),在Servlet 的init()方法中通過(guò)getInitParameter("ip")取得,返回String型數(shù)據(jù),
<servlet-mapping>內(nèi)的<servlet-name>與<servlet>內(nèi)的<servlet-name>一一對(duì)應(yīng),把客戶端對(duì)/HeeloServlet的請(qǐng)求對(duì)應(yīng)到<servlet-class>project4.HelloServlet</servlet-class>所指定的位置。
<url-pattern>/HelloServlet</url-pattern>指在IE url中的請(qǐng)求形式。這里的/ 是相對(duì)于當(dāng)前的web目錄的,如H:\tomcat4\webapps\myweb
web.xml文件中安全性的設(shè)置
<
security-constraint>
?<display-name>test</display-name>
?<web-resource-collection>
<web-resource-name>Success</web-resource-name>
<url-pattern>/HelloServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
?</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
?</auth-constraint>
<user-data-constraint>
?<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
?<realm-name>China of Beijing : ) </realm-name>
</login-config>
說(shuō)明:
<display-name>和<web-resource-name>可以是任意,但最好起一個(gè)有意義的名。<auth-constraint>中<role-name>約束了只有哪些角色可以訪問(wèn)由<url-pattern>指定的資源。
<http-method>對(duì)指定方法的訪問(wèn)進(jìn)行限定,未指出的不進(jìn)行限定。
<transport-guarantee>指明對(duì)轉(zhuǎn)輸?shù)臄?shù)據(jù)的要求,有三個(gè)可選值:NONE,INTEGRAL,CONFIDENTIAL?
?
<login-config>: 內(nèi)說(shuō)明的是以何種方式進(jìn)行身份驗(yàn)證,有三種可選值: None,Digest,Client-Cert,Basic,Form。
<realm-name>:區(qū)域名,可以是你想給客戶看的提示信息。
另一種驗(yàn)證方式:
<login-config>
<auth-method>FORM</auth-method>
?<form-login-config>
?<form-login-page>/login.htm</form-login-page>
<form-error-page>/loginError.htm</form-error-page>
</form-login-config>
</login-config>
login.htm文件:
<form method="POST" action="j_security_check" >
用戶名:<input type="text" name="j_username"> </p>
密 碼:<input type="password" name="j_password"> </p>
<input type="submit" value="讓我進(jìn)去吧!">
</form>
其中紅字部分不能更改,并且區(qū)分大小寫(xiě)
-
tomcat-users.xml 設(shè)置
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
?<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
<user name="wang" password="tomcat" roles="admin" />
</tomcat-users>
定義用戶名和用戶所屬的角色,在安全性訪問(wèn)中起作用,如Basic,F(xiàn)orm等加密方式。
server.xml
<Context path="/icq" docBase="myweb" debug="0" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_icq_log." suffix=".txt" timestamp="true"/>
</Context>
Logger 段,為icq這個(gè)應(yīng)用目錄建立一個(gè)日志文件;
Prefix 是日志文件的前綴; suffix 是日志文件的后綴; myweb說(shuō)明其相對(duì)webapps的位置,是物理存在的目錄;
/icq說(shuō)明其相對(duì)web URL的路徑,是一個(gè)虛擬的路徑,如:http://localhost/icq reloadable在開(kāi)發(fā)時(shí)比較有用,指自動(dòng)載入新的Servlet類。
<Connector className="org.apache.catalina.connector.http.HttpConnector" port="8080" minProcessors="5" maxProcessors="75" ... ... 大概在server.xml中的第55、56行,配置服務(wù)器的端口。
-
配置日志
日志文件有四個(gè),在jakarta-tomcat-4.0.1\logs目錄下,
catalina_log在125行catalina_log.2002-06-20.txt 記錄了tomcat服務(wù)器啟動(dòng)的相關(guān)信息
localhost_access_log在180行: localhost_access_log.2002-06-20.txt 用來(lái)記錄客戶端訪問(wèn)了哪些資源,格式如: 172.28.11.91 - - [20/Jun/2002:13:29:09 8000] "GET /web/WwwServlet HTTP/1.1" 500 149 200 149 是服務(wù)器反應(yīng)的狀態(tài)碼
localhost_examples_log localhost_examples_log.2002-06-20.txt在213行這個(gè)是examples的日志文件
localhost_log localhost_log.2002-06-20.txt 在190行?
?
這里記錄了服務(wù)器所運(yùn)行的程序的詳細(xì)信息。包括錯(cuò)誤信息,調(diào)試信息等。用如下寫(xiě)法向這個(gè)日志文件寫(xiě)入調(diào)試信息在Servlet中用getServletContext().log("Servlet 中報(bào)的錯(cuò)!"); 或在JSP中用application.log("哈哈,出錯(cuò)了:("); (ServletContext同application是一回事)
posted @ 2006-09-05 10:09 蕭秋水 閱讀(473) | 評(píng)論 (0) | 編輯 收藏