Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this lab you are going to implement basic login and logout operations using the Servlet Session Tracking API. 1 . Create a Java class
In this lab you are going to implement basic login and logout operations using the Servlet Session Tracking API.
Create a Java class User that represents a user. In particular, the User class should have at least three properties: name, username, and password. You may add additional properties like id if you want.
Create a servlet Login as follows:
a In the init method of the Login servlet, create at least two User objects. One of the User objects must have the following property values:
name: "John Doe"
username: "jdoe"
password: "abcd"
Add the User objects to a list and save the list in application scope.
b In doGet display a login form, eg
Username:
Password:
Login submit button
The form should submit data to the Login servlet using a POST request.
c In doPost check if the submitted username and password match any user in the list of users. If a match is found ie login is successful set a session attribute user whose value is the User object that matches the submitted username and password, then redirect to the Members servlet. If no match is found ie login fails redirect back to the login form.
pt Create a servlet Members. This servlet first checks if the session attribute user is set. If so it displays the following:
Hi Welcome to the Members Area!
Logout
where is the name of the user who is currently logged in you can get the name from the session attribute user which is a User object If the session attribute user does not exist, the servlet redirects to the login form.
Clicking on Logout should take the user to a Logout servlet which invalidates the session ie calling the invalidate method in HttpSession then redirects to the login form.
Notes
All Java classes in a web application must be in a package.
In Java, to check if the values of two strings are the same, you have to use the method equals or equalsIgnoreCase instead of the operator.
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