Package servlet
Class LoginServlet
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
servlet.LoginServlet
- All Implemented Interfaces:
Serializable
,javax.servlet.Servlet
,javax.servlet.ServletConfig
@WebServlet("/login")
public class LoginServlet
extends javax.servlet.http.HttpServlet
Servlet that handles user authentication and login process.
Implements two factor authentication with password verification and OTP.
Redirects users to appropriate pages based on their roles.
- Author:
- TAMIL MUGHILAN
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doGet
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handles GET requests by redirecting to login page.protected void
doPost
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handles POST requests for login and OTP verification.private void
handleLogin
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handles the initial login attempt with email and password.private void
handleOTPVerification
(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handles OTP verification and completes the login process.void
init()
Initializes the servlet with database storage and authentication service.Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
dataStorage
-
authService
-
-
Constructor Details
-
LoginServlet
public LoginServlet()
-
-
Method Details
-
init
public void init() throws javax.servlet.ServletExceptionInitializes the servlet with database storage and authentication service.- Overrides:
init
in classjavax.servlet.GenericServlet
- Throws:
javax.servlet.ServletException
- if initialization fails
-
doGet
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException Handles GET requests by redirecting to login page.- Overrides:
doGet
in classjavax.servlet.http.HttpServlet
- Parameters:
request
- the HTTP servlet requestresponse
- the HTTP servlet response- Throws:
javax.servlet.ServletException
- if a servlet error occursIOException
- if an I/O error occurs
-
doPost
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException Handles POST requests for login and OTP verification. Routes to appropriate handler based on action parameter.- Overrides:
doPost
in classjavax.servlet.http.HttpServlet
- Parameters:
request
- the HTTP servlet requestresponse
- the HTTP servlet response- Throws:
javax.servlet.ServletException
- if a servlet error occursIOException
- if an I/O error occurs
-
handleLogin
private void handleLogin(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException Handles the initial login attempt with email and password. Generates OTP if credentials are valid.- Parameters:
request
- the HTTP servlet requestresponse
- the HTTP servlet response- Throws:
javax.servlet.ServletException
- if a servlet error occursIOException
- if an I/O error occurs
-
handleOTPVerification
private void handleOTPVerification(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException Handles OTP verification and completes the login process. Creates user session and redirects based on role.- Parameters:
request
- the HTTP servlet requestresponse
- the HTTP servlet response- Throws:
javax.servlet.ServletException
- if a servlet error occursIOException
- if an I/O error occurs
-