简体   繁体   English

运行servlet显示错误

[英]Running servlet shows error

I wrote following codes in the java file in eclipse seeing a tutorial. 我在eclipse中的java文件中编写了以下代码,并查看了教程。 When I try to run this on the server it shows me 当我尝试在服务器上运行它时,它向我显示

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. 我正在Eclipse IDE上运行它。 I am using Tomcat 9 server. 我正在使用Tomcat 9服务器。

web.xml 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>

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。

404 status code means the server is not able to find the requested resource. 404状态码表示服务器无法找到请求的资源。 Please check if you have mapped the request uri to the servlet and included the servlet and classname in web.xml correctly. 请检查您是否已将请求uri映射到servlet并将servlet和类名正确包含在web.xml中。

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>

See the example: Servlet Tutorials 请参见示例: Servlet教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM