Professional Documents
Culture Documents
Tạo thư mục WEB-INF chứa các thư mục : classes,source,lib,tabs và tập tin
XML web.xml.
Trong thư mục source chứa các file java,javabean.
Trong thư mục classes chứa các class.
Tập tin XML có nội dung sau :
----------web.xml------------
<web-app>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Các trang JSP nằm cùng cấp với thư mục WEB-INF:
-------------login.jsp--------------------
<body>
<%
if(request.getParameter("btlogin")==null)
{
%>
<form name="form1" method="post" action="">
<table width="249" border="0">
<tr>
<th width="280" scope="col">Login</th>
</tr>
<tr>
<td>Username
<input type="text" name="textuser"></td>
</tr>
<tr>
<td>Password
<input type="password" name="textpass"></td>
</tr>
<tr>
<td>
<p align="center"><input type="submit" value="Login"
name="btlogin"></td>
</tr>
-2-
<tr>
<td>
<p align="center"><a href="newacc.jsp">new account</a></p></td>
</tr>
</table>
</form>
<%
}
else
{
%>
<jsp:useBean id="bean" class="myclass.QLSach" scope="application">
<jsp:setProperty name="bean" property="strDriver"
value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<jsp:setProperty name="bean" property="strUrl"
value="jdbc:odbc:QLSach"/>
</jsp:useBean>
<%
String user=request.getParameter("textuser");
String pass=request.getParameter("textpass");
boolean kt=false;
try
{
kt=bean.checkuser(user,pass);
}
catch(Exception ex)
{
out.println("<div align='center'><h1>Loi roi</h1></div>");
}
if(kt==true)
response.sendRedirect("book.jsp");
else
out.println("<div align='center'><h1>Invalid user<h1></div>");
}
%>
</body>
</html>
------------newacc.jsp-----------------
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Create Account</title>
</head>
<body>
-3-
<%
if(request.getParameter("btnewaccount")==null)
{
%>
<div align=center><h1>Create Account</h1></div>
<form method="POST" action="">
<table border="0" width="60%" id="table1">
<tr>
<td width="17%">Username</td>
<td width="42%"><input type="text" name="textuser"
size="27"></td>
</tr>
<tr>
<td width="17%">Password</td>
<td width="42%"><input type="password"
name="textpass" size="27"></td>
</tr>
<tr>
<td colspan="2">
<p align="center"><input type="submit" value="New
Account" name="btnewaccount"></td>
</tr>
</table>
</form>
<%
}
else
{
%>
<jsp:useBean id="bean" class="myclass.QLSach" scope="application"/>
<%
String username=request.getParameter("textuser");
String password=request.getParameter("textpass");
try
{
bean.insertAccount(username,password);
out.println("<div align=center><h1>Create account
succesfull<h1></div>");
}
catch(Exception ex)
{
out.println("<div align=center><h1>Create account
false<h1></div>");
}
}
%>
</body>
</html>
-4-
------------book.jsp---------------
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
{
exn.printStackTrace();
}
%>
</table>
<p align="center"><a href="newbook.jsp">NewBook</a></p>
</body>
</html>
--------------newbook.jsp--------------
<%
String bookCode=request.getParameter("textcode");
String bookName=request.getParameter("textname");
String bookPrice=request.getParameter("textprice");
try
{
bean.insertBook(bookCode,bookName,bookPrice);
out.println ("<div align=center><h1>Insert Sussesfull<h1></div>");
}
catch(Exception ex)
{
out.println ("<div align=center><h1>Insert Fails<h1></div>");
}
}
%>
</body>
</html>
----------------QLSach.java-----------------------
package myclass;
import java.sql.*;
Statement sttm=con.createStatement();
String cautruyvan="select * from Account where Username='"+username+"'
and Password='"+pass+"'";
ResultSet rs=sttm.executeQuery(cautruyvan);
boolean kt;
if(rs.next())
{
kt=true;
}
else
{
kt=false;
}
return kt;
}
public void insertAccount(String user,String pass) throws Exception
{
Class.forName(strDriver);
Connection con=DriverManager.getConnection(strUrl);
String cautruyvan="INSERT INTO Account VALUES (?,?)";
PreparedStatement pre=con.prepareStatement(cautruyvan);
pre.setString(1,user);
pre.setString(2,pass);
pre.addBatch();
pre.executeBatch();
}
public ResultSet getbook() throws Exception
{
Class.forName(strDriver);
Connection con=DriverManager.getConnection(strUrl);
Statement sttm=con.createStatement();
ResultSet rs=sttm.executeQuery("Select * from Books");
return rs;
}
public void insertBook(String code,String name,String price) throws Exception
{
int bookCode=Integer.parseInt(code);
int bookPrice=Integer.parseInt(price);
Class.forName(strDriver);
Connection con=DriverManager.getConnection(strUrl);
String cautruyvan="INSERT INTO Books VALUES (?,?,?)";
PreparedStatement pre=con.prepareStatement(cautruyvan);
pre.setInt(1,bookCode);
pre.setString(2,name);
pre.setInt(3,bookPrice);
pre.addBatch();
pre.executeBatch();
}
}