index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="login.jsp" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
String un = request.getParameter("username");
String pass = request.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gk", "root", "");
// SQL query to validate user
String query = "SELECT * FROM login WHERE un=? AND pass=?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, un);
pstmt.setString(2, pass);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
// Valid user
out.println("Welcome " + un);
} else {
// Invalid user
out.println("Invalid username or password");
}
con.close();
}
catch (Exception e) {
out.println("Error: " + e);
}
%>
Output:
No comments:
Post a Comment