Java 运行 servlet 显示错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43061147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:39:25  来源:igfitidea点击:

Running servlet shows error

javaservletshttp-status-code-404

提问by Shubham Agarwal Bhewanewala

I wrote following codes in the java file in eclipse seeing a tutorial. When I try to run this on the server it shows me

我在 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

My code is

我的代码是

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>");
    }

}

Is there any problem in the execution part or Am i missing something?? I am running this on Eclipse IDE. I am using Tomcat 9 server.

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

web.xml

网页.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>

采纳答案by Jure Kolenko

Do you have your web.xml configured?

你的 web.xml 配置了吗?

Should have something like

应该有类似的东西

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

in it.

在里面。

Also, you should probably extend HttpServlet instead of Servlet.

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

回答by OTM

404 status code means the server is not able to find the requested resource. Please check if you have mapped the request uri to the servlet and included the servlet and classname in web.xml correctly.

404 状态码表示服务器无法找到请求的资源。请检查您是否已将请求 uri 映射到 servlet 并在 web.xml 中正确包含了 servlet 和类名。

回答by Hùng Ng Vi

I think you should implement abstract class javax.servlet.http.HttpServlet.

我认为你应该实现抽象类javax.servlet.http.HttpServlet

And specific package of the class(if need):

以及类的特定包(如果需要):

<servlet-class>path.to.package.ServletInterface</servlet-class>

<servlet-class>path.to.package.ServletInterface</servlet-class>

See the example: Servlet Tutorials

请参阅示例:Servlet 教程