File 4 of 28: login.jsp

Online Class Attendance System

File 4 of 28: login.jsp

Folder Location: OnlineClassAttendance/login.jsp

1. Purpose of This File

This file checks the current user's session. If the user is logged in, it returns the user ID, username, full name, and role.

2. Complete JSP Source Code

<%
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    Integer userId = (Integer) session.getAttribute("userId");
    String username = (String) session.getAttribute("username");
    String fullName = (String) session.getAttribute("fullName");
    String role = (String) session.getAttribute("role");

    if (userId != null) {

        out.print("{");
        out.print("\"loggedIn\":true,");
        out.print("\"userId\":" + userId + ",");
        out.print("\"username\":\"" + username + "\",");
        out.print("\"fullName\":\"" + fullName + "\",");
        out.print("\"role\":\"" + role + "\"");
        out.print("}");

    } else {

        out.print("{");
        out.print("\"loggedIn\":false,");
        out.print("\"message\":\"User is not logged in\"");
        out.print("}");

    }
%>

3. Explanation

  • setContentType(): Specifies that the response contains JSON data.
  • getAttribute(): Retrieves information stored in the session.
  • userId: Identifies the logged-in user.
  • username: Stores the login username.
  • fullName: Stores the user's full name.
  • role: Identifies whether the user is an ADMIN, TEACHER, or STUDENT.

4. How to Test

Open this URL in your browser:

http://localhost:8080/OnlineClassAttendance/login.jsp

If the user is not logged in, the response will be:

{
    "loggedIn": false,
    "message": "User is not logged in"
}

After successful authentication, the response will contain the user's session information.

5. Important Note

This is a classroom demonstration of session checking. In a production application, JSON strings should be generated using a proper JSON library to safely escape special characters.


Next File: File 5 of 28 — authenticate.jsp

In the next file, we will connect JSP with Oracle Database and verify the username and password using JDBC.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন