运行servlet显示错误 [英] Running servlet shows error

查看:68
本文介绍了运行servlet显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在eclipse中的Java文件中编写了以下代码,并查看了教程.当我尝试在服务器上运行它时,会显示

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

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服务器.

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.

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?

Do you have your web.xml configured?

应该有类似的东西

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

在其中.

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

Also, you should probably extend HttpServlet instead of Servlet.

这篇关于运行servlet显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆