Reputation: 11
I am making a simple web app using Apache Tomcat version:8.0.36. I am getting the "Class foo.Counter is not a Servlet" error. I have extended HttpServlet in the Servlet Class and configured the web.xml. I am clicking on the 'extreme' option repeatedly.
package foo;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Counter extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String c=req.getParameter("hobby");
Choices m=new Choices();
ArrayList l=m.getNames(c);
req.setAttribute("name", l);
RequestDispatcher view=req.getRequestDispatcher("BasicCounter.jsp");
view.forward(req, resp);
}
}
And the web.xml file is as follows:
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Counter</servlet-name>
<servlet-class>foo.Counter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Counter</servlet-name>
<url-pattern>/HobbyParameter.do</url-pattern>
</servlet-mapping>
</web-app>
HTML file as (names.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="POST" action="HobbyParameter.do">
Choose a Hobby:<br>
<select name="hobby" size="1">
<option>horse skiing
<option>extreme
<option>alpine scuba
<option>speed dating
</select>
<br>
<input type="SUBMIT" >
</form>
</body>
</html>
And JSP as (BasicCounter.jsp):
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
The friends who share your hobby of <%= request.getAttribute("hobby") %> are:<br>
<%
ArrayList list= (ArrayList) request.getAttribute("name");
Iterator it=list.iterator();
while(it.hasNext())
{
out.print("<br>"+it.next());
}
%>
</body>
</html>
And the error is as follows:
SEVERE: Allocate exception for servlet Counter java.lang.ClassCastException: foo.Counter cannot be cast to javax.servlet.Servlet at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
Upvotes: 1
Views: 508
Reputation: 3002
Looks like you have assembled servlet.jar to your war.
If you use maven then you should use scope provided
for servlet api like
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Make sure that you don't have any servlet jar dependencies in Tomcat server except its own.
Upvotes: 0
Reputation: 3512
Look for instances of javax.servlet.ServletException.class in your WEB-INF/lib. Contents for servler.jar or servlet.api.jar should be provided bye the container. so you need to put servler.jar into WEB-INF/lib folder and add jar into your project.
Upvotes: 0