5.2 Develop a simple JSP program for user login form with static and dynamic database 

Static

form-static.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Static</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="static-action.jsp" method="post">
Username: <input type="text" name="name"/><br/>
Password: <input type="password" name="pass"/> <br/>
<button type="submit">Login</button>
</form>
</body>
</html>

static-action.jsp
<%--
Document : static-action
Created on : 27-Oct-2021, 10:48:28 AM
Author : arpit
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Static Action</title>
</head>
<body>
<%
String name = request.getParameter("name");
String pass = request.getParameter("pass");
if(name.equals("tony") && pass.equals("loveyou3000")){
out.print("User is Valid");
}
else{
out.print("User is Invalid");
}
%>
</body>
</html>

Output
form

action page

Dynamic / with database

make table


insert the following value

INSERT INTO `login`(`username`, `pass`) VALUES ('tony','loveyou3000')
INSERT INTO `login`(`username`, `pass`) VALUES ('natasha','nat3000')

Now add mysql-connector jar to the library

form-dynamic.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Dynamic</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="dynamic-action.jsp" method="post">
Username: <input type="text" name="name"/><br/>
Password: <input type="password" name="pass"/> <br/>
<button type="submit">Login</button>
</form>
</body>
</html>

dynamic-action.jsp
<%--
Document : dynamic-action
Created on : 27-Oct-2021, 11:04:11 AM
Author : arpit
--%>

<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dynamic</title>
</head>
<body>
<%
String name = request.getParameter("name");
String pass = request.getParameter("pass");
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");

PreparedStatement pst = con.prepareStatement("select username, pass from login where username=? and pass=?");

pst.setString(1, name);
pst.setString(2, pass);

ResultSet rs = pst.executeQuery();

if(rs.next()){
out.print("Valid User");
}
else{
out.print("Invalid User");
}
}
catch(Exception e){
out.print(e);
}
%>
</body>
</html>

Output

form

action page


Happy Coding :)