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>
Do you have your web.xml configured?
Should have something like
in it.
Also, you should probably extend HttpServlet instead of Servlet.
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
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.