javax.servlet.ServletException:实例化servlet类java.UserScoresServlet

时间:2017-11-04 03:57:20

标签: java servlets tomcat7 servlet-3.0

我一直在研究类似的主题,但没有一个解决方案有效。我正在尝试使用Eclipse在Web应用程序中映射一个简单的HttpServlet,但是当从调用get 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>

服务器类如下。正如我上面提到的,我一直在做注释或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

任何人都能提供的任何帮助都将是一个很大的帮助。我彻底搜索了这个网站并尝试了类似问题的每一个建议都无济于事。感谢帮助。

1 个答案:

答案 0 :(得分:0)

JLS中所述,您不应将result_list = [] my_list = ['abc,1234,,ickd,sold', 'yeje,38393,,dkdi,eole', 'euei,38393,,idkd,dikd'] for val in my_list: result_list.append(str(val)+"|") # result_list.append() print ''.join(result_list) 用作开发包的名称:

  

仅供本地使用的软件包名称应该具有以小写字母开头的第一个标识符,但该第一个标识符不应该是标识符java;以标识符java开头的包名称是为Java SE平台的包保留的

在您的代码中注明:

java

将包的名称更改为其他名称。