Creating a Dynamic Web Project Start by creating a Dynamic Web Project in your IDE. This will serve as the foundation for the login system with visitor tracking functionality.
Creating the Login Form JSP Create a JSP file in the WebContent directory with the following login form structure:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>User Authentication</title>
</head>
<body>
<form action="/WEB02/AuthController" method="post">
Username: <input type="text" name="userid"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Sign In">
</form>
</body>
</html>
Project Structure Organization Organize your source code into logical packages within the web src directory:
servlet package for HTTP controllers dao package for data access objects service package for business logic tools package for database utilities
Authentication Servlet Implementation Create the main servlet that handles login requests and tracks visitor count:
public class AuthController extends HttpServlet {
private AuthService authService = new AuthService();
@Override
public void init() throws ServletException {
int visitorCount = 0;
ServletContext appContext = getServletContext();
appContext.setAttribute("visitorCount", visitorCount);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String userId = req.getParameter("userid");
String passCode = req.getParameter("password");
boolean isValid = authService.authenticate(userId, passCode);
if(isValid) {
ServletContext ctx = getServletContext();
int currentCount = (int)ctx.getAttribute("visitorCount");
currentCount++;
ctx.setAttribute("visitorCount", currentCount);
resp.getWriter().write("Welcome! You are visitor number: " + currentCount);
} else {
resp.getWriter().write("Authentication failed");
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
Service Layer Implementation The service layer handles bussiness logic and exception management:
public class AuthService {
private UserDAO userDAO = new UserDAO();
public boolean authenticate(String username, String password) {
boolean isValid = false;
try {
isValid = userDAO.validateCredentials(username, password);
} catch (SQLException ex) {
ex.printStackTrace();
}
return isValid;
}
}
Data Access Layer The DAO layer handles database operations for user validation:
public class UserDAO {
public boolean validateCredentials(String username, String password)
throws SQLException {
Connection dbConn = DBConnection.getConnection();
String query = "SELECT COUNT(*) FROM users WHERE username = ? AND password = ?";
PreparedStatement stmt = dbConn.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet result = stmt.executeQuery();
boolean isValid = false;
if(result.next()) {
isValid = result.getInt(1) > 0;
}
DBConnection.closeResources(result, stmt, dbConn);
return isValid;
}
}
Deployment and Testing Deploy the application to your web server and test the login functionality. The system will display the visitor count for successful authentications.