-1Tạ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 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="" %>
Untitled Document <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <% if(request.getParameter("btlogin")==null) { %>
<% } 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"/> <% String user=request.getParameter("textuser"); String pass=request.getParameter("textpass"); boolean kt=false; try { kt=bean.checkuser(user,pass); } catch(Exception ex) { out.println("
Loi roi
"); } if(kt==true) response.sendRedirect("book.jsp"); else out.println("
Invalid user
"); } %> ------------newacc.jsp----------------<%@ page language="java" import="myclass.QLSach" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
Create Account
-3<% if(request.getParameter("btnewaccount")==null) { %>
Create Account
<% } 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("
Create account succesfull
"); } catch(Exception ex) { out.println("
Create account false
"); } } %>
-4-
------------book.jsp--------------<%@ page language="java" import="myclass.*" import="java.sql.*" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
New Page 1 List Books
BookCode | BookName | BookPrice |
<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+=""; strtam+=""+rs.getInt("BookCode")+" | "; strtam+=""+rs.getString("BookName")+" | "; strtam+=""+rs.getInt("BookPrice")+" | "; strtam+="
"; out.println(strtam); } } catch(NullPointerException exn)
-5{ exn.printStackTrace(); } %>
NewBook
--------------newbook.jsp-------------<%@ page language="java" import="myclass.QLSach" %> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
New Page 1 <% if(request.getParameter("btnewbook")==null) { %>
New Book
<% } 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 ("
Insert Sussesfull
"); } catch(Exception ex) { out.println ("
Insert Fails
"); } } %> ----------------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);
-7Statement 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(); } }