今天用eclipse跑一个web统计图的jsp文件,遇到了
javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:343)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

的问题,各种查找各种试,网上有人说设置变量,有人说把servlet.jar导入这个lib,那个lib我都去试了,还是不行,我另外导入了一个用到servlet的例子,

成功了,所以不应该是我的环境变量设置有例子,找了一个多小时,我在百度上也没看到几个这种问题,个人快绝望了,于是想想就在google上看看吧,

在google上一搜,在eclipse的问答板块找到了一个同样的问题,下面的回复是:

This can happen when you've placed server-specific libraries in the webapp's /WEB-INF/lib or probably JRE/lib. Big chance that you copied Tomcat's /lib/servlet-api.jar into there. You shouldn't do that. This would only lead to collisions in the classpath which leads to this kind of errors and it will make your webapp unportable (i.e. it is tied to run on Tomcat only, you can't run it at another servers like Glassfish, JBoss AS, Websphere, etc). You should keep the server-specific libraries at their default location. Cleanup the /WEB-INF/lib from any server-specific libraries and cleanup JRE/libfrom any 3rd party libraries.

You probably copied server-specific libraries there because you wasn't able to compile your servlets. Copying the libraries in /WEB-INF/lib is the wrong solution. You should basically just specify those libraries in the compiletime classpath. Since you're using Eclipse, this can be done easily: first add Tomcat in Servers view, then associate your webapp project with the integrated Tomcat instance. This way Eclipse will automatically add the server-specific libraries to the project's buildpath. On a brand new web project you can choose the server during project creation wizard. On existing web projects, you can modify it in Targeted Runtimes section in project's properties.

哎,离了百度就是鸟文了,但是也得看啊,我大概看了一下,说的是“你可能放置server-specific库在webapp的 /WEB-INF/lib 或者JRE/lib,  很有可能是你将servlet-api.jar拷进去了,你不能这么做,这样会导致冲突,有冲突当然会有错误”解决办法是,你应该保持你的server-specific库中的内容在他们的正确位置,,清理掉你拷进去的库文件,并且清理掉jre/lib中的所有第三方库文件,我这才想起我下午跑一个其它工程的时候也是将jar文件一通乱加,终于跑通了,没想到不是不报,只是时候未到,我赶紧将下午加进来的那些jar包没用到的都去掉,并且删除了几个路径下的servlet-api.jar文件,再一跑,奇迹般的通了!

这篇文章只适合和我一样问题的朋友,如果你真的是缺少jar包,那么你该加还是应该加上,环境变量该设还是应该设置,如果你已经做了上面几条,再来尝试我的这种方法。还有一点就是在平时将你的jre库文件打包备份,如果下次需要纯净的jre环境的时候好直接拿出来用,平时百度不能解决的问题也许google一下就能有欣喜的收获,毕竟这些东西是老外的玩意。


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐