每当我尝试访问我的servlet时,Tomcat都会抛出异常java.lang.ClassNotFoundException:com.web.servlets.SearchServiceServlet。 servlet采用正确的包结构:
我在web.xml中定义了我的servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>PetClinic</display-name>
<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>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<servlet>
<servlet-name>searchService</servlet-name>
<servlet-class>com.web.servlets.SearchServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>searchService</servlet-name>
<url-pattern>/searchServ</url-pattern>
</servlet-mapping>
</web-app>
servlet类:
public class SearchServiceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SearchServiceServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
int age = Integer.valueOf( request.getParameter("age") );
String pet_name = request.getParameter("pet_Name");
String pet_type = request.getParameter("pet_type");
int rating = Integer.valueOf(request.getParameter("rating"));
int experience = Integer.valueOf(request.getParameter("experience"));
System.out.println("First name = "+first_name+",last name = "+last_name+",age = "+age+",pet's name = "+pet_name+",pet's type = "+pet_type+",rating = "+rating+",experience = "+experience);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
是不是eclipse没有正确生成类?如果是,我该怎么测试呢?因为一个简单的POJO与main方法工作正常。
此外,这里是完整的异常以及我启动应用程序时tomcat打印的内容:
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.17
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Mar 13 2019 15:55:27 UTC
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.17.0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_181\jre
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_181-b13
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\Barracuda\Desktop\Projects\Eclipse Projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 9.0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\Barracuda\Desktop\Projects\Eclipse Projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\Barracuda\Desktop\Projects\Eclipse Projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 9.0\endorsed
апр 11, 2019 12:15:21 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1251
апр 11, 2019 12:15:21 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: An older version [1.2.17] of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.21]
апр 11, 2019 12:15:21 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library [1.2.17] using APR version [1.6.3].
апр 11, 2019 12:15:21 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
апр 11, 2019 12:15:21 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
апр 11, 2019 12:15:22 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.0.2o 27 Mar 2018]
апр 11, 2019 12:15:22 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
апр 11, 2019 12:15:23 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
апр 11, 2019 12:15:23 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [2 666] milliseconds
апр 11, 2019 12:15:23 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
апр 11, 2019 12:15:23 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.17]
апр 11, 2019 12:15:23 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
апр 11, 2019 12:15:24 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
апр 11, 2019 12:15:24 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
апр 11, 2019 12:15:24 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
апр 11, 2019 12:15:24 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [987] milliseconds
апр 11, 2019 12:15:28 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet [searchService] as unavailable
апр 11, 2019 12:15:28 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [searchService]
java.lang.ClassNotFoundException: com.web.servlets.SearchServiceServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1363)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1186)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:540)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:521)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:761)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
P.S该类位于正确的包中:package com.web.servlets;
尝试在类文件中指定包。
package com.web.servlets;
java.lang.ClassNotFoundException
当ClassLoader找不到指定的类时抛出异常。
你有:
<servlet-class>com.web.servlets.SearchServiceServlet</servlet-class>
这意味着你有一个包:com.web.servlets
所在的SearchServiceServlet
类。
如果您有一个Maven项目,您应该有scr/main/java/%yourpackagename%/yourClass
如果它是一个简单的Web项目,那么它应该像src/yourpackagename/yourClass
。
日食重启后问题得到解决......虽然还有另一个问题。每当我保存servlets类时,在更改它之后,它都不会看到新类,而是使用它的先前版本。这里的问题是什么?