Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.servlet.http.HttpSession;
  19.  
  20. /**
  21. *
  22. * @author DELL
  23. */
  24. public class RemoveStudent extends HttpServlet {
  25.  
  26. private final String SALT = "897sdn9j98u98jk";
  27.  
  28. /**
  29. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  30. * methods.
  31. *
  32. * @param request servlet request
  33. * @param response servlet response
  34. * @throws ServletException if a servlet-specific error occurs
  35. * @throws IOException if an I/O error occurs
  36. */
  37. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  38. throws ServletException, IOException, SQLException {
  39. response.setContentType("text/html;charset=UTF-8");
  40. try (PrintWriter out = response.getWriter()) {
  41. /* TODO output your page here. You may use following sample code. */
  42. HttpSession session = request.getSession();
  43. java.sql.Connection con
  44. = DriverManager.getConnection("jdbc:mysql://localhost:3306/gp?zeroDateTimeBehavior=convertToNull", "root", "mohamednabil00000");
  45. String email = request.getParameter("email");
  46.  
  47. String query0 = "select user_id from gp.user_login where email = AES_ENCRYPT('" + email + "',sha2('" + SALT + "',512))";
  48.  
  49. java.sql.Statement st = con.createStatement();
  50. String id = request.getParameter("student_id");
  51. boolean ok=true;
  52. if (id == null ||id.equals("")) {
  53. ResultSet rs = st.executeQuery(query0);
  54. if (rs.next()) {
  55. id= rs.getString(1);
  56.  
  57. } else {
  58. ok=false;
  59. session.setAttribute("msg", "This Email does not exist!");
  60. request.getRequestDispatcher("remove_student.jsp").forward(request, response);
  61. }
  62.  
  63. }
  64.  
  65. if(ok){
  66. String query1 = "delete from gp.student_user where student_id=" + id;
  67. String query2 = "delete from gp.user_login where user_id=" + id;
  68.  
  69. System.out.println("hereee " + id);
  70.  
  71. try {
  72. st.executeUpdate(query1);
  73. st.executeUpdate(query2);
  74. request.getRequestDispatcher("admin_view_students.jsp").forward(request, response);
  75. } catch (SQLException | ServletException | IOException e) {
  76.  
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. }
  85.  
  86. }
  87.  
  88.  
  89. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  90. /**
  91. * Handles the HTTP <code>GET</code> method.
  92. *
  93. * @param request servlet request
  94. * @param response servlet response
  95. * @throws ServletException if a servlet-specific error occurs
  96. * @throws IOException if an I/O error occurs
  97. */
  98. @Override
  99. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  100. throws ServletException, IOException {
  101. try {
  102. processRequest(request, response);
  103. } catch (SQLException ex) {
  104. Logger.getLogger(RemoveStudent.class.getName()).log(Level.SEVERE, null, ex);
  105. }
  106. }
  107.  
  108. /**
  109. * Handles the HTTP <code>POST</code> method.
  110. *
  111. * @param request servlet request
  112. * @param response servlet response
  113. * @throws ServletException if a servlet-specific error occurs
  114. * @throws IOException if an I/O error occurs
  115. */
  116. @Override
  117. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  118. throws ServletException, IOException {
  119. try {
  120. processRequest(request, response);
  121. } catch (SQLException ex) {
  122. Logger.getLogger(RemoveStudent.class.getName()).log(Level.SEVERE, null, ex);
  123. }
  124. }
  125.  
  126. /**
  127. * Returns a short description of the servlet.
  128. *
  129. * @return a String containing servlet description
  130. */
  131. @Override
  132. public String getServletInfo() {
  133. return "Short description";
  134. }// </editor-fold>
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement