Issue
I have a dynamic web project in eclipse. I have created some new packages in the project and moved some servlets around. Now the servlets are not working because the path in the web.xml file is not updated. It is throwing the following exception
Mar 14, 2017 10:45:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet ServletName as unavailable
Mar 14, 2017 10:45:12 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet ServletName
java.lang.ClassNotFoundException: com.package.database.ServletName
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1062)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.jamonapi.http.JAMonTomcatValve.invoke(JAMonTomcatValve.java:67)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
The exceptions says it all, the <servlet-class>
in the web.xml file for the servlet is not correct. If I update the path, it will work but there are quite few servlets I have moved around.
Is is possible to automatically update the whole web.xml file to fix all the <servlet-class>
?
Solution
I don't think eclipse can automatically update all the paths but if you are using Tomcat 7.0 then you have a workaround as described here
Tomcat 7 is a Servlet 3.0 compatible container. Since Servlet 3.0, the servlets can be configured by @WebServlet annotation on the class without the need for a web.xml configuration entry. Look closer at the servlet class you just created, there's a @WebServlet annotation on it containing all information you specified in New Servlet wizard.
Effectively, this new way of configuring servlets
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {}
does exactly the same as this legacy way of configuring servlets
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
You will have to change the Dynamic Web Module version to 3.0 in Project Facets section of project's properties if it less than 3.0.
Answered By - AL̲̳I
Answer Checked By - Marie Seifert (JavaFixing Admin)