Question: Java Servlets with tomcat Implement a remember me function on the provided student login code through the checkbox. When a Student checks the Remember Me

Java Servlets with tomcat

Implement a "remember me" function on the provided student login code through the checkbox.

When a Student checks the Remember Me checkbox, your application should create a new cookie named "student". The value of the cookie should be the Sha256 hash (as a HEX string ) of the student's ID. When a Student visits your Login page, you should automatically check for the existence of the student cookie. If the cookie exists, you should search all Students in your database (ArrayList), and compare the cookie value against the hash of each student's ID. If there is a match, do not display the login form to the Student. Instead, consider the student logged in (as if they submitted valid credentials) and automatically redirect them to the member's only area (MyProfile). When a Student logs out of your site, you should destroy the student cookie, if it exists, along with invalidating the current session and redirecting the Student back to Login. *you can use john@doe.com and abcd for the email and password, respecitively.*

LoginSessions

package login;

import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList;

import javax.servlet.ServletConfig; 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 javax.servlet.http.HttpSession;

import lab4.Student;

@WebServlet(urlPatterns="/sessions/Login", loadOnStartup=3) public class LoginSessions extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException { super.init(config); // Create a few students ArrayList students = new ArrayList(); students.add(new Student("John", "Doe", "john@doe.com", "abcd")); students.add(new Student("Mary", "Jane", "mary@jane.com", "efgh")); students.add(new Student("Joe", "Boxer", "joe@boxer.com", "ijkl")); // Add the students to the application scope (Servlet Context) getServletContext().setAttribute("students", students); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set the content type response.setContentType("text/html"); // Get a reference to the PrintWriter that lets us talk to the client PrintWriter out = response.getWriter(); // Generate the HTML out.println(""); out.println(""); out.println(""); out.println(" "); out.println(" "); /* Page Title goes here */ out.println(" Login (Sessions)"); out.println(""); /* Page Body goes here */ out.println(""); out.println("

"); out.println("
"); out.println("

Login HttpSessions

"); out.println("
"); // Display the error message if it exists String error = (String) request.getAttribute("error"); if (error != null) out.println("

" + error + "

"); // Create the login form out.println("
"); out.println("
"); out.println(" "); out.println(" "); out.println("
"); out.println("
"); out.println(" "); out.println(" "); out.println("
"); out.println("
"); out.println(" "); out.println("
"); out.println(" "); out.println("
"); out.println("
"); out.println(""); out.println(""); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the credentials from the request object String username = request.getParameter("username"); String password = request.getParameter("password"); // If the user submitted bad input, just redisplay the form if (username == null || username.trim().length() == 0 || password == null || password.trim().length() == 0) { doGet(request, response); // Don't forget, calling `doGet` does not stop the execution of this method. // We need to `return`. return; } // If we get here then the Student submitted a username and password. // Do something with the "Remember Me" checkbox // To do... // Authenticate the Student by searching all of the Students in our app // and comparing the submitted credentials against each student's email and password ArrayList students = (ArrayList) getServletContext().getAttribute("students"); for (Student student : students) { if (student.getEmail().toLowerCase().equals(username.trim().toLowerCase()) && student.getPassword().equals(password)) { // If we get here, the username and password match the current `student`. // Let's create a session attribute that references `this current student`. HttpSession session = request.getSession(); session.setAttribute("authenticatedStudent", student); // Now that we've set an attribute in the session scope, let's // redirect the Student to the "Student's Profile" area. response.sendRedirect("MyProfile"); return; } } // if we get here then we couldn't find a Student that matched the submitted credentials // So, we add an error message to the request scope and redisplay the form request.setAttribute("error", "Invalid username and/or password"); doGet(request, response); }

}

StudentProfile.java

package login;

import java.io.IOException; import java.io.PrintWriter;

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 lab4.Student;

/** * Servlet implementation class StudentProfile */ @WebServlet("/sessions/MyProfile") public class StudentProfile extends HttpServlet { private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Let's get a reference to the student who is currently logged in Student student = (Student) request.getSession().getAttribute("authenticatedStudent"); // If there is no student logged in, redirect back to the login page if (student == null) { response.sendRedirect("Login"); return; } // Set the content type response.setContentType("text/html"); // Get a reference to the PrintWriter that lets us talk to the client PrintWriter out = response.getWriter(); // Generate the HTML out.println(""); out.println(""); out.println(""); out.println(" "); out.println(" "); /* Page Title goes here */ out.println(" Student Profile"); out.println(""); /* Page Body goes here */ out.println(""); out.println("

"); out.println("
"); out.println("

" + student.getFirstName() + "'s Profile

"); out.println("

This is a Student's Only area.

"); out.println(" Logout"); out.println("
"); out.println("

Grades " + student.getFullName() + "

"); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(" "); // Print all of the student's scores. double[] scores = student.getScores(); for (int i = 0; i < scores.length; i++) { out.println(" "); out.println(" "); out.println(" "); out.println(" "); } out.println("
AssignmentScore
Assignment " + (i+1) + "" + scores[i] + "
"); out.println("
"); out.println(""); out.println("");

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }

}

LogoutSessions.java

package login;

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

@WebServlet("/sessions/Logout") public class LogoutSessions extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().invalidate(); response.sendRedirect("Login"); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

}

Student.java

package login;

public class Student {

static int count = 0; static final int NUMBER_OF_ASSIGNMENTS = 5; int id; String firstName, lastName; String email; String password; double[] scores = new double[NUMBER_OF_ASSIGNMENTS]; public Student(String firstName, String lastName, String email, String password) { this.id = count++; this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; // Randomly assign scores to this student; for (int i = 0; i < NUMBER_OF_ASSIGNMENTS; i++) scores[i] = Math.random() * 100; }

public String getFullName() { return firstName + " " + lastName; } public String getFirstName() { return firstName; }

public void setFirstName(String firstName) { this.firstName = firstName; }

public String getLastName() { return lastName; }

public void setLastName(String lastName) { this.lastName = lastName; }

public String getEmail() { return email; }

public void setEmail(String email) { this.email = email; }

public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }

public double[] getScores() { return scores; }

public void setScores(double[] scores) { this.scores = scores; }

public int getId() { return id; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!