Flidro

Untitled

Oct 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package BoogalooBookstore;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.*;
  6. import java.io.IOException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11.  
  12. @WebServlet(name = "RegisterServlet", urlPatterns = "/RegisterServlet")
  13. public class RegisterServlet extends HttpServlet {
  14.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15.         //Get request parameters for username and password and generate salt to be stored
  16.         String username = request.getParameter("username");
  17.         String email = request.getParameter("email");
  18.         String password = request.getParameter("password");
  19.         byte[] salt = MySQLConnectionHandler.generateSalt();
  20.  
  21.         MySQLConnectionHandler connHandler = new MySQLConnectionHandler();
  22.         Connection conn = connHandler.connection;
  23.         PreparedStatement prepStatement = null;
  24.  
  25.         try {
  26.             prepStatement = conn.prepareStatement(
  27.                     "INSERT INTO `asdfaser_users`(`username`, `email`, `password`, `salt`) VALUES (?, ?, ?, ?)");
  28.             prepStatement.setString(1, username);
  29.             prepStatement.setString(2, email);
  30.             prepStatement.setString(3, MySQLConnectionHandler.byteToString(MySQLConnectionHandler.getHashWithSalt(password, "SHA-256", salt)));
  31.             prepStatement.setString(4, MySQLConnectionHandler.byteToString(salt));
  32.             prepStatement.execute();
  33.  
  34.         } catch (SQLException exc) {
  35.             System.out.println("SQLException: " + exc.getMessage());
  36.         } catch (Exception exc) {
  37.             System.out.println("Exception: " + exc.getMessage());
  38.             System.out.println(exc);
  39.         } finally {
  40.             //closing resources
  41.             if (prepStatement != null) {
  42.                 try {
  43.                     prepStatement.close();
  44.                 } catch (SQLException ignored) {
  45.                 }//ignore
  46.                 prepStatement = null;
  47.             }
  48.  
  49.             if (conn != null) {
  50.                 try {
  51.                     conn.close();
  52.                 } catch (SQLException ignored) {
  53.                 }//ignore
  54.                 conn = null;
  55.             }
  56.         }
  57.  
  58.         //invalidate old session
  59.         HttpSession oldSession = request.getSession(false);
  60.         if (oldSession != null) {
  61.             oldSession.invalidate();
  62.         }
  63.  
  64.         //generate a new session
  65.         HttpSession newSession = request.getSession(true);
  66.  
  67.         //session expires after 5 minutes
  68.         newSession.setMaxInactiveInterval(5 * 60);
  69.  
  70.         //create a cookie for the session and set max age to 30 minutes
  71.         Cookie message = new Cookie("message", "Welcome");
  72.         message.setMaxAge(30 * 60);
  73.         response.addCookie(message);
  74.         response.sendRedirect("/LoginSuccess.jsp");
  75.     }
  76. }
Add Comment
Please, Sign In to add comment