eclipse上的Java Servlet:实例化servlet类的错误ClassNotFoundException的

时间:2017-12-16 11:47:56

标签: java eclipse jsp servlets

我知道这个问题已被问过很多,但我尝试了一切,但它仍然不适合我,希望有人可以提供帮助。

我正在尝试使用eclipse在服务器上运行servlet页面,不断显示此错误:

HTTP Status 500 - Internal Server Error

这是我的源代码:

我写了一个简单的servlet页面,只是为了看它在服务器上运行:

    package main.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 StationsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public StationsServlet() {

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.println("<html>");
        writer.println("<body>");
        writer.println("Hello");
        writer.println("</body></html>");
        writer.flush();
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

我的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"
    version="3.1">
    <display-name>weather-files-war</display-name>
    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>stationsServlet</servlet-name>
        <servlet-class>main.java.servlet.StationsServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>stationsServlet</servlet-name>
        <url-pattern>/stations</url-pattern>
    </servlet-mapping>
</web-app>

最后,我的文件夹/模块结构:

enter image description here

  • PS:我创建了一个homepage.jsp并且它在服务器上运行正常,问题是在点击servlet类时。

2 个答案:

答案 0 :(得分:0)

首先检查您的StationsServlet.java附近的感叹号有什么问题

还尝试添加注释2017-12-16 13:05:16 [scrapy.core.engine] INFO: Spider opened 2017-12-16 13:05:16 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) xxxxxxxxxx [] xxxxxxxxxx [] 2017-12-16 13:05:21 [scrapy.core.engine] INFO: Closing spider (finished) 2017-12-16 13:05:21 [scrapy.statscollectors] INFO: Dumping Scrapy stats: {'downloader/request_bytes': 986, 'downloader/request_count': 1, 'downloader/request_method_count/POST': 1, 'downloader/response_bytes': 163, 'downloader/response_count': 1, 'downloader/response_status_count/200': 1, 'finish_reason': 'finished', 'finish_time': datetime.datetime(2017, 12, 16, 12, 5, 21, 707451), 'log_count/INFO': 7, 'log_count/WARNING': 1, 'response_received_count': 1, 'scheduler/dequeued': 2, 'scheduler/dequeued/memory': 2, 'scheduler/enqueued': 2, 'scheduler/enqueued/memory': 2, 'splash/execute/request_count': 1, 'splash/execute/response_count/200': 1, 'start_time': datetime.datetime(2017, 12, 16, 12, 5, 16, 816927)} 2017-12-16 13:05:21 [scrapy.core.engine] INFO: Spider closed (finished) 在servlet类中@WebServlet(name = "StationsServlet ", value = "/stations")之前。 它需要帮助。

答案 1 :(得分:0)

我创建了一个新项目和一个新工作区,只是将模块和类复制到新项目中,并且可以正常工作。

我想这与Eclipse项目的结构和配置有关。