I am using Eclipse for Java EE, Mars 2. I wrote a servlet application first using Java 8 and Apache Tomcat 8.0.x in this IDE and that runs just fine.
Now, I am trying to port the code to Kotlin. But the Apache Tomcat server, it seems like from the information posted below, is unable to locate and load my class LoginServlet
.
I've ported the helper classes and just the one servlet class just now named LoginServlet
. I am removing all the rest of the code in this question just to present a bare bones skeleton.
Here is my set up:
package bookyard.server;
// import statements ommitted for brevity
open class LoginServlet : HttpServlet() {
override fun doGet(request : HttpServletRequest, response : HttpServletResponse) {
val msg: String = "HTTP GET method not supported.";
try {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} catch (e: IOException) {
e.printStackTrace();
}
}
override fun doPost(request : HttpServletRequest, response : HttpServletResponse) {
this.doPostInternal(request, response);
}
private fun doPostInternal(request: HttpServletRequest, response: HttpServletResponse) {
...
}
}
I first annotated the LoginServlet
class with the @WebServlet("/login")
annotation but that ran the server just fine but gave me a 404 for the path /login and even for the ServletContext
path http://localhost:8080/BookyardServer/.
So, I moved the servlet configuration to the WEB-INF\web.xml
file and it now looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>BookyardServer</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>bookyard.server.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
My project Build Path includes references to all the libraries my code uses. A snapshot of it is provided here.
In my Project Facets and the Targeted Runtimes, I have a reference to the Apache Tomcat 8.0 runtime.
However, when I Debug As -> Debug on Server, the browser reports the following error:
HTTP Status 500 - Error instantiating servlet class bookyard.server.LoginServlet
message Error instantiating servlet class bookyard.server.LoginServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception javax.servlet.ServletException: Error instantiating servlet class bookyard.server.LoginServlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Unknown Source)
root cause java.lang.ClassNotFoundException: bookyard.server.LoginServlet org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332) org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Unknown Source)
I am pretty sure this has something to do with the %CLASSPATH% but I am not sure what. I have set the Apache Tomcat jar files in the class path. Everything else that my code uses is also in the class path.
I am not sure why it isn't able to load my servlet class. Could you please help?