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 " + error + "Login HttpSessions
"); 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
}
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.getFirstName() + "'s Profile
"); out.println("This is a Student's Only area.
"); out.println(" Logout"); out.println("Grades " + student.getFullName() + "
"); out.println("Assignment | "); out.println("Score | "); out.println("
---|---|
Assignment " + (i+1) + " | "); out.println("" + scores[i] + " | "); 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started