Servlets don't work with Tomcat - class is not a servlet
Hi,
I'm running Tomcat 5.5.17 in Linux environment and have deployed a webapp in a directory (myapp). I've set servlet mappings and re-checked that they are correct.
When I browse to myapp/index.jsp, it shows up correctly. However, when I continue using the servlet and the servlet tries to access myapp/measurements, I get the following error:
javax.servlet.ServletException: Class org.myapp.measureIt is not a Servlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:754)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:684)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:876)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) java.lang.Thread.run(Thread.java:595)
root cause
java.lang.ClassCastException: org.myapp.measureIt
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:754)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:684)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:876)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
java.lang.Thread.run(Thread.java:595)
the Localhost log prints out the following:
INFO: Marking servlet measurements as unavailable
5.10.2006 9:53:53 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet measurements
5.10.2006 9:53:54 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet HTMLManager
java.lang.NoSuchMethodError: javax.servlet.ServletConfig.getServletName()Ljava/lang/String;
at org.apache.catalina.manager.ManagerServlet.init(ManagerServlet.java:447)
at javax.servlet.GenericServlet.init(GenericServlet.java:172)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:757)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:130)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
The same happens with each and every one of the classes.
When I recompile the java files, it passes without errors, but I get the following warnings:
javax/servlet/http/HttpServlet.java:706: warning: [deprecation] setStatus(int,java.lang.String) in javax.servlet.http.HttpServletResponse has been deprecated
public void setStatus (int sc, String sm)
^
javax/servlet/http/HttpServlet.java:707: warning: [deprecation] setStatus(int,java.lang.String) in javax.servlet.http.HttpServletResponse has been deprecated
{ resp.setStatus (sc, sm); }
Does that have something to do with the errors? my custom servlets extend those javax.servlet classes.
I'd be extremely grateful for any help, am kind of a newbie with both Servlets and Tomcat and have been struggling with this for days now.
[4706 byte] By [
NBarnesa] at [2007-10-3 6:40:04]

The servlet mappings would seem to be in order. Here's the web.xml file located in tomcat5/webapps/myapp/WEB-INF:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>measurements</servlet-name>
<servlet-class>org.myapp.measureIt</servlet-class>
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>measurements</servlet-name>
<url-pattern>/measurements</url-pattern>
</servlet-mapping>
and the class files are located in classes/org/myapp folder Still, myapp/measurements results in the errors above.
In server.xml, the webapps folder seems to be configured appropriately:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
What could be wrong?
Here is the code of measureIt.java
/**
* Class is to receive a request coming from index.jsp and invoke a class which generates the HTML page. The generated page is sent as response. */
package org.myapp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.myapp.DbMeasurements;
public class MeasureIt extends HttpServlet {
String url = "";
public MeasureIt()
{
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
url = rb.getString("url");
}
public void init() throws ServletException
{
super.init();
ServletContext ctx = getServletContext();
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println ("*** AccessServlet got a GET request");
makePage(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println ("*** AccessServlet got a POST request");
makePage(req, resp);
}
private void makePage(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
String begdate;
String enddate;
String part;
String count;
begdate = req.getParameter("begdate");
enddate = req.getParameter("enddate");
part = req.getParameter("part");
count = req.getParameter("count");
String shtml = DbMeasurements.generateHtml(url, begdate, enddate, part, count);
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println(shtml);
out.close();
}
}
Basically, the index.jsp allows access to different pages (measurements data, graphical views etc.), which use many unrelated classes, none of which are found.
To get the data, I'm using mysql-connector-java-3.1.13 to connect to a mySQL database, is this ok with this version of Tomcat? I don't have much knowledge on inner workings of jdbc drivers. The database username/password are passed using LocalStrings.Properties, they are correct in themselves.
Thanks for the help, found the error:
For some reason, the following line in setclasspath.sh was commented:
#CLASSPATH=
uncommented it, and now the classes are found. It didn't occur to me that duplicates in classpath result in this.
....
Although, the servlets still don't actually fetch any data from the mysql database. Catalina.out prints out the following:
*** AccessServlet got a POST request
Exception: com.mysql.jdbc.Driver
null
null
6.10.2006 15:20:00 org.apache.catalina.connector.CoyoteAdapter service
SEVERE: An exception or error occurred in the container during the request processing
java.lang.NoSuchMethodError: javax.servlet.ServletResponse.isCommitted()Z
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:112)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:754)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:684)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:876)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Does anybody know what that is all about?