Online voting system project in java with source code using JSP and Servlet. This project is designed to automate the voting system. where multiple parties or teams can be involved in choosing the leader.
The complete voting management project including the admin and user side is available with source code, project report, and configuration on your machine.
In the voting system, there are two types of applications users Admin and Voter, where Admin will have the main control over the system.
As technology in the front end, we have used HTML, CSS, and Bootstrap. At the data access layer, we have used JDBC API. The Database used here is MYSQL. The whole project is following the MVC (Model View & Controller) design pattern.
Voting System Abstract
This voting system is an excellent solution for any kind of voting process. where the admin can add multiple parties and voters can choose their candidate. Admin can analyze the results and declare a winning candidate.
The voting system is dedicated to selecting a candidate from the list, electing, or determining. The main goal of voting is to come up with leaders of the people’s choice.
In most countries and cities voting is a manual process, Some of the problems involved include ridging votes during elections, insecure or inaccessible polling stations, inadequate polling materials, and also inexperienced personnel.
This online voting/polling system seeks to address the above issues. It should be noted that with this system in place, the users, and citizens, in this case, shall be given ample time during the voting period. They shall also be trained on how to vote online before the election time.
It’s a secure platform, to make it secure and fair it’s using a unique voter id so single users and voters uniquely also can vote only once for each election. It is a web application, and users can access it by using a secure and smooth browser.
Different modules and user roles of the voting system project
Let’s talk more about the feature, functionalities, and user roles in the voting system project. Admin is the driver of this application because admin is the only user who will manage all the voter’s and parties’ activities.
Modules in Voting System Project
Login and Registration
Yes, Users or voters can Register for the application and maintain the profile. where the admin can only log in and manage the other users.
Voting
This is the user module where voters can see their listed elections and parties as per the election so they can vote which is analyzed by the admin user.
Voting Result analysis
Admin will view the complete voting list and find the winner that is just manual verification because the application is using an internal algorithm to find the winner of the election.
Parties
Parties are another indirect user of the application that will be managed by the admin.
Admin User
Admin can add, edit, delete and view the voters and parties. also, the admin can view the elections list and can manage their own profile.
Voters
A voter can register into the application and login in the application, After login users can view the parties and vote for their candidate, and also voters can view the live winner as election results.
Tools and Technologies
Technology/Domain:Â Java
Front-End:Â JSP, Html, CSS, JS, Bootstrap.
Server-side:Â Servlet.
Back-end:Â MYSQL.
Server:Â Tomcat 8.5.
Contact to get Source code
Skype Id: jcodebun
Email: jcodebun@gmail.com
WhatsApp: +91 8827363777
Price: 999Â INR
Note: If you need the source code you can contact Us. We will provide complete source code and all the required things like Database and project reports with all the diagrams. Also, we have created a STEP-by-STEP configuration tutorial to help you in the configuration process.
If you find any kind of difficulties during the configuration, we will provide a complete project configuration guide remotely using any Desk or Zoom.
Data Flow diagram(DFD) for online voting system
DFD for admin
DFD for Voter
Activity diagrams for Voting System
Below, is the sample Source code for the Online voting System. Below is the source code for the complete source code of the user module of the library management project in Java.
User Bean for voting project
package in.co.online.vote.bean; import java.sql.Timestamp; import java.util.Date; /** * User JavaBean encapsulates TimeTable attributes * * @author Navigable Set * @version 1.0 * @Copyright (c) Navigable Set * */ public class UserBean extends BaseBean { private String firstName; /** * Last Name of User */ private String lastName; /** * Login of User */ private String login; /** * Password of User */ private String password; /** * Confirm Password of User */ private String confirmPassword; /** * Date of Birth of User */ private Date dob; /** * MobielNo of User */ private String mobileNo; /** * Role of User */ private long roleId; /** * Number of unsuccessful login attempt */ private String gender; private String voterId; /** * Last login timestamp */ /** * accessor */ public String getVoterId() { return voterId; } public void setVoterId(String voterId) { this.voterId = voterId; } /** * @return FirstName Of User */ public String getFirstName() { return firstName; } /** * @param FirstName * To set User FirstName */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return LastName Of User */ public String getLastName() { return lastName; } /** * @param LastName * To set User LastName */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return Login id Of User */ public String getLogin() { return login; } /** * @param Login * Id To set User Login ID */ public void setLogin(String login) { this.login = login; } /** * @return Password Of User */ public String getPassword() { return password; } /** * @param Password * To set User Password */ public void setPassword(String password) { this.password = password; } /** * @return Confirm Password Of User */ public String getConfirmPassword() { return confirmPassword; } /** * @param Confirm * PAssword To set User Confirm Password */ public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; } /** * @return Date Of Birth Of User */ public Date getDob() { return dob; } /** * @param Date * Of Birth To set User Date Of Birth */ public void setDob(Date dob) { this.dob = dob; } /** * @return Mobile No Of User */ public String getMobileNo() { return mobileNo; } /** * @param Mobile * No To set User Mobile No */ public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } /** * @return ROle Id Of User */ public long getRoleId() { return roleId; } /** * @param Role * Id To set User ROle Id */ public void setRoleId(long roleId) { this.roleId = roleId; } /** * @return unSuccessfulLogin Of User /** * @return unSuccessfulLogin Of User */ public String getGender() { return gender; } /** * @param Gender * To set User Gender */ public void setGender(String gender) { this.gender = gender; } public String getKey() { return id + ""; } public String getValue() { return firstName + " " + lastName; } }
User controller(Servlet)
package in.co.online.vote.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import in.co.online.vote.bean.BaseBean; import in.co.online.vote.bean.UserBean; import in.co.online.vote.bean.VoteBean; import in.co.online.vote.exception.ApplicationException; import in.co.online.vote.model.UserModel; import in.co.online.vote.model.VotePartiesModel; import in.co.online.vote.util.DataUtility; import in.co.online.vote.util.DataValidator; import in.co.online.vote.util.PropertyReader; import in.co.online.vote.util.ServletUtility; /** * Servlet implementation class UserLoginCtl */ @WebServlet(name = "UserLoginCtl", urlPatterns = { "/UserLoginCtl" }) public class UserLoginCtl extends BaseCtl { private static final long serialVersionUID = 1L; public static final String OP_REGISTER = "Register"; public static final String OP_SIGN_IN = "SignIn"; public static final String OP_SUBMIT = "Submit"; public static final String OP_SIGN_UP = "SignUp"; public static final String OP_LOG_OUT = "logout"; public static String HIT_URI = null; private static Logger log = Logger.getLogger(UserLoginCtl.class); @Override protected boolean validate(HttpServletRequest request) { log.debug("UserLoginCtl Method validate Started"); boolean pass = true; String op = request.getParameter("operation"); if (OP_SIGN_UP.equals(op) || OP_LOG_OUT.equals(op)) { return pass; } String login = request.getParameter("voterId"); if (DataValidator.isNull(login)) { request.setAttribute("voterId", PropertyReader.getValue("error.require", "Voter Id")); pass = false; } log.debug("UserLoginCtl Method validate Ended"); return pass; } /** * Populates bean object from request parameters * * @param request * @return */ @Override protected BaseBean populateBean(HttpServletRequest request) { log.debug("UserLoginCtl Method populateBean Started"); UserBean bean = new UserBean(); bean.setId(DataUtility.getLong(request.getParameter("id"))); bean.setVoterId(DataUtility.getString(request.getParameter("voterId"))); log.debug("UserLoginCtl Method PopulatedBean End"); return bean; } /** * Display Login form * */ /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug("Method doGet Started"); HttpSession session = request.getSession(true); String op = DataUtility.getString(request.getParameter("operation")); UserModel model = new UserModel(); long id = DataUtility.getLong(request.getParameter("id")); if (id > 0) { UserBean userBean; try { userBean = model.findByPK(id); ServletUtility.setBean(userBean, request); } catch (Exception e) { log.error(e); ServletUtility.handleException(e, request, response); return; } } else if (OP_LOG_OUT.equals(op)) { session = request.getSession(false); session.invalidate(); ServletUtility.setSuccessMessage("You have been logged out successfully", request); ServletUtility.forward(OVSView.USER_LOGIN_VIEW, request, response); return; } if (session.getAttribute("user") != null) { ServletUtility.redirect(OVSView.WELCOME_CTL, request, response); return; } ServletUtility.forward(getView(), request, response); log.debug("Method doGet end"); } /** * Submit Logic */ /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug(" UserLoginCtl Method doPost Started"); HttpSession session = request.getSession(true); String op = DataUtility.getString(request.getParameter("operation")); // get Model UserModel model = new UserModel(); long id = DataUtility.getLong(request.getParameter("id")); if (OP_SUBMIT.equalsIgnoreCase(op)) { UserBean bean = (UserBean) populateBean(request); try { VotePartiesModel vModel=new VotePartiesModel(); VoteBean vBean=vModel.findByVoteId(bean.getVoterId()); if(vBean==null) { System.out.println("Voter Id==="+bean.getVoterId()); bean = model.findByLogin(bean.getVoterId()); if (bean != null) { session.setAttribute("user", bean); session.setMaxInactiveInterval(10 * 6000); long rollId = bean.getRoleId(); // save state of session remember URL String uri = request.getParameter("uri"); if (uri == null || "null".equalsIgnoreCase(uri)) { ServletUtility.redirect(OVSView.OTP_CTL, request, response); return; } else { ServletUtility.redirect(uri, request, response); } return; } else { bean = (UserBean) populateBean(request); ServletUtility.setBean(bean, request); ServletUtility.setErrorMessage("Invalid Voter ID", request); } }else { ServletUtility.setBean(bean, request); ServletUtility.setErrorMessage("You Have Already Vote", request); } } catch (ApplicationException e) { log.error(e); ServletUtility.handleException(e, request, response); return; } } else if (OP_SIGN_UP.equalsIgnoreCase(op)) { ServletUtility.redirect(OVSView.USER_REGISTRATION_CTL, request, response); return; } ServletUtility.forward(getView(), request, response); log.debug("UserLoginCtl Method doPost Ended"); } /** * Returns the VIEW page of this Controller * * @return */ @Override protected String getView() { return OVSView.USER_LOGIN_VIEW; } }
UserModel(Database transections)
package in.co.online.vote.model; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.log4j.Logger; import in.co.online.vote.bean.UserBean; import in.co.online.vote.exception.ApplicationException; import in.co.online.vote.exception.DatabaseException; import in.co.online.vote.exception.DuplicateRecordException; import in.co.online.vote.exception.RecordNotFoundException; import in.co.online.vote.util.EmailBuilder; import in.co.online.vote.util.EmailMessage; import in.co.online.vote.util.EmailUtility; import in.co.online.vote.util.JDBCDataSource; /** * JDBC Implementation of UserModel * * @author Navigable Set * @version 1.0 * @Copyright (c) Navigable Set */ public class UserModel { private static Logger log = Logger.getLogger(UserModel.class); /** * Find next PK of user * * @throws DatabaseException */ private long roleId; /** * @return the roleId */ public long getRoleId() { return roleId; } /** * @param roleId * the roleId to set */ public void setRoleId(long roleId) { this.roleId = roleId; } public Integer nextPK() throws DatabaseException { log.debug("Model nextPK Started"); Connection conn = null; int pk = 0; try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement("SELECT MAX(ID) FROM V_USER"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { pk = rs.getInt(1); } rs.close(); } catch (Exception e) { log.error("Database Exception..", e); throw new DatabaseException("Exception : Exception in getting PK"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model nextPK End"); return pk + 1; } /** * Add a User * * @param bean * @throws DatabaseException * */ public long add(UserBean bean) throws ApplicationException, DuplicateRecordException { Connection conn = null; int pk = 0; UserBean existbean = findByLogin(bean.getVoterId()); if (existbean != null) { throw new DuplicateRecordException("Voter Id already exists"); } try { conn = JDBCDataSource.getConnection(); pk = nextPK(); // Get auto-generated next primary key System.out.println(pk + " in ModelJDBC"); conn.setAutoCommit(false); // Begin transaction PreparedStatement pstmt = conn.prepareStatement("INSERT INTO V_USER VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); pstmt.setInt(1, pk); pstmt.setString(2, bean.getFirstName()); pstmt.setString(3, bean.getLastName()); pstmt.setString(4, bean.getLogin()); pstmt.setString(5, bean.getPassword()); pstmt.setDate(6, new java.sql.Date(bean.getDob().getTime())); pstmt.setString(7, bean.getMobileNo()); pstmt.setLong(8, bean.getRoleId()); pstmt.setString(9, bean.getGender()); pstmt.setString(10,bean.getVoterId()); pstmt.setString(11, bean.getCreatedBy()); pstmt.setString(12, bean.getModifiedBy()); pstmt.setTimestamp(13, bean.getCreatedDatetime()); pstmt.setTimestamp(14, bean.getModifiedDatetime()); pstmt.executeUpdate(); conn.commit(); // End transaction pstmt.close(); } catch (Exception e) { try { conn.rollback(); } catch (Exception ex) { ex.printStackTrace(); throw new ApplicationException("Exception : add rollback exception " + ex.getMessage()); } throw new ApplicationException("Exception : Exception in add User"); } finally { JDBCDataSource.closeConnection(conn); } return pk; } /** * Delete a User * * @param bean * @throws DatabaseException */ public void delete(UserBean bean) throws ApplicationException { Connection conn = null; try { conn = JDBCDataSource.getConnection(); conn.setAutoCommit(false); // Begin transaction PreparedStatement pstmt = conn.prepareStatement("DELETE FROM V_USER WHERE ID=?"); pstmt.setLong(1, bean.getId()); pstmt.executeUpdate(); conn.commit(); // End transaction pstmt.close(); } catch (Exception e) { try { conn.rollback(); } catch (Exception ex) { throw new ApplicationException("Exception : Delete rollback exception " + ex.getMessage()); } throw new ApplicationException("Exception : Exception in delete User"); } finally { JDBCDataSource.closeConnection(conn); } } /** * Find User by Login * * @param login * : get parameter * @return bean * @throws DatabaseException */ public UserBean findByLogin(String voterId) throws ApplicationException { log.debug("Model findByLogin Started"); StringBuffer sql = new StringBuffer("SELECT * FROM V_USER WHERE voterId=?"); UserBean bean = null; Connection conn = null; System.out.println("sql" + sql); try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); pstmt.setString(1, voterId); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { bean = new UserBean(); bean.setId(rs.getLong(1)); bean.setFirstName(rs.getString(2)); bean.setLastName(rs.getString(3)); bean.setLogin(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setDob(rs.getDate(6)); bean.setMobileNo(rs.getString(7)); bean.setRoleId(rs.getLong(8)); bean.setGender(rs.getString(9)); bean.setVoterId(rs.getString(10)); bean.setCreatedBy(rs.getString(11)); bean.setModifiedBy(rs.getString(12)); bean.setCreatedDatetime(rs.getTimestamp(13)); bean.setModifiedDatetime(rs.getTimestamp(14)); } rs.close(); } catch (Exception e) { e.printStackTrace(); log.error("Database Exception..", e); throw new ApplicationException("Exception : Exception in getting User by login"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model findByLogin End"); return bean; } /** * Find User by PK * * @param pk * : get parameter * @return bean * @throws DatabaseException */ public UserBean findByPK(long pk) throws ApplicationException { log.debug("Model findByPK Started"); StringBuffer sql = new StringBuffer("SELECT * FROM V_USER WHERE ID=?"); UserBean bean = null; Connection conn = null; try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); pstmt.setLong(1, pk); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { bean = new UserBean(); bean.setId(rs.getLong(1)); bean.setFirstName(rs.getString(2)); bean.setLastName(rs.getString(3)); bean.setLogin(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setDob(rs.getDate(6)); bean.setMobileNo(rs.getString(7)); bean.setRoleId(rs.getLong(8)); bean.setGender(rs.getString(9)); bean.setVoterId(rs.getString(10)); bean.setCreatedBy(rs.getString(11)); bean.setModifiedBy(rs.getString(12)); bean.setCreatedDatetime(rs.getTimestamp(13)); bean.setModifiedDatetime(rs.getTimestamp(14)); } rs.close(); } catch (Exception e) { e.printStackTrace(); log.error("Database Exception..", e); throw new ApplicationException("Exception : Exception in getting User by pk"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model findByPK End"); return bean; } /** * Update a user * * @param bean * @throws DatabaseException */ public void update(UserBean bean) throws ApplicationException, DuplicateRecordException { log.debug("Model update Started"); Connection conn = null; UserBean beanExist = findByLogin(bean.getLogin()); // Check if updated LoginId already exist if (beanExist != null && !(beanExist.getId() == bean.getId())) { throw new DuplicateRecordException("LoginId is already exist"); } try { conn = JDBCDataSource.getConnection(); conn.setAutoCommit(false); // Begin transaction PreparedStatement pstmt = conn.prepareStatement( "UPDATE V_USER SET FIRSTNAME=?,LASTNAME=?,LOGIN=?,PASSWORD=?,DOB=?,MOBILENO=?,ROLEID=?," + "GENDER=?,voterId=?" + "CREATEDBY=?,MODIFIEDBY=?,CREATEDDATETIME=?,MODIFIEDDATETIME=? WHERE ID=?"); pstmt.setString(1, bean.getFirstName()); pstmt.setString(2, bean.getLastName()); pstmt.setString(3, bean.getLogin()); pstmt.setString(4, bean.getPassword()); pstmt.setDate(5, new java.sql.Date(bean.getDob().getTime()) ); pstmt.setString(6, bean.getMobileNo()); pstmt.setLong(7, bean.getRoleId()); pstmt.setString(8, bean.getGender()); pstmt.setString(9, bean.getVoterId()); pstmt.setString(10, bean.getCreatedBy()); pstmt.setString(11, bean.getModifiedBy()); pstmt.setTimestamp(12, bean.getCreatedDatetime()); pstmt.setTimestamp(13, bean.getModifiedDatetime()); pstmt.setLong(14, bean.getId()); pstmt.executeUpdate(); conn.commit(); // End transaction pstmt.close(); } catch (Exception e) { e.printStackTrace(); log.error("Database Exception..", e); try { conn.rollback(); } catch (Exception ex) { throw new ApplicationException("Exception : Delete rollback exception " + ex.getMessage()); } throw new ApplicationException("Exception in updating User "); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model update End"); } /** * Search User * * @param bean * : Search Parameters * @throws DatabaseException */ public List search(UserBean bean) throws ApplicationException { return search(bean, 0, 0); } /** * Search User with pagination * * @return list : List of Users * @param bean * : Search Parameters * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * * @throws DatabaseException */ public List search(UserBean bean, int pageNo, int pageSize) throws ApplicationException { log.debug("Model search Started"); StringBuffer sql = new StringBuffer("SELECT * FROM V_USER WHERE 1=1"); if (bean != null) { if (bean.getId() > 0) { sql.append(" AND id = " + bean.getId()); } if (bean.getFirstName() != null && bean.getFirstName().length() > 0) { sql.append(" AND FIRSTNAME like '" + bean.getFirstName() + "%'"); } if (bean.getLastName() != null && bean.getLastName().length() > 0) { sql.append(" AND LASTNAME like '" + bean.getLastName() + "%'"); } if (bean.getLogin() != null && bean.getLogin().length() > 0) { sql.append(" AND LOGIN like '" + bean.getLogin() + "%'"); } if (bean.getPassword() != null && bean.getPassword().length() > 0) { sql.append(" AND PASSWORD like '" + bean.getPassword() + "%'"); } if (bean.getDob() != null && bean.getDob().getDate() > 0) { sql.append(" AND DOB = " + bean.getGender()); } if (bean.getMobileNo() != null && bean.getMobileNo().length() > 0) { sql.append(" AND MOBILENO = " + bean.getMobileNo()); } if (bean.getRoleId() > 0) { sql.append(" AND ROLEID = " + bean.getRoleId()); } } // if page size is greater than zero then apply pagination if (pageSize > 0) { // Calculate start record index pageNo = (pageNo - 1) * pageSize; sql.append(" Limit " + pageNo + ", " + pageSize); // sql.append(" limit " + pageNo + "," + pageSize); } System.out.println("user model search :"+sql); ArrayList list = new ArrayList(); Connection conn = null; try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { bean = new UserBean(); bean.setId(rs.getLong(1)); bean.setFirstName(rs.getString(2)); bean.setLastName(rs.getString(3)); bean.setLogin(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setDob(rs.getDate(6)); bean.setMobileNo(rs.getString(7)); bean.setRoleId(rs.getLong(8)); bean.setGender(rs.getString(9)); bean.setVoterId(rs.getString(10)); bean.setCreatedBy(rs.getString(11)); bean.setModifiedBy(rs.getString(12)); bean.setCreatedDatetime(rs.getTimestamp(13)); bean.setModifiedDatetime(rs.getTimestamp(14)); list.add(bean); } rs.close(); } catch (Exception e) { log.error("Database Exception..", e); throw new ApplicationException("Exception : Exception in search user"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model search End"); return list; } /** * Get List of User * * @return list : List of User * @throws DatabaseException */ public List list() throws ApplicationException { return list(0, 0); } /** * Get List of User with pagination * * @return list : List of users * @param pageNo * : Current Page No. * @param pageSize * : Size of Page * @throws DatabaseException */ public List list(int pageNo, int pageSize) throws ApplicationException { log.debug("Model list Started"); ArrayList list = new ArrayList(); StringBuffer sql = new StringBuffer("select * from V_USER"); // if page size is greater than zero then apply pagination if (pageSize > 0) { // Calculate start record index pageNo = (pageNo - 1) * pageSize; sql.append(" limit " + pageNo + "," + pageSize); } System.out.println("sql in list user :"+sql); Connection conn = null; try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { UserBean bean = new UserBean(); bean.setId(rs.getLong(1)); bean.setFirstName(rs.getString(2)); bean.setLastName(rs.getString(3)); bean.setLogin(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setDob(rs.getDate(6)); bean.setMobileNo(rs.getString(7)); bean.setRoleId(rs.getLong(8)); bean.setGender(rs.getString(9)); bean.setVoterId(rs.getString(10)); bean.setCreatedBy(rs.getString(11)); bean.setModifiedBy(rs.getString(12)); bean.setCreatedDatetime(rs.getTimestamp(13)); bean.setModifiedDatetime(rs.getTimestamp(14)); list.add(bean); } rs.close(); } catch (Exception e) { log.error("Database Exception..", e); throw new ApplicationException("Exception : Exception in getting list of users"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model list End"); return list; } /** * @param id * : long id * @param old * password : String oldPassword * @param new * password : String newPassword * @throws DatabaseException */ public UserBean authenticate(String login, String password) throws ApplicationException { log.debug("Model authenticate Started"); StringBuffer sql = new StringBuffer("SELECT * FROM V_USER WHERE LOGIN = ? AND PASSWORD = ?"); UserBean bean = null; Connection conn = null; try { conn = JDBCDataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); pstmt.setString(1, login); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { bean = new UserBean(); bean.setId(rs.getLong(1)); bean.setFirstName(rs.getString(2)); bean.setLastName(rs.getString(3)); bean.setLogin(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setDob(rs.getDate(6)); bean.setMobileNo(rs.getString(7)); bean.setRoleId(rs.getLong(8)); bean.setGender(rs.getString(9)); bean.setVoterId(rs.getString(10)); bean.setCreatedBy(rs.getString(11)); bean.setModifiedBy(rs.getString(12)); bean.setCreatedDatetime(rs.getTimestamp(13)); bean.setModifiedDatetime(rs.getTimestamp(14)); System.out.println("Usermodel here"); } } catch (Exception e) { log.error("Database Exception..", e); throw new ApplicationException("Exception : Exception in get roles"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model authenticate End"); return bean; } public long registerUser(UserBean bean) throws ApplicationException, DuplicateRecordException { log.debug("Model add Started"); long pk = add(bean); HashMap<String, String> map = new HashMap<String, String>(); map.put("login", bean.getLogin()); map.put("password", bean.getPassword()); String message = EmailBuilder.getUserRegistrationMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(bean.getLogin()); msg.setSubject("Registration is successful for Job Portal"); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); try { EmailUtility.sendMail(msg); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return pk; } /** * Reset Password of User with auto generated Password * * @return boolean : true if success otherwise false * @param login * : User Login * @throws ApplicationException * @throws org.omg.CORBA.portable.ApplicationException * @throws RecordNotFoundException * : if user not found */ /*public boolean resetPassword(UserBean bean) throws ApplicationException, org.omg.CORBA.portable.ApplicationException { String newPassword = String.valueOf(new Date().getTime()).substring(0, 4); UserBean userData = findByPK(bean.getId()); userData.setPassword(newPassword); try { update(userData); } catch (DuplicateRecordException e) { return false; } HashMap<String, String> map = new HashMap<String, String>(); map.put("login", bean.getLogin()); map.put("password", bean.getPassword()); map.put("firstName", bean.getFirstName()); map.put("lastName", bean.getLastName()); String message = EmailBuilder.getForgetPasswordMessage(map); EmailMessage msg = new EmailMessage(); msg.setTo(bean.getLogin()); msg.setSubject("Password has been reset"); msg.setMessage(message); msg.setMessageType(EmailMessage.HTML_MSG); EmailUtility.sendMail(msg); return true; }*/ }
User login page
<%@page import="in.co.online.vote.controller.UserLoginCtl"%> <%@page import="in.co.online.vote.util.DataUtility"%> <%@page import="in.co.online.vote.util.ServletUtility"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%@ include file="../jsp/Header.jsp"%> <main class="login-form"> <div class="cotainer"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> User Login <h6 style="color: red;"><%=ServletUtility.getErrorMessage(request)%></h6> <h6 style="color: green;"><%=ServletUtility.getSuccessMessage(request)%></h6> </div> <div class="card-body"> <form action="<%=OVSView.USER_LOGIN_CTL%>" method="post"> <jsp:useBean id="bean" class="in.co.online.vote.bean.UserBean" scope="request"></jsp:useBean> <% String uri = (String) request.getAttribute("uri"); %> <input type="hidden" name="uri" value="<%=uri%>"> <input type="hidden" name="id" value="<%=bean.getId()%>"> <input type="hidden" name="createdBy" value="<%=bean.getCreatedBy()%>"> <input type="hidden" name="modifiedBy" value="<%=bean.getModifiedBy()%>"> <input type="hidden" name="createdDatetime" value="<%=DataUtility.getTimestamp(bean.getCreatedDatetime())%>"> <input type="hidden" name="modifiedDatetime" value="<%=DataUtility.getTimestamp(bean.getModifiedDatetime())%>"> <div class="form-group row"> <label for="email_address" class="col-md-4 col-form-label text-md-right">Voter Id<font color="red">*</font></label> <div class="col-md-6"> <input type="text" id="email_address" class="form-control" placeholder="Enter Voter Id" name="voterId" value="<%=DataUtility.getStringData(bean.getLogin())%>" > <font color="red"><%=ServletUtility.getErrorMessage("voterId", request)%></font> </div> </div> <div class="col-md-6 offset-md-4"> <input type="submit" class="btn btn-primary" name="operation" value="<%=UserLoginCtl.OP_SUBMIT%>"> </div> </form> </div> </div> </div> </div> </div> </main> <div style="margin-top: 224px"> <%@ include file="../jsp/Footer.jsp"%> </div> </body> </html>