I have a Java servlet that has a doGet method which is supposed to print a html document along with the contents of a mySql table and get rendered on the browser on clicking a button in the index page. but the on clicking the button nothing happens but a post method to submit record to the table using fetch works perfectly.
Servlet code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","password");
PreparedStatement ps = connection.prepareStatement(QUERY);
ResultSet rs = ps.executeQuery();
PrintWriter out = response.getWriter();
out.println("<html><body><table><tr><th>Name</th><th>Email</th><th>Phone</th><th>Department</th><th>Gender</th><th>Hobbie</th></tr>");
while(rs.next()) {
out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"</td><td>"+rs.getString(6)+"</td></tr>");
}
out.println("</table></body></html>");
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
Fetch API
fetch('studentform')
.then(response => response.text())
.then(text => {
console.log(text);
document.querySelector('#details').innerHTML = text;})
.catch(e=> console.log(e));
studentform is the servlet