2

I think I've read every question on this topic so far and I really tried a lot of solutions, so sorry if I overlooked anything. I'm using Eclipse with Tomcat 8. Tomcat is already configured as server and the MySQL-connector....jar is in the WEB-INF/lib folder, web.xml just in the /WEB-INF one and the index just in /WebContent

index.html:

<!DOCTYPE html>
<html>
<form action="Servlets/Start" method="post">
<font face="verdana" size="2">
 Enter Table Name :<input type="text" name="table"> 
                   <input type="submit" value="Display">
  </font>
</form>


</html

start.java:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 * Servlet implementation class Start
 */
@WebServlet("/Start")
public class Start extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Start() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
PrintWriter pw=respond.getWriter(); 
respond.SetContentType("text/html");
String tb=request.getParameter("table");
try
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc::oracle::thin:@localhost:music","root","1234");    
    Statement st=con.createStatement();
    System.out.println("connection established successfully!");
    ResultSet rs=st.executeQuery("SELECT * FROM"+tb);

    pw.println("<table border=1>");
        while(rs.next())
        {
            pw.println("<tr><td>"+rs.getInt(1)+"</td></td>+rs.getString(2)+</td>"+"<td>"+rs.getString(3)+"</td></tr>");
        }
    pw.println("</table>");
    pw.close();
}
catch (Exception e){
e.printStackTrace();
}
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

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" version="3.1">
<display-name>Servlets</display-name>
<servlet> 
<servlet-name>Start</servlet-name> 
<servlet-class>start.Start</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>Start</servlet-name> 
<url-pattern>/Start</url-pattern> 
</servlet-mapping>
     <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>

Now whenever I run the index.html on the server, everything works fine, however after pressing the button it specifies a 404 Error and I have no idea why exactly this occurs. I've tried using just /Start as action in index.html, which leads to the error message just pointing to /Start, however when I use Servlets/Start it is pointing to Servlets/Servlets/Start if that helps. Entering localhost:8080/Servlets prompts me to index.html which then leads to the same problem

As I'm slowly getting very frustrated, I'd like to ask you for help, thanks in advance!

Seteris
  • 35
  • 1
  • 6
  • In your form resource is given as `"Servlets/Start"` but in web.xml as just "Start". The default location in tomcat is classes folder. Where exactly are your classes? – pahan May 28 '16 at 10:52
  • @224 I've already tried just using /Start and it didn't work aswell =/ – Seteris May 28 '16 at 11:01
  • @Pshemo http://localhost:8080/Servlets/Servlets/Start is the URL, my project is called Servlets – Seteris May 28 '16 at 11:01
  • @224 it seems like I missed something very crucial, my tomcat install doesn't have a Phone_book directory (keep in mind the service is not running so that's not the source of the error) and I think I missed something about class files, because there aren't any.. sigh – Seteris May 28 '16 at 11:14
  • "the web.xml file looks for class files in tomcat/webapps/Servlets/WEB-INF/classes, and if your classes are not there, it will give this error. What is the location of your class files?" This is what I meant to say. – pahan May 28 '16 at 11:15
  • don't get frustrated. Start over. Find a good tutorial. [link1](http://www.studytonight.com/servlet/creating-servlet-in-eclipse.php) or [link2](http://www.javatpoint.com/creating-servlet-in-eclipse-ide) – pahan May 28 '16 at 11:23
  • @224 Alright, it seems my question should actually have been: Where to locate classes and where exactly do I put them? Now I'm even more frustrated =/ – Seteris May 28 '16 at 11:24
  • @224 Thanks a lot for your help, I guess I'll start over, hope it works out this time =) – Seteris May 28 '16 at 11:24

2 Answers2

1

I have copied your source codes and created a web project in eclipse. enter image description here

The Start.java (inside a package named start) is as follows:-

package start;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

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 Start
 */
@WebServlet("/Start")
public class Start extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Start() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
        PrintWriter pw = respond.getWriter();
        respond.setContentType("text/html");
        String tb = request.getParameter("table");
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc::oracle::thin:@localhost:music", "root", "1234");
            Statement st = con.createStatement();
            System.out.println("connection established successfully!");
            ResultSet rs = st.executeQuery("SELECT * FROM" + tb);

            pw.println("<table border=1>");
            while (rs.next()) {
                pw.println("<tr><td>" + rs.getInt(1) + "</td></td>+rs.getString(2)+</td>" + "<td>" + rs.getString(3)
                        + "</td></tr>");
            }
            pw.println("</table>");
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        // response.getWriter().append("Served at:
        // ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

The web.xml has been modified to remove servlet mapping as it exists as @WebServlet on the class itself. The content is as follows:

<?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>Servlets</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>
</web-app>

The index.html (in WebContent folder) is as follows:

<!DOCTYPE html>
<html>
<form action="Start" method="post">
<font face="verdana" size="2">
 Enter Table Name :<input type="text" name="table"> 
                   <input type="submit" value="Display">
  </font>
</form>
</html>

The result is that I can access the web application at http://localhost:8080/Servlets/ and by clicking on the display button, go to the next page as well.

enter image description here

Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19
  • I can run it up to this point aswell, however entering a table name(with a valid and existant table with data in it) returns me the 404 error type Status report message /Servlets/Servlets/Start description The requested resource is not available. – Seteris May 28 '16 at 11:20
  • 1
    It actually works now! However the servlet is just a blank white screen, but that might be because MySQL isn't running? Also thanks a lot for the intensive help! – Seteris May 28 '16 at 11:53
  • @Seteris Why you are talking about MySQL? In the Servlet, you are actually connecting to Oracle database. – Sanjeev Saha May 28 '16 at 12:00
  • I am probably the most stupid person right now, that's what you get for trying so many tutorials that all do different stuff ... at least the connectivity issue is resolved - seems like not using web.xml fixed it.. Thanks a lot, I can't tell you how happy I am currently! – Seteris May 28 '16 at 12:06
0

In index.html section change the action to action="Start". In web.xml put the full qualified class name in

stumbler
  • 647
  • 3
  • 11
  • 26