Stack Exchange
java - servlet page calls give error 404 Not found
I wrote a servlet page in java to retrieve records from a table in MySQL. Then , I will call the servlet page in JSP page to display the results on the browser.
MyServlet.java : 
@WebServlet(name = "MyServlet")
public class MyServlet extends HttpServlet {

    private Statement statement;




   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   response.setContentType("text/html");

        PrintWriter out=response.getWriter();

 try{
     Class.forName("com.mysql.jdbc.Driver");

     Connection conn = DriverManager.getConnection( "jdbc:mysql://126.32.3.11:3306/test", "root", "root");

     String sql ="Select * from filequeue;";

     statement = conn.createStatement();

     ResultSet resultSet = statement.executeQuery(sql);




String str = "<table border=1><tr><th>UniqueID</th><th>FilePath</th><th>Status</th><th>DateTime</th><th>Error</th></tr>";


     while(resultSet.next()){

       str += "<tr><td>"+ resultSet.getString(1) + "</td><td>" + resultSet.getString(2)+ "</td><td>" +resultSet.getString(3)+ "</td><td>" +resultSet.getString(4)+ "</td><td>" +resultSet.getString(5)+"</td></tr>";
     // display each record

     }
    str+= "</table>";
     out.println(str);
 resultSet.close();
     statement.close();
     conn.close();


 }catch(Exception e){

 System.out.println(e);

 }finally {
     out.close();
 }


    }
}

In my web.xml :
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <servlet>
        <description></description>
        <display-name>Servlet</display-name>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class> MyServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/Servlet</url-pattern>
    </servlet-mapping>


</web-app>

And in my index.jsp :
<body>
  <h1>Simple File Queue table</h1>
<a href="MyServlet">View table</a>


  </body>

However, when I click on "View table" it says Error: 404 Not found. I do not know why as it should display the records in the page. Am I making any mistake here?
Edit:
This is screenshot of my file structure:


Please, refactor your code to be more readable. I understand you tried all kind of things but helping ain't easy if you cant read java properly. thanks

Try remove space in <servlet-class> MyServlet</servlet-class> and better add a package from MyServlet class. Like <servlet-class>org.app.MyServlet</servlet-class>.
<url-pattern>/Servlet</url-pattern> means that URL for this servlet is {hostname:port}/Servlet, not a MyServlet (<a href="MyServlet">View table</a>)
And don't forget about application name. If you project deploy with name 'myapp' (and your index page is {host:port}/myapp) then all your servlets intercept this paths: "{host:port}/myapp/{url-pattern from servlet-mapping}".
Try this in web.xml:
<servlet>
    <description></description>
    <display-name>Servlet</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>

index.jsp:
  <body>
    <h1>Simple File Queue table</h1>
    <a href="/{application-name}/servlet">View table</a>
  </body>


So what needs to be changed?

I removed the space but still same issue

@Daredevil what pakage of your MyServlet (first line of code in file)?

No package, Maybe i attached a screenshot. See edited post

@Daredevil what URL do you use for open index page?

I think i do no have package in src folder

@Daredevil nice URL :) try View table in your index.jsp

should i add package name?

It works now! You should update your answer and i'll mark it

And is mysqlconnector.jar neccessary work this to work?

the reason is "QueueTableWeb_war_exploded". it is your application name. And all servlets deploys by path: "{host:port}/QueueTableWeb_war_exploded/{url-pattern}"

do I need the mysql connector jar as well?

@Daredevil, you need it if you want working with mysql.
