Naseer Ahammed Naseer Ahammed - 5 months ago 25
HTML Question

Http 404 resource not found

I am new to servlets and jsp. I did a small application on Eclipse JEE version. But when I am executing I am getting 'http 404: resource not found' error even though the Tomcat has been installed properly on Eclipse. Here is the code I am using:

This is my ServletDemo.java

package com.advancejava;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class ServletDemo
*/
@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })

public class ServletDemo extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ServletDemo() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
try(PrintWriter pw = response.getWriter()) {
pw.println("<!DOCTYPE html>");
pw.println("<html>");
pw.println("<head>");
pw.println("<body>");
pw.println("<h1>Servlet Demo"+request.getContextPath()+"</h1>");
pw.println("</body>");
pw.println("</html>");
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
try{
pw.println("<html>");
pw.println("<head>");
pw.println("<title>Servlet</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<p>First DemoServlet</p>");
pw.println("</body>");
pw.println("</html>");
}
finally{
System.out.println("This is my Servlet Page");
}
}


}

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="true" version="3.0">
<display-name>ServletDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletDemo</servlet-name>
<servlet-class>com.advancejava.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo</servlet-name>
<url-pattern>/ServletDemo</url-pattern>
</servlet-mapping></web-app>


my index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Advance java
<a href="com.advancejava.ServletDemo"></a>
</body>
</html>


If there are any mistakes, please forgive me and suggest me how to correct them.

Answer

Since you've already declare your servlet at

@WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })

don't declare twice in your web.xml.


the url to your servlet should be

http://localhost:8080/{contextPath}/ServletDemo

the contextPath will be your projectName by default if you use eclipse


there is one more thing, delete metadata-complete="true" in your web.xml. Otherwise it won't work.

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>ServletDemo</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Comments