CSE 3rd Year 5th Sem, Sec- C & D (2025-26)
Lab/ Practical’s details:
1. Write a program in JSP to provide Login. Password Functionality using
Type 1 Driver.
Step 1: Create the users Table
CREATE TABLE users (
username VARCHAR(50) PRIMARY KEY,
password VARCHAR(50)
);
INSERT INTO users VALUES ('admin', 'admin123');
Step 2: Configure ODBC DSN
Step 3: JSP Login Page (login.jsp)
<%@ page import="java.sql.*" %>
<html>
<head><title>Login Page</title></head>
<body>
<h2>Login</h2>
<form method="post" action="login.jsp">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
<%
String uname = request.getParameter("username");
String pwd = request.getParameter("password");
if (uname != null && pwd != null) {
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:UserDSN");
PreparedStatement ps = con.prepareStatement("SELECT * FROM users
WHERE username=? AND password=?");
ps.setString(1, uname);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
out.println("<h3 style='color:green;'>Login Successful! Welcome, " +
uname + ".</h3>");
} else {
out.println("<h3 style='color:red;'>Invalid username or
password.</h3>");
}
con.close();
} catch (Exception e) {
out.println("<h3 style='color:red;'>Error: " + e.getMessage() + "</h3>");
}}
%>
</body>
</html>
➢ The form submits to the same page (login.jsp).
➢ On submission, the JSP connects to the database using the Type 1 driver.
➢ It checks if the username and password match a record in the users table.
➢ Displays success or error message accordingly.
------------------------------------------------------------------------------------------------