Ajp Prac 8
Ajp Prac 8
Ajp Prac 8
Practical:8
Aim: Write a JSP application to manage User Profile. User must be able to View, Edit,
Delete information from application.
Code:
login.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Page</title>
</head>
<body>
<center>
<h2>You have to Login First...</h2>
<form action="#" method="post">
<fieldset style="margin-left: 600px; margin-right: 600px;">
<legend>Login Details</legend>
<label>Username:</label>
<input type="text" name="uname"><br>
<label>Password:</label>
<input type="Password" name="password"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</center>
</body>
</html>
<%@ page import="java.sql.*"%>
pg. 14
200210107046
<%
String userName = request.getParameter("uname");
String password = request.getParameter("password");
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
PreparedStatement pt = cn.prepareStatement("Select * from tbl_login where
username=? and password = ?");
pt.setString(1,userName);
pt.setString(2,password);
ResultSet rs = pt.executeQuery();
while(rs.next())
{
session.setAttribute("username",rs.getString(1));
response.sendRedirect("Index.jsp");
}
}
catch(Exception e)
{
out.println("Error : "+e);
}
%>
Index.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
pg. 15
200210107046
<title>Login Page</title>
</head>
<body>
<b1><h1>
<%
String username = (String)session.getAttribute("username");
if (username != null)
{
// out.print("Welcome, " + request.getParameter("name"));
out.print("Welcome to, " + username);
}
else
{
response.sendRedirect("login.jsp");
}
%>
<h1></b>
<center><h2> Here is User Profile Management is designed for display user information.<br><br>
1. View User Information <button><a href='view.jsp'>Click Here...</a></button><br>
2. Edit User Information <button><a href='edit.jsp'>Click Here...</a></button><br>
3. Delete User Information <button><a href='delete.jsp'>Click
Here...</a></button><br><br><br>
---For Logout <button><a href='logout.jsp'>Click Here...</a></button>
</h2></center>
</body>
</html>
View.jsp:
<b><h1>
<%
pg. 16
200210107046
%><td><%out.print(rs.getInt(1));%></td>
<td><%out.print(rs.getString(2));%></td>
<td><%out.print(rs.getString(3));%></td>
<td><%out.print(rs.getString(4));%></td>
<td><%out.print(rs.getString(5));%></td></tr><%
}
}
catch(Exception e)
{
out.println("Error : "+e);
}
%>
</tr>
</table>
<br>
<br>
<h2><a href="Index.jsp">Go Back to Main page...</a></h2>
</center>
pg. 18
200210107046
</body>
</html>
edit.jsp:
<b><h1>
<%
String username = (String)session.getAttribute("username");
if (username != null)
{
out.print("Welcome to, " + username);
}
else
{
response.sendRedirect("login.jsp");
}
%>
</h1></b>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit User Information</title>
</head>
<body>
<center><h1>Edit User Information</h1>
<form action="edit1.jsp" method="post">
<fieldset style="margin-left: 600px; margin-right: 600px;">
<legend>Details to fillup</legend>
pg. 19
200210107046
}
%>
</tr>
</table>
<br>
<h2><a href="Index.jsp">Go Back to Main page...</a></h2>
</center>
</body>
</html>
edit1.jsp:
<b><h1>
<%
String username = (String)session.getAttribute("username");
if (username != null)
{
// out.print("Welcome, " + request.getParameter("name"));
out.print("Welcome to, " + username);
}
else
{
response.sendRedirect("login.jsp");
}
%>
</h1></b>
<%@ page import="java.sql.*"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
try{
pg. 21
200210107046
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
PreparedStatement pt = cn.prepareStatement("select * from tbl_user where u_id =
?");
pt.setInt(1,id);
ResultSet rs = pt.executeQuery();
if (rs == null)
{
out.println("No user Founded this name.");
}
while(rs.next())
{
%>
<center><h1>Edit User Information</h1>
<table border="3" style="font-size: 18px;">
<form action="edit2.jsp" method="post">
<tr>
<td><label>User-ID: </label></td>
<td><input name="id"
value="<%out.print(rs.getInt(1));%>"></td>
</tr>
<tr>
<td><label>Name: </label></td>
<td><input type="name" name="name"
value="<%out.print(rs.getString(2));%>"></td>
</tr>
<tr>
<td><label>E-Mail ID:</label></td>
pg. 22
200210107046
pg. 23
200210107046
edit2.jsp:
<b><h1>
<%
String username = (String)session.getAttribute("username");
if (username != null)
{
out.print("Welcome to, " + username);
}
else
{
response.sendRedirect("login.jsp");
}
%>
</h1></b>
<center><h1>Edit User Information</h1>
<%@ page import="java.sql.*"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
String nm = request.getParameter("name");
String mail = request.getParameter("mail");
String c_number = request.getParameter("c_number");
String city = request.getParameter("city");
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
pg. 24
200210107046
delete.jsp:
<b><h1>
<%
String username = (String)session.getAttribute("username");
if (username != null)
{
out.print("Welcome to, " + username);
}
else
{
pg. 25
200210107046
response.sendRedirect("login.jsp");
}
%>
</h1></b>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Delete User Information</title>
</head>
<body>
<center><h1>Delete User Information</h1>
<form action="#" method="post">
<fieldset style="margin-left: 600px; margin-right: 600px;">
<legend>Details to fillup</legend>
<label>Enter User-ID:</label>
<input type="number" name="id" placeholder="Enter User-ID to Delete
Information"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
<table border="2">
<tr>
<th>User ID</th>
<th>Name</th>
</tr>
<tr>
<%@ page import="java.sql.*"%>
pg. 26
200210107046
<%
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
PreparedStatement pt = cn.prepareStatement("Select * from tbl_user");
ResultSet rs = pt.executeQuery();
while(rs.next())
{
%>
<td><%out.print(rs.getInt(1));%></td>
<td><%out.print(rs.getString(2));%></td></tr><%
}
}
catch(Exception e)
{
out.println("Error : "+e);
}
%>
</tr>
</table>
<%@ page import="java.sql.*"%>
<%
try{
int id = Integer.parseInt(request.getParameter("id"));
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
PreparedStatement pt = cn.prepareStatement("Delete from tbl_user where u_id = ?");
pg. 27
200210107046
pt.setInt(1,id);
int rs = pt.executeUpdate();
%><center><h2><%out.println(rs + " row Deleted Successfully.");%></h2><%
}
catch(Exception e)
{
out.println("Error : "+e);
}
%>
<br>
<h2><a href="view.jsp">Go Back to View page...</a><br><br>
<a href="edit.jsp">Go Back to Edit page...</a><br><br>
<a href="Index.jsp">Go Back to Main page...</a></h2>
</center>
</body>
</html>
delete1.jsp:
<%@ page import="java.sql.*"%>
<%
int id = Integer.parseInt(request.getParameter("id"));
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ajp";
Connection cn = DriverManager.getConnection(url,"root","");
PreparedStatement pt = cn.prepareStatement("Delete from tbl_user where u_id = ?");
pt.setInt(1,id);
int rs = pt.executeUpdate();
pg. 28
200210107046
logout.jsp:
<%
session.setAttribute("username",null);
response.sendRedirect("login.jsp");
%>
Output:
1) login.jsp
pg. 29
200210107046
2) Index.jsp
3) view.jsp
pg. 30
200210107046
6) edit information
7) edit1.jsp
pg. 31
200210107046
8) edited table
9) delete.jsp
pg. 32
200210107046
10) delete1.jsp
pg. 33
200210107046
pg. 34