Advertisement
Guest User

nefunguje:D

a guest
Dec 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 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.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.Statement;
  14. import javax.servlet.RequestDispatcher;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21. /**
  22. *
  23. * @author Lívia
  24. */
  25. @WebServlet(urlPatterns = {"/reg"})
  26. public class reg extends HttpServlet {
  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/mydb";
  34.  
  35. /**
  36. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  37. * methods.
  38. *
  39. * @param request servlet request
  40. * @param response servlet response
  41. * @throws ServletException if a servlet-specific error occurs
  42. * @throws IOException if an I/O error occurs
  43. */
  44. public void Formular(PrintWriter out) {
  45.  
  46. out.print("<html>\n" +
  47. "<style>\n" +
  48. "/* Full-width input fields */\n" +
  49. "input[type=text], input[type=password] {\n" +
  50. " width: 100%;\n" +
  51. " padding: 12px 20px;\n" +
  52. " margin: 8px 0;\n" +
  53. " display: inline-block;\n" +
  54. " border: 1px solid #ccc;\n" +
  55. " box-sizing: border-box;\n" +
  56. "}\n" +
  57. "\n" +
  58. "/* Set a style for all buttons */\n" +
  59. "button {\n" +
  60. " background-color: #4CAF50;\n" +
  61. " color: white;\n" +
  62. " padding: 14px 20px;\n" +
  63. " margin: 8px 0;\n" +
  64. " border: none;\n" +
  65. " cursor: pointer;\n" +
  66. " width: 100%;\n" +
  67. "}\n" +
  68. "\n" +
  69. "/* Extra styles for the cancel button */\n" +
  70. ".cancelbtn {\n" +
  71. " padding: 14px 20px;\n" +
  72. " background-color: #f44336;\n" +
  73. "}\n" +
  74. "\n" +
  75. "/* Float cancel and signup buttons and add an equal width */\n" +
  76. ".cancelbtn,.signupbtn {\n" +
  77. " float: left;\n" +
  78. " width: 50%;\n" +
  79. "}\n" +
  80. "\n" +
  81. "/* Add padding to container elements */\n" +
  82. ".container {\n" +
  83. " padding: 16px;\n" +
  84. "}\n" +
  85. "\n" +
  86. "/* Clear floats */\n" +
  87. ".clearfix::after {\n" +
  88. " content: \"\";\n" +
  89. " clear: both;\n" +
  90. " display: table;\n" +
  91. "}\n" +
  92. "\n" +
  93. "/* Change styles for cancel button and signup button on extra small screens */\n" +
  94. "@media screen and (max-width: 300px) {\n" +
  95. " .cancelbtn, .signupbtn {\n" +
  96. " width: 50%;\n" +
  97. " }\n" +
  98. "}\n" +
  99. "</style>\n" +
  100. "<body>\n" +
  101. " <center>\n" +
  102.  
  103. "<h2>Registračný formulár</h2>\n" +
  104. " </center>\n" +
  105. //------------FORMULAR---------------------------------
  106. "<form action=\"/action_page.php\" style=\"border:1px solid #ccc\">\n" +
  107. " <div class=\"container\">\n" +
  108. //------------MENO--------------------------------------
  109. " <label><b>Meno</b></label>\n" +
  110. " <input type=\"text\" placeholder=\" Vložte Meno\" name=\"meno\" required>\n" +
  111. "\n" +
  112. //------------PRIEZVISKO--------------------------------
  113. "<label><b>Priezvisko</b></label>\n" +
  114. " <input type=\"text\" placeholder=\" Vložte Priezvisko\" name=\"email\" required>\n" +
  115. "\n" +
  116. //------------EMAIL------------------------------------
  117. "<label><b>Email</b></label>\n" +
  118. " <input type=\"text\" placeholder=\" Vložte Email\" name=\"email\" required>\n" +
  119. "\n" +
  120. //------------LOGIN---------------------------------------
  121. "<label><b>Login</b></label>\n" +
  122. " <input type=\"text\" placeholder=\" Vložte Login\" name=\"Login\" required>\n" +
  123. "\n" +
  124. "\n" +
  125. //------------HESLO----------------------------------------
  126. " <label><b>Heslo</b></label>\n" +
  127. " <input type=\"password\" placeholder=\"Vložte Heslo\" name=\"psw\" required>\n" +
  128. "\n" +
  129. //ZNOVU HESLO--------------------------------------------
  130. " <label><b>Zopakujte Heslo</b></label>\n" +
  131. " <input type=\"password\" placeholder=\"Zopakujte Heslo\" name=\"psw-repeat\" required>\n" +
  132. " <input type=\"checkbox\" checked=\"checked\"> Zapamätať\n" +
  133. " <p>Vytvorením účtu súhlasíte s <a href=\"#\">Obchodnými podmienkami</a>.</p>\n" +
  134. "\n" +
  135. " <div class=\"clearfix\">\n" +
  136. //--------------ODIST---------------------
  137. " <button type=\"button\" class=\"cancelbtn\">Odísť</button>\n" +
  138. //---------------REGISTROVAT---------------------
  139. " <button type=\"submit\"name =\"tlacidlo\" class=\"signupbtn\">Registrovať</button>\n" +
  140. " </div>\n" +
  141. " </div>\n" +
  142. "</form>\n" +
  143. "\n" +
  144. "</body>\n" +
  145. "</html>");
  146.  
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  156. throws ServletException, IOException {
  157. response.setContentType("text/html;charset=UTF-8");
  158. try (PrintWriter out = response.getWriter()) {
  159. /* TODO output your page here. You may use following sample code. */
  160. out.println("<!DOCTYPE html>");
  161. String tlacidlo2 = (request.getParameter("tlacidlo")).substring(0,1);
  162. if(tlacidlo2.equals("Z")){
  163. Formular(out);
  164.  
  165. }
  166. if(tlacidlo2.equals("R")){
  167.  
  168. String a=request.getParameter("meno");
  169. String b=request.getParameter("priezvisko");
  170. String c=request.getParameter("email");
  171. String d=request.getParameter("login");
  172. String e=request.getParameter("heslo");
  173.  
  174. try{
  175. Class.forName("com.mysql.jdbc.Driver");
  176. con = DriverManager.getConnection(URL, userName, password);
  177. PreparedStatement ps=con.prepareStatement("insert into pouzivatelia values(?,?,?,?)");
  178. ps.setString(1,a);
  179. ps.setString(2,b);
  180. ps.setString(3,c);
  181. ps.setString(4,d);
  182. ps.setString(5,e);
  183. int i=ps.executeUpdate();
  184. if(i>0)
  185. out.print("You are successfully registered...");
  186.  
  187.  
  188. }catch (Exception e2) {System.out.println(e2);}
  189.  
  190. out.close();
  191. }
  192.  
  193.  
  194.  
  195.  
  196. }
  197. }
  198.  
  199. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  200. /**
  201. * Handles the HTTP <code>GET</code> method.
  202. *
  203. * @param request servlet request
  204. * @param response servlet response
  205. * @throws ServletException if a servlet-specific error occurs
  206. * @throws IOException if an I/O error occurs
  207. */
  208. @Override
  209. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  210. throws ServletException, IOException {
  211. processRequest(request, response);
  212.  
  213. }
  214. /**
  215. * Handles the HTTP <code>POST</code> method.
  216. *
  217. * @param request servlet request
  218. * @param response servlet response
  219. * @throws ServletException if a servlet-specific error occurs
  220. * @throws IOException if an I/O error occurs
  221. */
  222. @Override
  223. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  224. throws ServletException, IOException {
  225. processRequest(request, response);
  226. response.setContentType("text/html");
  227. PrintWriter out = response.getWriter();
  228.  
  229. }
  230.  
  231.  
  232.  
  233.  
  234. /**
  235. * Returns a short description of the servlet.
  236. *
  237. * @return a String containing servlet description
  238. */
  239. @Override
  240. public String getServletInfo() {
  241. return "Short description";
  242. }// </editor-fold>
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement