/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/[Link] to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/[Link] to edit
this template
*/
package controller;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
*
* @author SANG
*/
@WebServlet(name = "Register", urlPatterns = {"/Register"})
public class Register extends HttpServlet {
/**
* 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
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Register</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Servlet Register at " + [Link]() +
"</h1>");
[Link]("</body>");
[Link]("</html>");
}
}
// <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 {
[Link]("/Register/[Link]").forward(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 {
HttpSession session = [Link]();
String fname = [Link]("fname");
String lname = [Link]("lname");
String email = [Link]("email");
String birthdate = [Link]("birthdate");
String gender = [Link]("gender");
String password = [Link]("password");
String re_password = [Link]("re_password");
String Phone = [Link]("Phone");
String Address = [Link]("address");
String msg;
int checkfailed = 0; //test verify input
UserDAO ud = new UserDAO();
if () {
msg = "Re-Password not like pasword";
checkfailed = 1;
} else if (validatePhone(Phone) != null) {
msg = validatePhone(Phone);
checkfailed = 1;
} else if (validatePass(password) != null) {
msg = validatePass(password);
checkfailed = 1;
} else if (validateName(fname) != null) {
msg = validateName(fname);
checkfailed = 1;
} else if (validateName(lname) != null) {
msg = validateName(lname);
checkfailed = 1;
} else if (validateBirthdate(birthdate) != null) {
msg = validateBirthdate(birthdate);
checkfailed = 1;
} else if (validateBirthdate(birthdate) != null) {
msg = validateBirthdate(birthdate);
checkfailed = 1;
} else if (validateAddress(Address) != null) {
msg = validateAddress(Address);
checkfailed = 1;
} else {
boolean check = [Link](fname, lname, email, birthdate, gender,
password, Phone, Address);
if (check) {
msg = "Check your verify message email";
checkfailed = 0;
} else {
msg = "Email has been registered";
checkfailed = 1;
}
}
[Link]("msg", msg);
[Link]("checkfailed", checkfailed);
if (checkfailed == 1) {
[Link]("fname", fname);
[Link]("lname", lname);
[Link]("email", email);
[Link]("birthdate", birthdate);
[Link]("gender", gender);
[Link]("password", password);
[Link]("re_password", re_password);
[Link]("Phone", Phone);
[Link]("Address", Address);
}
[Link]("/Register/[Link]").forward(request,
response);
}
public String validateAddress(String address) {
// Regular expression to match letters and spaces
String regex = "^[a-zA-Z\\s]*$";
if (address == null || [Link]().isEmpty()) {
return "Address cannot be null or empty";
} else if ([Link](" ")) {
return "Leading spaces are not allowed";
} else if () {
return "Address must contain only letters and spaces";
}
return null;
}
public String validateName(String name) {
// Regular expression to match only English letters
String regex = "^[a-zA-Z]+$";
if (name == null || [Link]().isEmpty()) {
return "Name cannot be null or empty";
} else if ([Link](" ")) {
return "Leading spaces are not allowed";
} else if () {
return "Name must contain only letters";
}
return null;
}
public String validatePhone(String p) {
if (p == null || [Link]()) {
return "Phone must not be empty";
} else if ([Link](" ")) {
return "Phone number must not contain leading spaces";
} else if () {
return "Phone must start with 0 and contain 10 digits";
}
return null;
}
public String validatePass(String p) {
if (p == null || ) {
return "Password must be between 6 and 16 characters";
}
return null;
}
public String validateBirthdate(String birthdate) {
if (birthdate == null || [Link]()) {
return "Birthdate not empty";
}
DateTimeFormatter formatter = [Link]("yyyy-MM-dd");
try {
LocalDate dateOfBirth = [Link](birthdate, formatter);
LocalDate currentDate = [Link]();
Period age = [Link](dateOfBirth, currentDate);
if ([Link]() < 15) {
return "User must be at least 15 years old";
}
} catch (DateTimeParseException e) {
return "Invalid birthdate format";
}
return null;
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>