我跟踪了Java的Servlet教程,一遍又一遍地发现了相同的问题。我可以在Tomcat 8.5.47中提供一个页面,但是,例如,当我尝试将Servlet绑定到表单动作中时,执行该消息时会出现以下错误:消息错误,实例化Servlet类[xxx.xxx.SearchServlet]
这是一个“动态Web项目”。我检查了项目属性,并且Source src部分指向Deploy Path WEB-INF / classes。另外,所有目录都包含在“构建路径”中。
以下是主要文件:
src / xxx.xxx / SearchServlet.java
package xxx.xxx;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
String str = req.getParameter("str_search");
PrintWriter out = res.getWriter();
out.println(str);
}
}
WebContent / WEB-INF / web.xml
<?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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>search</servlet-name>
<servlet-class>SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
</web-app>
这是完整的报告:
HTTP状态500 –内部服务器错误类型异常报告实例化servlet类[SearchServlet]时发生消息错误描述服务器遇到意外情况,阻止其满足请求。例外javax.servlet.ServletException:实例化servlet类[SearchServlet]时出错org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:810)org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:748)根本原因java.lang.ClassNotFoundException:SearchServletorg.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1360)org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1182)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:810)org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:748)注意在服务器日志中可以找到根本原因的完整堆栈跟踪。Apache Tomcat / 8.5.47
我想知道这是否与Eclipse Installation的权限有关,或者是否与项目结构有关?有人有小费吗?也许我应该寻找一个容器映像或尝试在其上构建的东西?谢谢。
只需在<servlet-class>
标记中添加类的完全限定名称。
示例:
<servlet-class>com.stack.SearchServlet</servlet-class>
或者,如果您愿意:
package com.stack;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @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);
}
}