Example : How to make the Login Page in a JSP project, and after successful validation, open the Home Page?
STEP 1 : CREATE LOGIN PAGE FIRST (login.jsp)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<style>
body
{
font-family: Arial;
background:#e6f2ff;
}
.loginbox
{
width:350px;
margin:120px auto;
padding:30px;
background:white;
border-radius:10px;
box-shadow:0px 0px 10px gray;
}
h2
{
text-align:center;
color:#003366;
}
input[type=text],
input[type=password]
{
width:100%;
height:40px;
margin-top:10px;
margin-bottom:20px;
padding-left:10px;
font-size:16px;
}
input[type=submit],
input[type=reset]
{
width:100%;
height:40px;
background:#003366;
color:white;
border:none;
font-size:18px;
border-radius:5px;
margin-top: 5px;
}
</style>
</head>
<body>
<div class="loginbox">
<h2>User Login</h2>
<form action="oywfNewLoginCheck.jsp" method="post">
<label>Login ID</label>
<input type="text" name="lid" required>
<label>Password</label>
<input type="password" name="pss" required>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</form>
</div>
</body>
</html>
STEP 2 : CREATE LOGIN PAGE VALIDATION (logincheck.jsp)
<%@page import="java.sql.*"%>
<%
String lid = request.getParameter("lid");
String pss = request.getParameter("pss");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"system",
"raj"
);
String sql = "SELECT * FROM FINALADAM WHERE LID=? AND PSS=?";
ps = con.prepareStatement(sql);
ps.setString(1, lid);
ps.setString(2, pss);
rs = ps.executeQuery();
if(rs.next())
{
session.setAttribute("user", lid);
response.sendRedirect("home.jsp");
}
else
{
out.println("<script>alert('Invalid Login ID or Password'); window.location='login.jsp';</script>");
}
}
catch(Exception e)
{
out.println(e);
}
%>
STEP 3 : CREATE HOME PAGE (home.jsp)
<%
String user = (String)session.getAttribute("user");
if(user == null)
{
response.sendRedirect("login.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome To Home Page</h1>
<h2>
Logged User :
<%=user%>
</h2>
<a href="logout.jsp">Logout</a>
</body>
</html>
STEP 4 : CREATE LOGOUT PAGE (logout.jsp)
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
STEP 5 : CREATE ORACLE 10g TABLE AND FEED DATA INTO IT
CREATE TABLE FINALADAM
(
LID VARCHAR2(50),
PSS VARCHAR2(50)
);
-------------------------------------------
INSERT INTO FINALADAM VALUES('admin','123');
COMMIT;
INSERT INTO FINALADAM VALUES('admin','admin');
COMMIT;
STEP 6 : HOW TO MAKE LOGIN PAGE RUN FIRST IN JSP USING NET BEANS 8.2.
click this link
![]()
0 Comments