jfsd skill 5 question 2

Published on August 29, 2021
Last updated September 18, 2021

Write a java program/JSP to connect to the database and extract data from the tables and display them using various SQL queries. Insert the details of the users who register with the website, to the database. and authenticate the user when he/she submits the login form using the username and password from the database using JSP and give the user authority to change his/her password.

Video explaining the code and process in telugu language

create login table in database

CREATE TABLE skill5login(
    username VARCHAR(26) NOT NULL, 
    password VARCHAR(26) NOT NULL);

Insert data into the table

INSERT INTO skill5login (username, password) VALUES ('nikhil', 'nikhil');

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login page</title>
</head>
<body>
<form action="handleLogin.jsp" method="post">  
Email:<input type="text" name="username"/><br/><br/>  
Password:<input type="password" name="password"/><br/><br/>  
<input type="submit" value="login"/>
<a href="./signup.jsp">sign up</a>
</form>
</body>
</html>

handleLogin.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
	String username = request.getParameter("username");
	String password = request.getParameter("password");

	Class.forName("oracle.jdbc.driver.OracleDriver");

	String dbUserName = "system";

	String dbUserPassword = "nikhil4u";

	Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", dbUserName, dbUserPassword);

	Statement stmt = con.createStatement();

	String check_login_query = "SELECT username FROM skill5login where username=? and password=?";
	
	PreparedStatement pstmt = con.prepareStatement(check_login_query);
	
	pstmt.setString(1, username);
	pstmt.setString(2, password);
	
	ResultSet rs = pstmt.executeQuery();
	
	boolean status = rs.next();
	
	if(status){
		session.setAttribute("username", username);
		response.sendRedirect("changePassword.jsp");
	} else{
		out.print("Sorry, email or password error");
	}
	%>

</body>
</html>

signup.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Sign up page</title>
</head>
<body>
	<form action="handleSignup.jsp" method="post">
		Email:<input type="text" name="username" /><br />
		<br /> Password:<input type="password" name="password" /><br />
		<br /> <input type="submit" value="signup" /> <a href="./">login</a>
	</form>
</body>
</html>

handleSignup.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
	String username = request.getParameter("username");
	String password = request.getParameter("password");

	Class.forName("oracle.jdbc.driver.OracleDriver");

	String dbUserName = "system";

	String dbUserPassword = "nikhil4u";

	Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", dbUserName, dbUserPassword);

	Statement stmt = con.createStatement();

	String create_user_query = "INSERT INTO skill5login (username, password) VALUES (?, ?)";

	PreparedStatement pstmt = con.prepareStatement(create_user_query);

	pstmt.setString(1, username);
	pstmt.setString(2, password);

	ResultSet rs = pstmt.executeQuery();

	boolean status = rs.next();

	if (status) {
		session.setAttribute("username", username);
		response.sendRedirect("changePassword.jsp");
	} else {
		out.print("Sorry, email or password error");
	}
	%>

</body>
</html>

changePassword.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">
<title>Home page</title>
</head>

<body>

	<%
	response.setHeader("Cache-Control", "no-store, max-age=0");
	if (session.getAttribute("username") == null) {
		response.sendRedirect("skill5q2.jsp");
	}
	%>
	<h1>
		Welcome
		<%
	if (session.getAttribute("username") != null) {
		String username = session.getAttribute("username").toString();
		out.println(username);
	}
	%>
	</h1>
	<br>
	<h2>You have logged in successfully</h2>
	<br>
	<br>
	<h2>Change your password</h2>
	<form action="handlePasswordChange.jsp" method="post">
		New Password:<input type="password" name="password" /><br />
		<br /> <input type="submit" value="changePassword" />
	</form>
</body>

</html>

handlePasswordChange.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Change Password</title>
</head>
<body>
	<%
	response.setHeader("Cache-Control", "no-store, max-age=0");
	if (session.getAttribute("username") == null) {
		response.sendRedirect("skill5q2.jsp");
	}
	if (session.getAttribute("username") != null) {
		String username = session.getAttribute("username").toString();

		String password = request.getParameter("password");

		Class.forName("oracle.jdbc.driver.OracleDriver");

		String dbUserName = "system";

		String dbUserPassword = "nikhil4u";

		Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", dbUserName, dbUserPassword);

		Statement stmt = con.createStatement();

		String change_password_query = "UPDATE skill5login SET password=? where username=?";

		PreparedStatement pstmt = con.prepareStatement(change_password_query);

		pstmt.setString(1, password);
		pstmt.setString(2, username);

		int i = pstmt.executeUpdate();
		out.print(i + " records changed");

	}
	%>

</body>
</html>


Tags :