File 5 of 28: authenticate.jsp

Online Class Attendance System

File 5 of 28: authenticate.jsp

Folder Location: OnlineClassAttendance/authenticate.jsp

1. Purpose of This File

This JSP file performs user authentication using JDBC and Oracle Database.

2. Complete JSP Source Code

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

    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if (username == null || password == null ||
        username.trim().isEmpty() || password.isEmpty()) {

        out.print("{\"success\":false,\"message\":\"Username and password are required\"}");

    } else {

        String dbURL = "jdbc:oracle:thin:@localhost:1521/XEPDB1";
        String dbUser = "YOUR_DB_USERNAME";
        String dbPassword = "YOUR_DB_PASSWORD";

        String sql = "SELECT USER_ID, USERNAME, FULL_NAME, ROLE " +
                     "FROM USERS " +
                     "WHERE USERNAME = ? AND PASSWORD = ? " +
                     "AND STATUS = 'ACTIVE'";

        try {

            Class.forName("oracle.jdbc.OracleDriver");

            java.sql.Connection con =
                java.sql.DriverManager.getConnection(
                    dbURL, dbUser, dbPassword
                );

            java.sql.PreparedStatement ps =
                con.prepareStatement(sql);

            ps.setString(1, username.trim());
            ps.setString(2, password);

            java.sql.ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                int userId = rs.getInt("USER_ID");
                String userName = rs.getString("USERNAME");
                String fullName = rs.getString("FULL_NAME");
                String role = rs.getString("ROLE");

                session.setAttribute("userId", userId);
                session.setAttribute("username", userName);
                session.setAttribute("fullName", fullName);
                session.setAttribute("role", role);

                out.print("{");
                out.print("\"success\":true,");
                out.print("\"message\":\"Login successful\",");
                out.print("\"role\":\"" + role + "\"");
                out.print("}");

            } else {

                out.print("{\"success\":false,\"message\":\"Invalid username or password\"}");

            }

            rs.close();
            ps.close();
            con.close();

        } catch (Exception e) {

            application.log("Authentication error", e);

            response.setStatus(500);
            out.print("{\"success\":false,\"message\":\"Server error during authentication\"}");

        }

    }
%>

3. Explanation

  • request.getParameter(): Receives username and password from the HTML form.
  • Class.forName(): Loads the Oracle JDBC driver.
  • DriverManager.getConnection(): Establishes a database connection.
  • PreparedStatement: Executes a parameterized SQL query.
  • executeQuery(): Searches for a matching user.
  • session.setAttribute(): Stores authenticated user information in the session.
  • ResultSet: Retrieves the matching database record.

4. Oracle Database Configuration

Change these three values according to your Oracle Database configuration:

String dbURL = "jdbc:oracle:thin:@localhost:1521/XEPDB1";
String dbUser = "YOUR_DB_USERNAME";
String dbPassword = "YOUR_DB_PASSWORD";

Make sure the Oracle JDBC driver JAR is available in:

Tomcat/lib/ojdbc8.jar

5. Expected JSON Response

Successful login:

{
    "success": true,
    "message": "Login successful",
    "role": "ADMIN"
}

Invalid login:

{
    "success": false,
    "message": "Invalid username or password"
}

6. Important Security Notes

  • This example uses plaintext passwords for classroom demonstration only.
  • Real applications must store securely hashed passwords.
  • Use environment-based database credentials instead of hardcoding them.
  • Use try-with-resources to manage database connections in production.
  • Generate JSON using a JSON library to safely escape database values.

7. How to Test

Start Tomcat and open:

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

Enter a valid username and password from your USERS table. The login form sends the information to authenticate.jsp.


Next File: File 6 of 28 — logout.jsp

We will implement logout functionality by invalidating the current user session.

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

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