Advertisement
Guest User

Untitled

a guest
Jan 6th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package registracia;
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.Statement;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19.  
  20. /**
  21. *
  22. * @author Juri
  23. */
  24. @WebServlet(urlPatterns = {"/kontrola"})
  25. public class kontrola extends HttpServlet {
  26.  
  27. String driver = "com.mysql.jdbc.Driver";
  28. Connection con = null;
  29. Statement stmt = null;
  30. ResultSet rs = null;
  31. String userName = "root";
  32. String password = "";
  33. String URL = "jdbc:mysql://localhost/kelebercova_shop";
  34.  
  35. public void init() {
  36. try {
  37. super.init();
  38. Class.forName(driver);
  39. con = DriverManager.getConnection(URL, userName, password);
  40. } catch (Exception ex) {
  41. }
  42. }
  43.  
  44. void ZapisDoDB(PrintWriter out, String meno, String priezvisko, String email, String heslo, String adresa) {
  45. if (email.contains("@") == false || email.contains(".") == false) {
  46. } else {
  47. try {
  48. stmt = con.createStatement(); // "', "+"'" + priezvisko +
  49. stmt.executeUpdate("INSERT INTO pouzivatelia (meno, priezvisko, email, heslo, adresa) VALUES ('" + meno + "','" + priezvisko + "','" + email + "','" + heslo + "','" + adresa + "' ) ");
  50. stmt.close();
  51. } catch (Exception e) {
  52. System.out.println("Problém so zápisom " + e.toString());
  53. }
  54. }
  55.  
  56. }
  57.  
  58.  
  59. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  60. throws ServletException, IOException {
  61. response.setContentType("text/html;charset=UTF-8");
  62. try (PrintWriter out = response.getWriter()) {
  63. String tlacidlo = request.getParameter("tlacidlo").substring(0, 1);
  64. if (tlacidlo.equals("R")) {
  65. ZapisDoDB(out, request.getParameter("meno"), request.getParameter("priezvisko"), request.getParameter("email"), request.getParameter("heslo"), request.getParameter("adresa"));
  66. }
  67.  
  68. out.println("<!DOCTYPE html>");
  69.  
  70. out.println("<title>Servlet kontrola</title>");
  71.  
  72. if (request.getParameter("email").toString().contains("@") == false || request.getParameter("email").toString().contains(".") == false) {
  73.  
  74. } else {
  75.  
  76. // sem vypises napriklad ze Dakujem za registraciu alebo tak
  77.  
  78. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  79. /**
  80. * Handles the HTTP <code>GET</code> method.
  81. *
  82. * @param request servlet request
  83. * @param response servlet response
  84. * @throws ServletException if a servlet-specific error occurs
  85. * @throws IOException if an I/O error occurs
  86. */
  87. @Override
  88. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  89. throws ServletException, IOException {
  90. processRequest(request, response);
  91. }
  92.  
  93. /**
  94. * Handles the HTTP <code>POST</code> method.
  95. *
  96. * @param request servlet request
  97. * @param response servlet response
  98. * @throws ServletException if a servlet-specific error occurs
  99. * @throws IOException if an I/O error occurs
  100. */
  101. @Override
  102. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  103. throws ServletException, IOException {
  104. processRequest(request, response);
  105. }
  106.  
  107. /**
  108. * Returns a short description of the servlet.
  109. *
  110. * @return a String containing servlet description
  111. */
  112. @Override
  113. public String getServletInfo() {
  114. return "Short description";
  115. }// </editor-fold>
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement