Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlets;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.HashMap;
- 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;
- import beans.Customer;
- import beans.Login;
- import dbconn.ConnectionUtils;
- import dbconn.DBUtils;
- import utils.SessionUtils;
- @WebServlet(urlPatterns = {"/editAccountDetails"})
- public class EditAccountDetailsServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private Connection conn = null;
- public EditAccountDetailsServlet() {
- super();
- }
- public void updateUserRecords(Connection conn, HashMap<String, String> map) throws SQLException {
- String customerID = map.get("id");
- // CUSTOMERS table
- if (map.get("firstName") != null && map.get("firstName").length() > 0){
- DBUtils.updateCustomerRecord(conn, customerID, "FIRSTNAME", map.get("firstName"));
- }
- if (map.get("surname") != null && map.get("surname").length() > 0) {
- DBUtils.updateCustomerRecord(conn, customerID, "SURNAME", map.get("surname"));
- }
- if (map.get("homeAddress") != null && map.get("homeAddress").length() > 0) {
- DBUtils.updateCustomerRecord(conn, customerID, "ADDRESS", map.get("homeAddress"));
- }
- if (map.get("phoneNumber") != null && map.get("phoneNumber").length() > 0) {
- DBUtils.updateCustomerRecord(conn, customerID, "PHONENUMBER", map.get("phoneNumber"));
- }
- if (map.get("emailAddress") != null && map.get("emailAddress").length() > 0) {
- DBUtils.updateCustomerRecord(conn, customerID, "EMAIL", map.get("emailAddress"));
- }
- // LOGINCREDS table
- if (map.get("userName") != null && map.get("userName").length() > 0) {
- DBUtils.updateLoginRecord(conn, customerID, "USERNAME", map.get("surname"));
- }
- if (map.get("password") != null && map.get("password").length() > 0) {
- DBUtils.updateLoginRecord(conn, customerID, "PASSWORD", map.get("surname"));
- }
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- RequestDispatcher dispatcher //
- = this.getServletContext().getRequestDispatcher("/WEB-INF/views/editAccountDetails.jsp");
- dispatcher.forward(request, response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // User details
- String firstName = request.getParameter("firstName");
- String surname = request.getParameter("surname");
- String homeAddress = request.getParameter("homeAddress");
- String phoneNumber = request.getParameter("phoneNumber");
- String emailAddress = request.getParameter("emailAddress");
- // Username & Password
- String userName = request.getParameter("userName");
- String password = request.getParameter("password");
- Customer loggedInCustomer = SessionUtils.getLoggedInCustomer(request.getSession());
- Login customerLogin = null;
- HashMap<String, String> map = new HashMap<>();
- map.put("id", loggedInCustomer.getCustomerID());
- map.put("firstName", firstName);
- map.put("surname", surname);
- map.put("homeAddress", homeAddress);
- map.put("phoneNumber", phoneNumber);
- map.put("emailAddress", emailAddress);
- map.put("password", password);
- map.put("userName", userName);
- Connection conn;
- try {
- conn = ConnectionUtils.getConnection();
- updateUserRecords(conn, map);
- loggedInCustomer = DBUtils.findRegisteredCustomer(conn, userName, password);
- customerLogin = DBUtils.retrieveLoginRecord(conn, loggedInCustomer.getCustomerID());
- request.setAttribute("loggedInCustomer", loggedInCustomer);
- request.setAttribute("customerCredentials", customerLogin);
- // Forward to /WEB-INF/views/register.jsp
- RequestDispatcher dispatcher //
- = this.getServletContext().getRequestDispatcher("/WEB-INF/views/account.jsp");
- dispatcher.forward(request, response);
- } catch (ClassNotFoundException | SQLException | NullPointerException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement