【问题标题】:Running servlet shows error运行 servlet 显示错误
【发布时间】:2017-08-21 00:44:18
【问题描述】:

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

【问题讨论】:

    标签: java servlets http-status-code-404


    【解决方案1】:

    你的 web.xml 配置了吗?

    应该有类似的东西

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

    在里面。

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

    【讨论】:

    • 此解决方案不起作用。它给出一个错误并且服务器停止
    • 提供有关错误和您的服务器配置以及您如何拨打电话的更多信息。
    • 需要是您的类的完全限定名称(正如@OTM 所说,“com.shaby.newservletdemo.servletInterface”),但由于这会引发 ClassNotFoundException 表明存在你的包装有问题。你的 pom.xml 文件是什么样的?或者,您可以尝试使用 @WebServlet 注释类并省略 web.xml 配置。
    • 这里找到了一种解决方案。你能解释一下stackoverflow.com/questions/24641037/…
    • 您应该接受给出解决方案的答案。
    【解决方案2】:

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

    【讨论】:

    • 有关 servlet 和 servlet-mapping 标签,请参见pubs.vmware.com/vfabric52/index.jsp?topic=/… 中的示例。
    • 你能把web.xml中的servlet-class标签值改成com.shaby.newservletdemo.servletInterface试试吗?
    • java.lang.ClassNotFoundException: com.shaby.newservletdemo.ServletInterface
    【解决方案3】:

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

    以及类的具体包(如果需要):

    &lt;servlet-class&gt;path.to.package.ServletInterface&lt;/servlet-class&gt;

    查看示例:Servlet Tutorials

    【讨论】:

    • 不,这不是错误。我得到了解决方案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 2019-11-13
    • 2018-10-10
    • 2016-11-22
    • 2021-12-20
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多