Running servlet shows error

↘锁芯ラ 提交于 2019-12-11 07:28:03

问题


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

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.

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>

回答1:


Do you have your web.xml configured?

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.




回答2:


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.




回答3:


I think you should implement abstract class 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



来源:https://stackoverflow.com/questions/43061147/running-servlet-shows-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!