In: Computer Science
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.
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;
}
}