最近老是被配置问题困扰,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等,对解决问题很有帮助。