最近老是被配置問題困擾,java配置麻煩這一點現在是深有體會。為不重蹈覆輒,這裡對遇到的配置方面的問題和相應的解決辦法作一總結。
1、context設定的問題
錯誤描述:找不到context。
錯誤分析:通常有如下幾類:
1)eclipse會在server.xml或/conf/catalina/{host}/${appname.xml}檔案中進行設定,如果eclipse在${appname.xml}中進行了動態設定,最好不要自己再到server.xml中去手工添加了。
2)在當前tomcat的webapps目錄下的所有應用,都不要再去畫蛇添足地在server.xml中手工添加,不然反而會出錯;
3)最好使用eclipse的tomcat外掛程式,直接使用server可能會出現莫名其妙的問題。
2、web.xml的問題
錯誤描述:找不到web.xml。可那玩意青天白日地放在web-inf目錄下。。。
錯誤分析:
1)第一種:eclipse的一個bug(http://shingwa.spaces.live.com/blog/cns!D90C8EAF1F36B3F9!407.entry):
2006-4-9 10:17:23 org.apache.catalina.startup.ContextConfig applicationWebConfig
資訊: Missing application web.xml, using defaults only StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
原來其實是Eclipse的一個Bug而已:Bugzilla Bug 110954
解決方案是:
I'll fix this in the 1.0 release. For now the error the error may be ignored. Or, in the mean time, you can add a web.xml with the contents "<web-app></web-app>" to the ".metadata/.plugins/org.eclipse.wst.server.core/tmp?/webapps/ROOT/WEB-INF" folder in your workspace. Replace the "?" in "tmp?" with the appropriate number. This is enough to make the error go away.
作者的解釋是這樣的:
To state the issue more clearly:
Until fixed, Tomcat 4.x and 5.x servers with "Run modules directly from the workspace..." checked will always output a line like the following into the console log:
INFO: Missing application web.xml, using defaults only StandardEngine[Catalina].StandardHost[localhost].StandardContext[]
Note there is no text within the "[]" that follows StandardContext. This means that it applies to the default context, i.e. the one named ROOT and thus whose context path is "". For this to apply to a web application added to the server, the context path for the web application would appear within the "[]".
I would guess that your error message has "StandardContext[]" and is not a symptom of your webapp breaking, even though it may be the only problem you see present in the console log. You may need check the ".metadata/.plugins/org.eclipse.wst.server.core/tmp?/logs" directory to see if there are other problems listed in log files there.
2)第二種:web.xml是手工建立的,大小寫有問題。
3)第三種:哈哈,還是context配置出問題了。。。這時tomcat會跳過web.xml;
4)第四種:servlet的版本問題:
Servlet2.4與Servlet2.2之間版本的問題
在web.xml第二行,Servlet2.4這樣寫的:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
Servlet2.2是這樣寫的:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
5)第五種:用spring時,如果採用listener,則可能報錯,並且錯誤資訊提示不明確,如下:
- 2006-12-31 9:59:25 org.apache.catalina.core.StandardContext start
- 嚴重: Error listenerStart
換用servlet,一切OK,配置如下:
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
所以最好採用servlet。
3、其它問題
tomcat和jdk版本的問題。最好在tomcat5.5下使用jdk1.5。否則可能會發生莫名其妙的問題。
4、弄懂eclipse的workspace設定檔.metadata,工程下面的.project、.classpath、.tomcatplugin等,對解決問題很有協助。