You are on page 1of 7

-1-

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--------------------

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


import="java.sql.*" errorPage="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<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-----------------

<%@ page language="java" import="myclass.QLSach" %>


<html>

<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---------------

<%@ page language="java" import="myclass.*" import="java.sql.*" %>


<html>

<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>

<body style="text-align: center">


<div align='center'><h1>List Books<h1></div>

<table border="1" width="62%" id="table1" align=center>


<tr>
<th>BookCode</th>
<th>BookName</th>
<th>BookPrice</th>
</tr>
<jsp:useBean id="bean" class="myclass.QLSach"
scope="application"/>
<%
try
{
ResultSet rs=null;
try
{
rs=bean.getbook();
}
catch(Exception ex)
{
ex.printStackTrace();
}
while(rs.next())
{
String strtam="";
strtam+="<tr>";
strtam+="<td
align=center>"+rs.getInt("BookCode")+"</td>";
strtam+="<td
align=center>"+rs.getString("BookName")+"</td>";
strtam+="<td
align=center>"+rs.getInt("BookPrice")+"</td>";
strtam+="</tr>";
out.println(strtam);
}
}
catch(NullPointerException exn)
-5-

{
exn.printStackTrace();
}
%>
</table>
<p align="center"><a href="newbook.jsp">NewBook</a></p>

</body>

</html>

--------------newbook.jsp--------------

<%@ page language="java" import="myclass.QLSach" %>


<html>
<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>

<body style="text-align: center">


<%
if(request.getParameter("btnewbook")==null)
{
%>
<div align=center><h1>New Book<h1></div>
<form method="POST" action="">
<table border="0" width="55%" id="table1">
<tr>
<td width="103">BookCode</td>
<td><input type="text" name="textcode" size="26"></td>
</tr>
<tr>
<td width="103">BookName</td>
<td><input type="text" name="textname" size="26"></td>
</tr>
<tr>
<td width="103">BookPrice</td>
<td><input type="text" name="textprice" size="26"></td>
</tr>
</table>
<p><input type="submit" value="New Book" name="btnewbook"></p>
</form>
<%
}
else
{
%>
<jsp:useBean id="bean" class="myclass.QLSach" scope="application"/>
-6-

<%
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.*;

public class QLSach


{
private String strDriver,strUrl;
public QLSach()
{
}
public void setStrDriver(String driver)
{
strDriver=driver;
}
public String getStrDriver()
{
return strDriver;
}
public void setStrUrl(String url)
{
strUrl=url;
}
public String getStrUrl()
{
return strUrl;
}
public boolean checkuser(String username,String pass) throws Exception
{
Class.forName(strDriver);
Connection con=DriverManager.getConnection(strUrl);
-7-

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();
}
}

You might also like