Question

In: Computer Science

Write a JAVA program by making a LOGIN form using Excel file for validation. if user...

Write a JAVA program by making a LOGIN form using Excel file for validation. if user is found, display the user's First Name and Last Name.


Solutions

Expert Solution

Table of content:

1. Create database table used for authentication

2. The User model class

3. Code Check Login method

4. Code Login Page

5. Code Login Servlet Class

first we need to write login.html form:

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Bookshop Website</title>

</head>

<body>

    <div style="text-align: center">

        <h1>Admin Login</h1>

        <form action="login" method="post">

            <label for="email">Email:</label>

            <input name="email" size="30" />

            <br><br>

            <label for="password">Password:</label>

            <input type="password" name="password" size="30" />

            <br>${message}

            <br><br>           

            <button type="submit">Login</button>

        </form>

    </div>

</body>

</html>

this program will show as bellow daigram

this is login code

in the login.html we gave action =login so that will be redirected to this logic

package net.codejava;

import java.io.*;

import java.sql.SQLException;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.*;

@WebServlet("/login")

public class UserLoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public UserLoginServlet() {

        super();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String email = request.getParameter("email");

        String password = request.getParameter("password");

        

        UserDAO userDao = new UserDAO();

        

        try {

            User user = userDao.checkLogin(email, password);

            String destPage = "login.jsp";

            

            if (user != null) {

                HttpSession session = request.getSession();

                session.setAttribute("user", user);

                destPage = "home.jsp";

            } else {

                String message = "Invalid email/password";

                request.setAttribute("message", message);

            }

            

            RequestDispatcher dispatcher = request.getRequestDispatcher(destPage);

            dispatcher.forward(request, response);

            

        } catch (SQLException | ClassNotFoundException ex) {

            throw new ServletException(ex);

        }

    }

}

and we need mysql for validation

mySql code

CREATE TABLE `users` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `email` varchar(45) NOT NULL,

  `password` varchar(45) NOT NULL,

  `fullname` varchar(45) NOT NULL,

  PRIMARY KEY (`id`)

)

example table:

this is user model class:

package net.codejava;

public class User {
private int id;
private String fullname;
private String email;
private String password;
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getFullname() {
       return fullname;
   }
   public void setFullname(String fullname) {
       this.fullname = fullname;
   }
   public String getEmail() {
       return email;
   }
   public void setEmail(String email) {
       this.email = email;
   }
   public String getPassword() {
       return password;
   }
   public void setPassword(String password) {
       this.password = password;
   }


}


Related Solutions

Create a C++ login program using file for 2 user. Usernames must be in one file...
Create a C++ login program using file for 2 user. Usernames must be in one file and password in the other file. Ask user for their usernames and password if it matches, they logged in successfully. If it doesn’t match,they have 3 trials ,then they have to sign in and create a username and password. After creating a username and password, they have to go to the login page again. Must work for visual studio code
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
Using Python programming, make a form that allows user to login to a modest web application...
Using Python programming, make a form that allows user to login to a modest web application that has a password and username with a file that has validated user. Once logged in, a formal greeting and the choice to change password should be available. The password should be strong(NIST SP 800-63B). All failed logins should be logged w/ date and Time. (Ex. 6 failed logins in 20 min dated 12 Jan 2020.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT