2

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.

kvorobiev
  • 5,012
  • 4
  • 29
  • 35
Naseer Ahammed
  • 89
  • 1
  • 2
  • 15
  • Isn't this line ``, should be ``. Since otherwise there is no point in creating a `url-pattern` for the said Servlet, if you want users of the page to see the directory structure of the Servlet concerned. Moreover, you using annotations, along with web.xml, which leads to more confusion :-). Use anyone, but not both :-) – nIcE cOw Mar 26 '16 at 05:46
  • Its redirecting the following url and same error http://localhost:8080/AdvanceJavaPractice/servlet/com.advancejava.ServletDemo – Naseer Ahammed Mar 26 '16 at 05:56
  • Sorry, replace it with `Something`, do watch `href` is without the first forward slash. Now when you will open `index.html` like say `localhost:8080/index.html`, now on clicking `Something`, the address on the browser will be `localhost:8080/ServletDeo` – nIcE cOw Mar 26 '16 at 06:08
  • No change in error – Naseer Ahammed Mar 26 '16 at 06:22
  • The `index.html` file should be inside `AdvanceJavaPractice` directory ( containing WEB-INF folder as well ). Otherwise, the path you specifying inside the `servlet-class` tag is wrong. Do check the address on the browser, once you send a Get request, and try to match that with the directory structure of the website, and make changes likewise in .html file as well – nIcE cOw Mar 26 '16 at 06:29
  • The index.html file is in WEB-INF folder as it should be placed in that directory. No change in error – Naseer Ahammed Mar 26 '16 at 06:39

2 Answers2

2

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>
Duncan
  • 696
  • 1
  • 5
  • 15
0

Previous response: The index page should be "my index.html" or "index.html". It needs to go right with what is in the servlet.

index.html

Posting it twice really is the problem. I'm glad this has been resolved. it's helpful to me as well. Thanks