Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author Lívia
- */
- @WebServlet(urlPatterns = {"/reg"})
- public class reg extends HttpServlet {
- String driver = "com.mysql.jdbc.Driver";
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- String userName = "root";
- String password = "";
- String URL = "jdbc:mysql://localhost/mydb";
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- public void Formular(PrintWriter out) {
- out.print("<html>\n" +
- "<style>\n" +
- "/* Full-width input fields */\n" +
- "input[type=text], input[type=password] {\n" +
- " width: 100%;\n" +
- " padding: 12px 20px;\n" +
- " margin: 8px 0;\n" +
- " display: inline-block;\n" +
- " border: 1px solid #ccc;\n" +
- " box-sizing: border-box;\n" +
- "}\n" +
- "\n" +
- "/* Set a style for all buttons */\n" +
- "button {\n" +
- " background-color: #4CAF50;\n" +
- " color: white;\n" +
- " padding: 14px 20px;\n" +
- " margin: 8px 0;\n" +
- " border: none;\n" +
- " cursor: pointer;\n" +
- " width: 100%;\n" +
- "}\n" +
- "\n" +
- "/* Extra styles for the cancel button */\n" +
- ".cancelbtn {\n" +
- " padding: 14px 20px;\n" +
- " background-color: #f44336;\n" +
- "}\n" +
- "\n" +
- "/* Float cancel and signup buttons and add an equal width */\n" +
- ".cancelbtn,.signupbtn {\n" +
- " float: left;\n" +
- " width: 50%;\n" +
- "}\n" +
- "\n" +
- "/* Add padding to container elements */\n" +
- ".container {\n" +
- " padding: 16px;\n" +
- "}\n" +
- "\n" +
- "/* Clear floats */\n" +
- ".clearfix::after {\n" +
- " content: \"\";\n" +
- " clear: both;\n" +
- " display: table;\n" +
- "}\n" +
- "\n" +
- "/* Change styles for cancel button and signup button on extra small screens */\n" +
- "@media screen and (max-width: 300px) {\n" +
- " .cancelbtn, .signupbtn {\n" +
- " width: 50%;\n" +
- " }\n" +
- "}\n" +
- "</style>\n" +
- "<body>\n" +
- " <center>\n" +
- "<h2>Registračný formulár</h2>\n" +
- " </center>\n" +
- //------------FORMULAR---------------------------------
- "<form action=\"/action_page.php\" style=\"border:1px solid #ccc\">\n" +
- " <div class=\"container\">\n" +
- //------------MENO--------------------------------------
- " <label><b>Meno</b></label>\n" +
- " <input type=\"text\" placeholder=\" Vložte Meno\" name=\"meno\" required>\n" +
- "\n" +
- //------------PRIEZVISKO--------------------------------
- "<label><b>Priezvisko</b></label>\n" +
- " <input type=\"text\" placeholder=\" Vložte Priezvisko\" name=\"email\" required>\n" +
- "\n" +
- //------------EMAIL------------------------------------
- "<label><b>Email</b></label>\n" +
- " <input type=\"text\" placeholder=\" Vložte Email\" name=\"email\" required>\n" +
- "\n" +
- //------------LOGIN---------------------------------------
- "<label><b>Login</b></label>\n" +
- " <input type=\"text\" placeholder=\" Vložte Login\" name=\"Login\" required>\n" +
- "\n" +
- "\n" +
- //------------HESLO----------------------------------------
- " <label><b>Heslo</b></label>\n" +
- " <input type=\"password\" placeholder=\"Vložte Heslo\" name=\"psw\" required>\n" +
- "\n" +
- //ZNOVU HESLO--------------------------------------------
- " <label><b>Zopakujte Heslo</b></label>\n" +
- " <input type=\"password\" placeholder=\"Zopakujte Heslo\" name=\"psw-repeat\" required>\n" +
- " <input type=\"checkbox\" checked=\"checked\"> Zapamätať\n" +
- " <p>Vytvorením účtu súhlasíte s <a href=\"#\">Obchodnými podmienkami</a>.</p>\n" +
- "\n" +
- " <div class=\"clearfix\">\n" +
- //--------------ODIST---------------------
- " <button type=\"button\" class=\"cancelbtn\">Odísť</button>\n" +
- //---------------REGISTROVAT---------------------
- " <button type=\"submit\"name =\"tlacidlo\" class=\"signupbtn\">Registrovať</button>\n" +
- " </div>\n" +
- " </div>\n" +
- "</form>\n" +
- "\n" +
- "</body>\n" +
- "</html>");
- }
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- String tlacidlo2 = (request.getParameter("tlacidlo")).substring(0,1);
- if(tlacidlo2.equals("Z")){
- Formular(out);
- }
- if(tlacidlo2.equals("R")){
- String a=request.getParameter("meno");
- String b=request.getParameter("priezvisko");
- String c=request.getParameter("email");
- String d=request.getParameter("login");
- String e=request.getParameter("heslo");
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(URL, userName, password);
- PreparedStatement ps=con.prepareStatement("insert into pouzivatelia values(?,?,?,?)");
- ps.setString(1,a);
- ps.setString(2,b);
- ps.setString(3,c);
- ps.setString(4,d);
- ps.setString(5,e);
- int i=ps.executeUpdate();
- if(i>0)
- out.print("You are successfully registered...");
- }catch (Exception e2) {System.out.println(e2);}
- out.close();
- }
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement