javax.servlet.ServletException: 实例化 servlet 时出错 class java.UserScoresServlet
javax.servlet.ServletException: Error instantiating servlet class java.UserScoresServlet
我一直在研究与此类似的主题,但 none 的解决方案有效。我正在尝试使用 Eclipse 在 Web 应用程序中映射一个简单的 HttpServlet,但是当从调用 servlet 上的 html 页面提交表单时出现以下错误。
HTTP Status 500 - Error instantiating servlet class java.UserScoresServlet
type Exception report
message Error instantiating servlet class java.UserScoresServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class java.UserScoresServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
root cause
java.lang.ClassNotFoundException: java.UserScoresServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
我已经尝试使用@WebServlet 注释和Web.xml 文件进行映射,但均无济于事。我试图使用一个或另一个,而不是同时使用两者。使用 JSP 和 HTML 页面时,我的映射工作正常。我的XML文件映射如下,位于WEB-INF目录下
<display-name>GetUserScores</display-name>
<servlet>
<servlet-name>GetUserScores</servlet-name>
<servlet-class>java.UserScoresServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetUserScores</servlet-name>
<url-pattern>/GetUserScores</url-pattern>
</servlet-mapping>
</web-app>
服务器class如下。正如我上面提到的,我一直在做注释,或者 web.xml,而不是两者。两者都抛出相同的异常。
package java;
import java.io.IOException;
import java.score.jpa.Score;
import java.score.jpa.ScoreService;
import java.user.jpa.User;
import java.user.jpa.UserService;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserScoresServlet
*/
@WebServlet(name = "GetUserScores", urlPatterns = "/GetUserScores")
public class UserScoresServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserService userService;
private ScoreService scoreService;
/**
* @see HttpServlet#HttpServlet()
*/
public UserScoresServlet() {
super();
userService = new UserService();
scoreService = new ScoreService();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = (String)request.getParameter("firstName");
String lastName = (String)request.getParameter("firstName");
User user = userService.findUserByFirstAndLastName(firstName, lastName);
if (user != null){
List<Score> scoreList = scoreService.findAllScoresByUser(user);
request.setAttribute("scoreList", scoreList);
}
gotoPage("/scores", request, response);
}
private void gotoPage(String address, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
文件结构见照片:File Structure Image
任何人都可以提供任何帮助,这将是一个很大的帮助。我彻底搜索了这个网站并尝试了所有类似问题的建议,但都无济于事。感谢您的帮助。
如 JLS 中所述,您不应使用 java
作为开发包的名称:
Names of packages intended only for local use should have a first identifier that begins with a lowercase letter, but that first identifier specifically should not be the identifier java
; package names that start with the identifier java
are reserved for package of the Java SE platform
在您的代码中注明:
package java;
import java.io.IOException;
import java.score.jpa.Score;
import java.score.jpa.ScoreService;
import java.user.jpa.User;
import java.user.jpa.UserService;
import java.util.List;
//rest of code...
将您的包裹名称更改为其他名称。
我一直在研究与此类似的主题,但 none 的解决方案有效。我正在尝试使用 Eclipse 在 Web 应用程序中映射一个简单的 HttpServlet,但是当从调用 servlet 上的 html 页面提交表单时出现以下错误。
HTTP Status 500 - Error instantiating servlet class java.UserScoresServlet
type Exception report
message Error instantiating servlet class java.UserScoresServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class java.UserScoresServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
root cause
java.lang.ClassNotFoundException: java.UserScoresServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
我已经尝试使用@WebServlet 注释和Web.xml 文件进行映射,但均无济于事。我试图使用一个或另一个,而不是同时使用两者。使用 JSP 和 HTML 页面时,我的映射工作正常。我的XML文件映射如下,位于WEB-INF目录下
<display-name>GetUserScores</display-name>
<servlet>
<servlet-name>GetUserScores</servlet-name>
<servlet-class>java.UserScoresServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetUserScores</servlet-name>
<url-pattern>/GetUserScores</url-pattern>
</servlet-mapping>
</web-app>
服务器class如下。正如我上面提到的,我一直在做注释,或者 web.xml,而不是两者。两者都抛出相同的异常。
package java;
import java.io.IOException;
import java.score.jpa.Score;
import java.score.jpa.ScoreService;
import java.user.jpa.User;
import java.user.jpa.UserService;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserScoresServlet
*/
@WebServlet(name = "GetUserScores", urlPatterns = "/GetUserScores")
public class UserScoresServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserService userService;
private ScoreService scoreService;
/**
* @see HttpServlet#HttpServlet()
*/
public UserScoresServlet() {
super();
userService = new UserService();
scoreService = new ScoreService();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = (String)request.getParameter("firstName");
String lastName = (String)request.getParameter("firstName");
User user = userService.findUserByFirstAndLastName(firstName, lastName);
if (user != null){
List<Score> scoreList = scoreService.findAllScoresByUser(user);
request.setAttribute("scoreList", scoreList);
}
gotoPage("/scores", request, response);
}
private void gotoPage(String address, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
文件结构见照片:File Structure Image
任何人都可以提供任何帮助,这将是一个很大的帮助。我彻底搜索了这个网站并尝试了所有类似问题的建议,但都无济于事。感谢您的帮助。
如 JLS 中所述,您不应使用 java
作为开发包的名称:
Names of packages intended only for local use should have a first identifier that begins with a lowercase letter, but that first identifier specifically should not be the identifier
java
; package names that start with the identifierjava
are reserved for package of the Java SE platform
在您的代码中注明:
package java;
import java.io.IOException;
import java.score.jpa.Score;
import java.score.jpa.ScoreService;
import java.user.jpa.User;
import java.user.jpa.UserService;
import java.util.List;
//rest of code...
将您的包裹名称更改为其他名称。