0

Hello everyone hope you having a good day,because i'm not xD , i'm new to the web programming and i've started with JSP and Servelts to do web operation my problem is when i click on "delete" it triggers the servlet but nothing happen the row doesn't get deleted and the value ( pw.print(name) ) doesn't show up. these are my codes.

DeleteServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class deleteuser extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter pw=response.getWriter();
    try {

        Connection con=Connecter.obtenirconnexion();
        Statement st=con.createStatement();
        String name=request.getParameter("id");
        String Query="delete from users where name ='"+ name+"'";
        st.executeUpdate(Query);
        pw.print(name);


    } catch (SQLException ex) {
        Logger.getLogger(deleteuser.class.getName()).log(Level.SEVERE, null, ex);
    }
     request.getRequestDispatcher("users.jsp").forward(request, response);


}
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

Users.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="login.Connecter"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Users</title>
    <style> table,td,th
        {
            border:1px black solid;
        } </style>
</head>
<body>
    <h1>Hello World</h1>
    <%
        Connection con = Connecter.obtenirconnexion();
        String req = "select * from users";
        PreparedStatement pst = con.prepareStatement(req);
        ResultSet cur = pst.executeQuery();
        out.println("<table ><tr><th>Name</th> <th>Password </th><th>Rank         </th><th>Delete </th>");
        while (cur.next()) {
            out.println("<tr><td>" + cur.getString(1) + "</td><td>" + cur.getString(2) + "</td><td>" + cur.getString(3) + "</td><td> <a href=deleteuser id='"+cur.getString(1)+"'> delete </a></td></tr>");
        }
        out.println("</table>");
        out.close();
    %>       
</body>

Anas Haxer
  • 35
  • 2
  • 8
  • Any errors in your logfile? – Jens Feb 24 '16 at 15:15
  • no there is nothing :/ – Anas Haxer Feb 24 '16 at 15:16
  • Maybe your click on "delete" invoke post method ? Not sure, but if no errors appear, this could be a problem. – Thodgnir Feb 24 '16 at 15:19
  • before the code in the servlet was in doPost method but the server showed me an error saying "La méthode HTTP GET n''est pas supportée par cette URL" means that the method HTTP GET isn't supported by this url so i looked it up and i found that i had to move it to the doGet and yes that server error didn't show after it – Anas Haxer Feb 24 '16 at 15:24

1 Answers1

1

Well, the link to the DeleteServlet is broken. In order to pass a request parameter, it should be:

<a href=deleteuser?id='"+cur.getString(1)+"'> delete </a>

(note the ?)

But what you do is bad.

  • you have direct database accesses in a JSP. Best practices recommend to do data accesses in a servlet that would populate request attributes and forward to a JSP to display everything (or even to have layers that could be unit tested : Servlet <-> service <-> persistence)
  • you do a response.getWriter().print(...) inside the servlet before forwarding to the JSP. What you print will be outside of the body part (and even before the head!): here again you should put it in a request attribute and use it in JSP
  • you use a GET request to change database values. GET requests should only be used for read only operations. A delete should use a POST request - this last part is mainly for purists and if it has been the only problem I would not even have mentioned it...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • the "?" isn't the problem i tried it before and it's giving the same results which are NOT DELETING ANYTHING , considering,getwriter i removed it because i don't need it anyway i was only trying to see if i have a problem in my request to the database :) – Anas Haxer Feb 24 '16 at 16:10