Professional Documents
Culture Documents
JSP and Ajax 2
JSP and Ajax 2
Aim:
Program to develop a Library Management System application accessing a database using JSP.
Procedure:
Relations using MYSQL given below enforcing primary key and foreign keyconstraints: BOOK
BRANCH)
1. Open MySQL.
2. Create a database.
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Library Management System</title> </head>
<body>
<h1 align="center">Library Management System</h1> <hr />
<a href="Book.html">Book Details</a> </body>
</html>
Book.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Book Details</title>
</head>
<body>
<h1 align="center">Book Details</h1> <hr />
<form action="AddBook.jsp" method="post"> <table>
<tr>
<td>Acc. No. :</td> <td><input type="text" name="accno"></td> </tr>
<tr>
<td>Title :</td> <td><input type="text" name="title"></td> </tr>
<tr>
<td>Author :</td> <td><input type="text" name="author"></td> </tr>
<tr>
<td>Publisher :</td> <td><input type="text" name="publisher"></td> </tr>
<tr>
<td>Edition :</td> <td><input type="text" name="edition"></td> </tr>
<tr>
<td>Price :</td> <td><input type="text" name="price"></td> </tr>
<tr>
<td colspan="2" align="center"><inputtype="submit" value="Add Book"></td> </tr>
</table>
<br /> <a href="ViewBooks.jsp">View All Books</a>
</form>
</body>
</html>
AddBook.jsp
ViewBooks.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-
8859-1" import="java.sql.*"%> <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>View All Book Details</title>
</head>
<body>
<h1 align="center">All Book Details</h1> <hr />
<%
Connection conn = null;
PreparedStatement ps = null; ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String URL = "jdbc:mysql://localhost:3306/library"; conn = DriverManager.getConnection(URL,
"root", "admin"); ps = conn.prepareStatement("select * from book order by accno");
rs = ps.executeQuery(); %>
<table border="1">
<tr>
<td>Acc No.</td>
<td>Title</td>
<td>Author</td>
<td>Publisher</td>
<td>Edition</td>
<td>Price</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<%
while (rs.next()) { %>
<tr>
<td><%=rs.getInt("accno")%></td> <td><%=rs.getString("title")%></td>
<td><%=rs.getString("author")%></td> <td><%=rs.getString("publisher")%></td>
<td><%=rs.getInt("edition")%></td> <td><%=rs.getInt("price")%></td> <td><a
href="EditBook.jsp?accno=
<%=rs.getInt("accno")%>">Edit</a></td> <td><a href="DeleteBook.jsp?accno=
<%=rs.getInt("accno")%>">Delete</a></td>
</tr>
<%
}
%>
</table>
<%
} catch (Exception e) {
out.println(e);
}
rs.close();
ps.close();
conn.close();
%>
<br />
<a href="Book.html">Back</a>
</body>
</html>
EditBook.jsp
UpdateBook.jsp
DeleteBook.jsp
Output:
Result:
Program to develop a Library Management System application accessing a database using JSP has
been developed.
Ex.No.: AJAX – Web Page Login Process
Date:
Aim:
Program to implement the concepts of AJAX for web page login process.
Procedure:
Program:
home.php
<?php
include "config.php";
// Check user login or not
if(!isset($_SESSION['uname'])){
header('Location: index.php');
}
// logout
if(isset($_POST['but_logout'])){
session_destroy();
header('Location: index.php');
}
?>
<!doctype html>
<html>
<head>
<title>Login page with jQuery and AJAX</title> </head>
<body>
<h1>Welcome to REC Homepage</h1> <form method='post' action="">
<input type="submit" value="Logout" name="but_logout"> </form>
</body>
</html>
Index.php
<!doctype html>
<html>
<head>
<title>Login page with jQuery and AJAX</title> <link href="style.css" rel="stylesheet"
type="text/css">
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript"> $(document).ready(function(){
$("#but_submit").click(function(){ var username = $("#txt_uname").val().trim();
var password = $("#txt_pwd").val().trim();
if( username != "" && password != "" ){ $.ajax({
url:'checkUser.php', type:'post', data:{username:username,password:password},
success:function(response){ var msg = ""; if(response == 1){
window.location = "home.php";
}else{
msg = "Invalid username and password!"; }
$("#message").html(msg); }
});
}
});
});
</script>
</head>
<body>
<div class="container">
<div id="div_login">
<h1>Login</h1>
<div id="message"></div>
<div>
<input type="text" class="textbox" id="txt_uname" name="txt_uname" placeholder="Username"/>
</div>
<div>
<input type="password" class="textbox" id="txt_pwd" name="txt_pwd" placeholder="Password"/>
</div>
<div>
<input type="button" value="Submit" name="but_submit"id="but_submit" />
</div>
</div>
</div>
</body>
</html>
config.php
<?php
session_start();
$host = "localhost"; /* Host name */ $user = "root"; /* User */
$password = "admin"; /* Password */ $dbname = "rec"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname); // Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error()); }
?>
checkUser.php
<?php
include "config.php";
$uname = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,$_POST['password']);
if ($uname != "" && $password != ""){ $sql_query = "SELECT count(*) as cntUser FROM
userdetails WHERE uname='".$uname."' and passwd='".$password."'"; $result =
mysqli_query($con,$sql_query); $row = mysqli_fetch_array($result);
$count = $row['cntUser'];
if($count > 0){
$_SESSION['uname'] = $uname; echo 1;
}else{
echo 0;
}
}
style.css
/* Container */
.container{
width:40%;
margin:0 auto;
}
/* Login */
#div_login{
border: 1px solid gray; border-radius: 3px; width: 470px;
height: 270px;
box-shadow: 0px 2px 2px 0px gray; margin: 0 auto;
}
#div_login h1{
margin-top: 0px;
font-weight: normal;
padding: 10px;
background-color: cornflowerblue; color: white;
font-family: sans-serif; }
#div_login div{
clear: both;
margin-top: 10px;
padding: 5px;
}
#div_login .textbox{
width: 96%;
padding: 7px;
}
#div_login input[type=submit]{ padding: 7px;
width: 100px;
background-color: lightseagreen; border: 0px;
color: white;
}
#message{
width:100%;
text-align:center;
color:red;
}
/* media */
@media screen and (max-width:720px){ .container{
width: 100%;
}
#div_login{
width: 99%;
}
}
Output:
Result:
Program to implement the concepts of AJAX for web page login process has been developed.