Question: 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.
1.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.
2. 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, e.g.
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 (i.e. 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 (i.e. login fails), redirect back to the login form.
2.(20pt) 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.
3.Clicking on Logout should take the user to a Logout servlet which invalidates the session (i.e. 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

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!