mercredi 24 février 2016

Deleting table row using jsp and servlet

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>




Aucun commentaire:

Enregistrer un commentaire