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...
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 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.
write a c# program that: The file is using a very simple form of compression in...
write a c# program that: The file is using a very simple form of compression in which there are no spaces, each word is separated by camel casing. For Example, the string "TheCatWillRun" is parsed as "The Cat Will Run". *Now for the statistics* Prints to the console the following statistics about the content in the file retrieved above. - How many of each letter are in the file - How many letters are capitalized in the file - The...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
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
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT