1

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>

3 Answers3

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.

Jure Kolenko
  • 799
  • 5
  • 10
  • this solution is not working. It gives an error and server stops – Shubham Agarwal Bhewanewala Mar 28 '17 at 06:04
  • Provide more information about the error and your server configuration and how you're making the call. – Jure Kolenko Mar 28 '17 at 06:20
  • 1
    needs to be the fully qualified name of your class (as @OTM said, "com.shaby.newservletdemo.servletInterface"), but since that throws the ClassNotFoundException that suggests there is a problem with your packaging. What do your pom.xml files look like? Alternativelly, you could try annotating the class with `@WebServlet` and leave out the web.xml configuration. – Jure Kolenko Mar 28 '17 at 08:34
  • here is one solution found. can you explain http://stackoverflow.com/questions/24641037/cannot-run-my-servlet-error-404?rq=1 – Shubham Agarwal Bhewanewala Mar 28 '17 at 08:38
  • You should accept the answer that gave the solution. – Jure Kolenko Mar 28 '17 at 12:58
1

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.

OTM
  • 656
  • 5
  • 8
  • Please see the example in https://pubs.vmware.com/vfabric52/index.jsp?topic=/com.vmware.vfabric.tc-server.2.8/getting-started/tutwebapp-web-xml-file.html for servlet and servlet-mapping tags. – OTM Mar 28 '17 at 06:09
  • Can you change servlet-class tag value in web.xml to com.shaby.newservletdemo.servletInterface and try ? – OTM Mar 28 '17 at 07:54
  • java.lang.ClassNotFoundException: com.shaby.newservletdemo.ServletInterface – Shubham Agarwal Bhewanewala Mar 28 '17 at 08:30
0

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

Hùng Ng Vi
  • 1,251
  • 2
  • 14
  • 20