Question
Java Servlets with tomcat Implement a remember me function on the provided Login.java code through the creation of a checkbox. When a Student checks the
Java Servlets with tomcat
Implement a "remember me" function on the provided Login.java code through the creation of a 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. *The credentials to login to the page are "cysun" for the username and "abcd" for the password*
Login.java
package cs320.servlet; 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; @WebServlet("/Login") public class Login extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "
" ); out.println( "Login" ); out.println( "
" ); out.println( "
" ); out.println( "Username: " ); out.println( "Password: " ); out.println( " " ); out.println( "
" ); out.println( "
" ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { if( request.getParameter( "username" ).equals( "cysun" ) && request.getParameter( "password" ).equals( "abcd" ) ) { request.getSession().setAttribute( "user", "cysun" ); response.sendRedirect( "Members" ); } else response.sendRedirect( "Login" ); } }
Members.java
package cs320.servlet; 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; @WebServlet("/Members") public class Members extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // check if a user has logged in or not if( request.getSession().getAttribute( "user" ) == null ) { response.sendRedirect( "Login" ); return; } // if the user is logged in, display the members-only content response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "
" ); out.println( "Member-Only Area" ); out.println( "" ); out.println( "
Member-Only Area!
" ); out.println( "Logout" ); out.println( "" ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { doGet( request, response ); } }
Logout.java
package cs320.servlet; 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("/Logout") public class Logout 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 ); } }
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