Using Database in The Web-Environment II

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Using Database in the web-

environment II
Reading Records from the
database

Type and run db_002.jsp file.


The results should be:
Created on : Aug 13, 2013, 1:28:16 PM
Author : ehab
--%>

<%@page contentType="text/html" pageEncoding="windows-1256"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<%
String sql = "";
Class.forName("org.gjt.mm.mysql.Driver");
try {
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/summer_2013", "root", "");
java.sql.Statement st = connection.createStatement();

sql = " SELECT * FROM student ";


java.sql.ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
out.println(rs.getString(1) + " , " + rs.getString(2)+" <br>" );

}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(sql);
}

%>
</body>
</html>
Reading Records from the
database Into a table
Type and run db_003.jsp file.
The results should be:
<%--
Document : db_003
Created on : Aug 13, 2013, 1:28:16 PM
Author : ehab
--%>
<%@page contentType="text/html" pageEncoding="windows-1256"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<%
String sql = "";
Class.forName("org.gjt.mm.mysql.Driver");
try {
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/summer_2013", "root", "");
java.sql.Statement st = connection.createStatement();
sql = " SELECT * FROM student ";
java.sql.ResultSet rs = st.executeQuery(sql);
out.println(" <Table cellpadding=\"10\" cellspacing=\"1\" align=\"center\" border=\"1\" >" );
while (rs.next()) {
out.println("<TR><TD bgcolor=\"#8888AA\">" + rs.getString(1) +
"</TD><TD bgcolor=\"#88AA88\"> " + rs.getString(2)+
"</TD><TD bgcolor=\"#AA8888\"> " + rs.getString(3)+" </TD></TR>" );
}
out.println(" </Table> ");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(sql);
}
%>
</body>
</html>
Guaranteeing that the user is
logged in for each page
Copy the following 3 files:
** db_004.jsp (similar to db_003.jsp + the
following lines:
<%
if (session.getAttribute("user")==null){
session.setAttribute("crPage","db_004.jsp" ); // save the current page to return back
response.sendRedirect("login.jsp"); // go to login page
}
%>

** login.jsp
** checlPassword.jsp
<%--
Document : db_004
Created on : Aug 13, 2013, 1:28:16 PM
Author : ehab
--%>
<%
if (session.getAttribute("user")==null){
session.setAttribute("crPage","db_004.jsp" ); // save the current page to return back
response.sendRedirect("login.jsp"); // go to login page
}
%>
<%@page contentType="text/html" pageEncoding="windows-1256"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<%
String sql = "";
Class.forName("org.gjt.mm.mysql.Driver");
try {
java.sql.Connection connection =
java.sql.DriverManager.getConnection("jdbc:mysql://localhost/summer_2013", "root", "");
java.sql.Statement st = connection.createStatement();

sql = " SELECT * FROM student ";


java.sql.ResultSet rs = st.executeQuery(sql);
out.println(" <Table cellpadding=\"10\" cellspacing=\"1\" align=\"center\" border=\"1\" >" );
while (rs.next()) {
out.println("<TR><TD bgcolor=\"#8888AA\">" + rs.getString(1) +
"</TD><TD bgcolor=\"#88AA88\"> " + rs.getString(2)+
"</TD><TD bgcolor=\"#AA8888\"> " + rs.getString(3)+" </TD></TR>" );
}
out.println(" </Table> ");
<%--
Document : login
Created on : Aug 15, 2013, 3:26:46 PM
Author : ehab
--%>

<%@page contentType="text/html" pageEncoding="windows-1256"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>JSP Page</title>
</head>
<body>
<form name="finishStart" action="checkPassword.jsp" mode="Get">
<TR><TD> user </TD> <TD> <input name="user"> </TD> </TR>
<TR><TD> Password </TD> <TD> <input type="password" name="passWord"> </TD>
</TR>
<TR> <TD colspan="2"> <input type="submit" value="ok" style="width:250px;" > </TD>
</TR>
</form>
</body>
</html>
CheckPassword.jsp
<%
String userName = request.getParameter("user");
String passWord = request.getParameter("passWord");
//get the passord of the username from the database
// select password from userPassord where userName ="+userName+ "and passWo
// assume that we got from the database one record with a password egypt
String dbPass = "egypt" ;
if(passWord.equals(dbPass)){
session.setAttribute("user",userName ); // save the current page to return back
String crPage =(String) session.getAttribute("crPage");
response.sendRedirect(crPage); // go to login page
}
else{
response.sendRedirect("login.jsp"); // go to login page
}

%>
*** Run the db_004.jsp
you will be redirected to login.jsp
*** Enter any user name and password
“egypt” (lower case)
*** then you will be redirected again to
db_004.jsp
Assignment 3
Extend the login files (login,
checkPassword, db_004) in this lecture to
read the user name and password from the
database

Hint just minor changes to the


checkPasword.jsp file. Creat a table contain
user and password records

You might also like