Hello every body>> ^_^
I want your help, I have done code that retrive data from the databse and the user can add data in the database , I want to do validition for student ID, if its in the datase before (if its valid before or not) I need to use AJAX to do that, I wrote the whole code for the servlet and jsp and it work good, now I need to add the code for AJAX but I don't know where to add it and what to write. when I search I see code but I don't know where is the good place for that code to be added , please I need your help quickly,
Please If you have code that can work good with my code please add it.
This is servlet code:
@WebServlet(name = "Quiz", urlPatterns = {"/Quiz"})
public class Quiz extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, InstantiationException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String StudentID = request.getParameter("StudentID");
String StudentName = (String) request.getParameter("Studentname");
String CourseID = (String) request.getParameter("CourseID");
String CourseName = (String) request.getParameter("Coursename");
String Tmarks = (String) request.getParameter("totalMarks");
//Connect to Database
Connection con = null;
Statement stmt = null;
String databaseURL = "jdbc:mysql://localhost:3306/user";
String driverName = "com.mysql.jdbc.Driver";
String user = "root";
String password = "";
try {
Class.forName(driverName).newInstance();
con = (Connection) DriverManager.getConnection(databaseURL, user, password);
stmt = (Statement) con.createStatement();
} catch (Exception e) {
System.out.println("ERROR Connecting to DB: " + e);
return;
}
//Insert data
String insertStmt = "insert into quiz values ("
+ "'" + StudentID + "', "
+ "'" + StudentName + "', "
+ "'" + CourseID + "', "
+ "'" + CourseName + "',"
+ "'" + Tmarks + "'"
+ ")";
try {
stmt.executeUpdate(insertStmt);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/Inserted.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
stmt.executeUpdate(insertStmt);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/ErrorDB.jsp");
dispatcher.forward(request, response);
System.out.println("ERROR: Insert into DB: " + e);
return;
}
//Close database
stmt.close();
con.close();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Quiz</title>");
out.println("</head>");
out.println("<body>");
// out.println("<h1>Servlet Quiz at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
THe code of jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="=#cccccc">
<center>
<h1 align="center">Student Grade information</h1>
<form name="form" action="Quiz" method="POST" >
<table border="1" bordercolor="red" table bgcolor="orange">
<thead>
<tr>
<th> Student ID </th>
<th><input type="text" name="StudentID" value="" /><p></th>
</tr>
</thead>
<tbody>
<tr>
<td> Student Name </td>
<td><input type="text" name="Studentname" value="" /><p></td>
</tr>
<tr>
<td>Course ID </td>
<td><input type="text" name="CourseID" value="" /><p></td>
</tr>
<tr>
<td>Course Name </td>
<td><input type="text" name="Coursename" value="" /><p></td>
</tr>
<tr>
<td>Total Mark </td>
<td><input type="text" name="totalMarks" value="" /><p></td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
       <input type="submit" value="Submit" name="submit" />
</form>
<a href="getData.jsp">Display table of student information</a> <p>
<a href="home.jsp">home page</a>
</body>
</html>
I'm waitting your help