java - 运行 servlet 显示错误

标签 java servlets http-status-code-404

我在 eclipse 看到教程的 java 文件中编写了以下代码。当我尝试在服务器上运行它时,它向我显示

Http: 404 error
Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

我的代码是

package com.shaby.newservletdemo;

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

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class servletInterface implements Servlet{
    ServletConfig servletConfig= null;
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("Servlet Destroyed.");
    }

    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return servletConfig;
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return "Version 1. 2016-2019";
    }

    @Override
    public void init(ServletConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        this.servletConfig= arg0;
        System.out.println("Servlet Initialized");
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        // TODO Auto-generated method stub
        arg1.setContentType("text/html");
        PrintWriter pw= arg1.getWriter();

        pw.println("<html><body>");
        pw.println("Hello Service has been done!!");
        pw.println("</body></html>");
    }

}

执行部分有什么问题吗?还是我遗漏了什么?? 我在 Eclipse IDE 上运行它。 我正在使用 Tomcat 9 服务器。

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">
  <display-name>servletsdemo</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>
  <servlet>
        <servlet-name>servletInterface</servlet-name>
        <servlet-class>servletInterface</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>servletInterface</servlet-name>
        <url-pattern>/servletInterface</url-pattern>
    </servlet-mapping>
</web-app>

最佳答案

您的 web.xml 配置了吗?

应该有类似的内容

<servlet-mapping>
   <servlet-name>servletInterface</servlet-name>
   <url-pattern>/servletInterface</url-pattern>
</servlet-mapping>

在里面。

此外,您可能应该扩展 HttpServlet 而不是 Servlet。

关于java - 运行 servlet 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43061147/

相关文章:

java - logback中不同日志级别的不同日志文件

http-status-code-404 - Mechanize/Ruby读取404页面源代码

java - 仅当从 servlet 访问时,带有日语的自定义 404 错误页面在 Tomcat 5 上变得不可读

java - 处理数组中的随机数组

java - WatchService 在集成测试中不起作用

Java 到 Kotlin_Gson.Json 转换错误

html - Servlet返回“HTTP状态404请求的资源(/Servlet)不可用”

Java Servlet JSP使用apache commons FileUpload删除文件

java - ide 和 java 应用服务器

java - HttpSession 有什么问题?