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

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.
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...
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...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT