So I was trying to input to a Database via Servlets I have been getting Error 404 for after submission of the following form built on index.html
<!DOCTYPE html>
<html>
<head>
<title>Sign in</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Inputs to Student Database</h1>
<form action="Server" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="rollno">Roll Number:</label><br>
<input type="text" id="rollno" name="rollno"><br>
<label for="age">Age:</label><br>
<input type="text" id="age" name="age"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is the error I get after clicking Submit screen shot of error shown on browser
here's the Server.java file
import java.io.IOException;
import java.sql.*;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Server extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name=request.getParameter("name");
int roll_no=Integer.parseInt(request.getParameter("rollno"));
int age=Integer.parseInt(request.getParameter("age"));
Exception r=insert(name, roll_no, age);
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Server</title>");
out.println("</head>");
out.println("<body>");
if(r==null)
out.println("<h1>☑️ Record Saved!</h1>");
else
out.println("<h1>❌ Faliure!</h1>\n<b>"+r+"<\b>");
out.println("</body>");
out.println("</html>");
}
}
public static Exception insert(String n, int r, int a){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Students?useSSL=false","<username>","<password>");
String sql = "INSERT INTO stu (rollno, name, age) VALUES (?, ?, ?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, r);
statement.setString(2, n);
statement.setInt(3, a);
statement.executeUpdate();
}
catch(Exception E)
{
return E;
}
return null;
}
}
I have tried replacing then value of action attribute in the for to /Server and WebApp/Server (WebApp is the name of my project) but it didn't work