Guest User

Untitled

a guest
Jun 2nd, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. package enquêtes;
  2. import java.io.*;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.servlet.*;
  14. import javax.servlet.http.*;
  15.  
  16. public class Inlogservlet_nieuwste_versie extends HttpServlet {
  17.  
  18. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException, IOException {
  20. HttpSession session = request.getSession(true);
  21. String userName = request.getParameter("anaam");
  22. String reserveringsNr = request.getParameter("resnr");
  23. String isAdmin = request.getParameter("admin");
  24. String klantNaam = ("Select GebruikerNaam FROM KLANT WHERE KLANT.GebruikerNaam ="+userName);
  25. String klantPw = ("SELECT ReserveringsNr FROM KLANT WHERE KLANT.ReserveringsNr ="+reserveringsNr);
  26.  
  27. boolean loginSuccess;
  28. if (userName.equals(klantNaam) && reserveringsNr.equals(klantPw)) {
  29. loginSuccess = true;
  30. } else {
  31. loginSuccess = false;
  32. }
  33. session.setAttribute("loginSuccess", loginSuccess);
  34.  
  35. // maak een database connectie
  36. Connection connection = connectToDB();
  37. // als success forward naar success
  38. ServletContext servletContext = this.getServletContext();
  39. RequestDispatcher rd = null;
  40.  
  41. //Voor admin geval
  42. if (isAdmin != null && Boolean.valueOf(isAdmin)) {
  43. // Is Admin
  44. Map klantAntwoord = zoekKlantenAntwoord(connection, userName, reserveringsNr);
  45. // idem beneden vul de request, sessie in
  46.  
  47. // forward naar nieuwe pagina (overzicht antwoorden
  48.  
  49. } else { // is Klant
  50. // Verifieer de login gegevens
  51. Map klantGegevens = searchKlant(connection, userName, reserveringsNr);
  52.  
  53.  
  54. if (klantGegevens != null) {
  55. request.getSession().setAttribute("plaatsnaam", klantGegevens.get("plaatsnaam"));
  56. request.getSession().setAttribute("Land", klantGegevens.get("Land"));
  57. // } else {
  58. // session.setAttribute("loginSuccess", loginSuccess);
  59. // }
  60.  
  61. rd = servletContext.getRequestDispatcher("/inlogscherm.jsp");//TODO verify context
  62.  
  63. }
  64.  
  65.  
  66. rd.forward(request, response);
  67. // Anders geeft foutmelding
  68. }
  69. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  70. /**
  71. * Handles the HTTP <code>GET</code> method.
  72. * @param request servlet request
  73. * @param response servlet response
  74. */
  75. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  76. throws ServletException, IOException {
  77. processRequest(request, response);
  78. }
  79.  
  80. /**
  81. * Handles the HTTP <code>POST</code> method.
  82. * @param request servlet request
  83. * @param response servlet response
  84. */
  85. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87. processRequest(request, response);
  88. }
  89.  
  90. /**
  91. * Returns a short description of the servlet.
  92. */
  93. public String getServletInfo() {
  94. return "Short description";
  95. }
  96.  
  97. private Connection connectToDB() {
  98. Connection connection = null;
  99. try {
  100. // TODO verify
  101. String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  102.  
  103. Class.forName(driverName);
  104.  
  105. // Create a connection to the database
  106. String serverName = "mysql.cmi-hro.nl:3306";
  107. String mydatabase = "cmi0806525";
  108. String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
  109.  
  110. // Vraag de db username en passord
  111. String dbUserName = "cmi0806525";
  112. String dbPassword = "hxfw064j";
  113. connection = DriverManager.getConnection(url, dbUserName, dbPassword);
  114. } catch (ClassNotFoundException e) {
  115. // Could not find the database driver
  116. System.out.println("Could not find the database driver " + e.getMessage());
  117. } catch (SQLException e) {
  118. // Could not connect to the database
  119. e.printStackTrace();
  120. }
  121. return connection;
  122. }
  123.  
  124. // In het geveal van klant
  125. private Map searchKlant(Connection connection, String userName, String reserveringsNr) {
  126. Map klantGegevens = null;
  127. String sqlQuery = "Select * from KLANT where GebruikersNaam=" + userName + "and ReserveringsNr" + reserveringsNr;
  128. try {
  129.  
  130. PreparedStatement ps = connection.prepareStatement(sqlQuery);
  131.  
  132. // Lees resultaat terug uit de ps
  133. ResultSet result = ps.executeQuery();
  134. if (result.getRow() > 0) {
  135. // Klant bestaat, haal klanten id op
  136. int klantId = result.getInt("KlantID");
  137. // Aan de hand van de klantenid haal boekinggegevens
  138. sqlQuery = "Select a.Plaatsnaam, a.Land from ACCOMODATIE a where a.AccoID in (Select b.AccoID from BOOKING b where b.KlantID = " + klantId;
  139. result = ps.executeQuery(sqlQuery);
  140.  
  141. klantGegevens = new HashMap();
  142. klantGegevens.put("plaatsnaam", result.getString("Plaatsnaam"));
  143. klantGegevens.put("land", result.getString("Land"));
  144.  
  145. return klantGegevens;
  146. }
  147.  
  148. } catch (SQLException ex) {
  149. Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
  150. }
  151.  
  152. return klantGegevens;
  153. }
  154.  
  155. private Map zoekKlantenAntwoord(Connection connection, String adminName, String password) {
  156. Map klantAntwoord = null;
  157. // TODO Achterhaal de tabelnaam, gebruikers is een gok!!! ook de reserveringsNr
  158. String sqlQuery = "Select * from ADMIN where AdminNaam=" + adminName + "and Wachtwoord=" + password;
  159. try {
  160.  
  161. PreparedStatement ps = connection.prepareStatement(sqlQuery);
  162.  
  163. // Lees resultaat terug uit de ps
  164. ResultSet result = ps.executeQuery();
  165. if (result.getRow() > 0) {
  166. // Admin bestaat, haal ingevulde antwoorden op
  167. sqlQuery = "Select a.plaatsnaam, a.Land from Accomodatie a where a.AccoID in ( " +
  168. "Select b.BookingID in (" +
  169. "Select ant.Waarde from ANTWOOORD ant";
  170.  
  171. // Voer query uit
  172. result = ps.executeQuery(sqlQuery);
  173.  
  174. klantAntwoord = new HashMap();
  175. klantAntwoord.put("plaatsnaam", result.getString("Plaatsnaam"));
  176. klantAntwoord.put("land", result.getString("Land"));
  177. klantAntwoord.put("Waarde", result.getString("Waarde"));
  178.  
  179. return klantAntwoord;
  180. }
  181. } catch (Exception e) {
  182. e.printStackTrace();
  183. }
  184.  
  185.  
  186. return klantAntwoord;
  187.  
  188. }
  189. }
Add Comment
Please, Sign In to add comment