我正在使用Apache Tomcat版本制作一个简单的Web应用程序:8.0.36。 我得到了" Class foo.Counter不是Servlet"错误。 我在Servlet类中扩展了HttpServlet并配置了web.xml。 我点击了极端的'选择重复。
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);
}
}
web.xml文件如下:
<?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文件为(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>
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>
错误如下:
SEVERE:为servlet Counter分配异常 java.lang.ClassCastException:foo.Counter无法强制转换为 javax.servlet.Servlet 在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102) 在 org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 在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) 在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) at 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(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) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
在WEB-INF / lib中查找javax.servlet.ServletException.class的实例。 servler.jar或servlet.api.jar的内容应该由容器提供。所以你需要将servler.jar放入WEB-INF / lib文件夹并将jar添加到你的项目中。
答案 1 :(得分:0)
看起来你已经为你的战争组装了servlet.jar。
如果您使用maven,则应将范围provided
用于servlet api,如
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
确保在Tomcat服务器中没有任何servlet jar依赖项,除了它自己的。