jsp寫的留言本管理系統(一)

       初學者java入門,小韓留言本管理系統

參考了一些材料,花了不到一週時間把基本功能都實現了下面讓分享我的設計作品。

1.系統概述(參考)

       留言管理系統的目的比較明確,就是提供留言的增加、刪除、修改和顯示的功能。初學者通過《小韓簡易留言管理系統》這個小作品可以對項目開發有個全局的認識,並且通過這個作品可以對開發過程有個清晰的認識。

       本系統運用了CSS+DIV技術佈局,用DREAMWEARE編寫代碼和FIREWORK輔助處理圖片,當然熟練的話也可以直接用文本方式寫代碼。

       本系統採用JSP+ACCESS數據庫設計,基本可以實現增加、刪除、修改和顯示的功能,如果需要修改增加功能可以自行研究。

系統運行效果圖:

 

2.數據庫設計

留言本的數據設計如圖:

表1:message

數據庫 表1 

表2:admin

 

具體的字段表示的含義可以根據英文翻譯。

3.主要頁面代碼的實現

index.jsp 

login.jsp

edit.jsp

add.jsp

about.jsp

3.1  index.jsp 首頁面

 

  1.  
  2. <%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*"  %>  
  3. <%  
  4.   String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("data/guestbook.mdb");  
  5.   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  6.   Connection conn=DriverManager.getConnection(url);  
  7.   Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  8.   Boolean a=(Boolean)session.getAttribute("a");  
  9. %>  
  10. <html><head>  
  11.     <title>簡單留言本--航碩網絡</title>  
  12. <meta http-equiv="Content-Type" content="text/html;charset=gb2312">  
  13.             <link href="css/classid.css" rel="stylesheet" type="text/css">  
  14.             <link href="css/classcss.css" rel="stylesheet" type="text/css">  
  15.             </head>  
  16. <body>  
  17.  
  18. <div>  
  19.   <jsp:include page="head.jsp" />  
  20. </div>  
  21. <%  
  22.   String sql="select * from message order by messageid DESC";  
  23.   ResultSet rs=stmt.executeQuery(sql);  
  24.   //rs.absolute(1);  
  25.   int intPageSize; //一頁顯示的記錄數   
  26.   int intRowCount; //記錄總數   
  27.   int intPageCount; //總頁數   
  28.   int intPage; //待顯示頁碼   
  29.   String strPage;   
  30.   int i;   
  31.   //設置一頁顯示的記錄數   
  32.  intPageSize = 5;   
  33. strPage=request.getParameter("page");  
  34. if(strPage==null){  
  35.     //表明QueryString中沒有page這個參數,此時顯示第一頁數據  
  36.     intPage=1;  
  37. }  
  38. else{  
  39.     //將字符串轉換成整型  
  40.     intPage=Integer.parseInt(strPage);  
  41.     if(intPage<1){  
  42.         intPage=1;  
  43.     }  
  44. }  
  45. //獲取記錄總數   
  46. rs.last();   
  47. intRowCount = rs.getRow();   
  48. //out.print(intRowCount);  
  49. //記算總頁數   
  50. intPageCount = (intRowCount+intPageSize-1) / intPageSize;   
  51. //out.print(intPageCount);  
  52. //調整待顯示的頁碼  
  53. if(intPage>intPageCount){  
  54.         intPage=intPageCount;  
  55.     }  
  56. if(intPageCount>0){  
  57.     rs.absolute((intPage-1)*intPageSize+1);  
  58. i = 0;   
  59.  
  60. while(i<intPageSize && !rs.isAfterLast()){   // intPageSize && !rs.isAfterLast()  
  61. //讀留言信息  
  62. i++;  
  63. int messageid=rs.getInt("messageid");  
  64. String username=rs.getString("username");  
  65. String content=rs.getString("content");  
  66. String title=rs.getString("title");  
  67. String sex=rs.getString("sex");  
  68. String reptime=rs.getString("reptime");  
  69. String userip=rs.getString("userip");  
  70. String repcontext=rs.getString("repcontext");  
  71. %>      
  72. <div id="center">  
  73.     <div id="left" class="left" >  
  74.        <div style="margin-top:25px"><img src="image/sex<%=sex%>.jpg" height="120" width="120"></div>  
  75.         <div><%=username%></div>  
  76.           
  77.     </div>  
  78.     <div id="right">  
  79.         <div id="right01">&nbsp;</div>  
  80.         <div id="right02">  
  81.                 <div align="right"  style="margin:0px 18px 5px 18px;"><div class="title" style=" float:left;margin-left:8px; text-align:center;"><%=title%></div> 留言時間:<%=reptime%> IP:<%=userip%> <hr/></div>  
  82.                 <div style="margin:0px 20px 50px 20px;float:left"><%=content%></div>          
  83.         <div style=" width:720px; text-align:left">  
  84.         <%if(repcontext!=null){%>  
  85.         <hr/>  
  86.         <strong>管理員回覆:</strong><%=repcontext%>  
  87.         <%}  
  88.         else{  
  89.           
  90.         }  
  91.         %>  
  92.         </div>  
  93.           
  94.         <%  
  95.     if(a==null||a.booleanValue()==true){  
  96.     %><div style=" width:720px; "><hr/>  
  97.     <div style="float:right; width:120px;"><a href="del.jsp?id=<%=messageid%>" class="del">刪除</a>  <a href="repadd.jsp?id=<%=messageid%>" class="del">回覆</a> <a href="alter.jsp?id=<%=messageid%>" class="del">編輯</a></div></div>  
  98.     <%  
  99.     }     
  100.     else{  
  101.     }  
  102.     %>  
  103.     </div>  
  104.       
  105.     <div id="right03">&nbsp;</div>    
  106.   </div>  
  107.  
  108.     <%   
  109.  rs.next();  
  110. //讀流言信息結束  
  111.   }   
  112. }%>  
  113. <div id="yema" class="yema"><STRONG>  
  114.   第<%=intPage%>頁 共<%=intRowCount%>條記錄/共<%=intPageCount%>頁   
  115.  <%  
  116.  if(intPage<2){  
  117.  out.print("首頁 | 上一頁");  
  118.  }else{%>  
  119.               <a href="index.jsp?page=1">首頁</a> | <a href="index.jsp?page=<%=intPage-1%>">上一頁</a>  
  120. <%}%>|   
  121. <%  
  122.  if((intPageCount-intPage)<1)  
  123. {  
  124.   out.print("下一頁 | 尾頁");  
  125.  }else{%>  
  126. <a href="index.jsp?page=<%=intPage+1%>">下一頁</a> | <a href="index.jsp?page=<%=intPageCount%>">尾頁</a>   
  127. <%  }  
  128. %>  
  129.   </STRONG></div>  
  130. </div>  
  131.     <div>  
  132.     <jsp:include page="foot.jsp"></jsp:include></div>  
  133. </body>  
  134. </html><%  
  135.     if(conn!=null)  
  136.     {  
  137.         conn.close();  
  138.     }  
  139.     if(stmt!=null)  
  140.     {  
  141.         stmt.close();  
  142.     }  
  143.     %>  
  144.      

 3.2 login.jsp

 

  1. <%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" %>  
  2. <html>  
  3.  
  4. <meta http-equiv="Content-Type" content="text/html;charset=gb2312">  
  5.             <link href="css/classid.css" rel="stylesheet" type="text/css">  
  6.             <link href="css/classcss.css" rel="stylesheet" type="text/css">  
  7. <script language="javascript">  
  8. function checks(){  
  9.     if(loginpost.loginname.value==""){  
  10.         alert("請輸入管理員帳號!");  
  11.         return false;  
  12.     }  
  13.     if(loginpost.loginname.value.length>20){  
  14.         alert("管理員帳號不超過20位!");  
  15.         return false 
  16.     }  
  17.     if(loginpost.loginpass.value==""){  
  18.         alert("請輸入管理員密碼!");  
  19.         return false;  
  20.     }  
  21.     if(loginpost.loginpass.value.length>20){  
  22.         alert("管理員密碼不超過20位!");  
  23.         return false;  
  24.     }  
  25.       
  26.     return true;  
  27. }  
  28. </script>  
  29. <head>  
  30. <title></title></head>  
  31.  
  32.  
  33. <body style="text-align:center">  
  34. <form name="loginpost" method="post" action="loginpost.jsp" onSubmit="return checks();">  
  35.  <div><jsp:include page="head.jsp"/></div>  
  36.  <div style="margin: auto;width:950px;">  
  37.     <div style="float:left; width:500px; height:300px; margin-top:10px; background:url(image/login.jpg)">&nbsp;</div>  
  38.       
  39.       
  40. <%  
  41. Boolean a=(Boolean)session.getAttribute("a");  
  42. if(a==null||a.booleanValue()==false)  
  43. {  
  44. %>  
  45.     <div style="float:left; width:340px; margin-left:40px; margin-top:10px; height:340px; background:url(image/loginbg.jpg)">  
  46.         <div style="margin:100px 40px 20px 50px;"><div id="login">用戶名</div><input type="text" name="loginname" size="25" ></div>  
  47.         <div style="margin:35px 40px 20px 51px;"><div id="login2">密 碼</div><input type="password" name="loginpass" size="27" ></div>  
  48.         <div style="margin-top:40px; margin-left:50px;">  
  49.         <div style="float:left; margin-right:1px;"><input type="submit" name="dl" value=" 登陸 "></div>  
  50.         <div style="float:left; "><input type="reset" name="cz" value=" 註冊 "></div></div>  
  51.     </div>  
  52.     <%}else{  
  53.     %>  
  54.     <div style="float:left; width:340px; margin-left:40px; margin-top:10px; height:340px; background:url(image/loginbg.jpg)">  
  55.       <div style="margin-top:40px; margin-left:50px;">  
  56.         <div style="float:left; margin-top:120px; margin-left:42px;" class="loginin">您已經登陸了</div>  
  57.       </div>  
  58.     </div>  
  59.     <%}%>     
  60. </div>  
  61. <div><jsp:include page="foot.jsp"/></div>  
  62. </body>  
  63. </html>  

其餘頁面代碼相似。

4.日誌處理分析

數據庫的連接:

  1. <%  
  2.   String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("data/guestbook.mdb");  
  3.   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  4.   Connection conn=DriverManager.getConnection(url);  
  5.   Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  6.   Boolean a=(Boolean)session.getAttribute("a");  
  7. %> 

數據處理:判斷每頁顯示的條數

 

  1. <%  
  2.   String sql="select * from message order by messageid DESC";  
  3.   ResultSet rs=stmt.executeQuery(sql);  
  4.   //rs.absolute(1);  
  5.   int intPageSize; //一頁顯示的記錄數   
  6.   int intRowCount; //記錄總數   
  7.   int intPageCount; //總頁數   
  8.   int intPage; //待顯示頁碼   
  9.   String strPage;   
  10.   int i;   
  11.   //設置一頁顯示的記錄數   
  12.  intPageSize = 5;   
  13. strPage=request.getParameter("page");  
  14. if(strPage==null){  
  15.     //表明QueryString中沒有page這個參數,此時顯示第一頁數據  
  16.     intPage=1;  
  17. }  
  18. else{  
  19.     //將字符串轉換成整型  
  20.     intPage=Integer.parseInt(strPage);  
  21.     if(intPage<1){  
  22.         intPage=1;  
  23.     }  
  24. }  
  25. //獲取記錄總數   
  26. rs.last();   
  27. intRowCount = rs.getRow();   
  28. //out.print(intRowCount);  
  29. //記算總頁數   
  30. intPageCount = (intRowCount+intPageSize-1) / intPageSize;   
  31. //out.print(intPageCount);  
  32. //調整待顯示的頁碼  
  33. if(intPage>intPageCount){  
  34.         intPage=intPageCount;  
  35.     }  
  36. if(intPageCount>0){  
  37.     rs.absolute((intPage-1)*intPageSize+1);  
  38. i = 0;   
  39.  
  40. while(i<intPageSize && !rs.isAfterLast()){   // intPageSize && !rs.isAfterLast()  
  41. //讀留言信息  
  42. i++;  
  43. int messageid=rs.getInt("messageid");  
  44. String username=rs.getString("username");  
  45. String content=rs.getString("content");  
  46. String title=rs.getString("title");  
  47. String sex=rs.getString("sex");  
  48. String reptime=rs.getString("reptime");  
  49. String userip=rs.getString("userip");  
  50. String repcontext=rs.getString("repcontext");  
  51. %>     

頁碼的顯示:

 

  1. <div id="yema" class="yema"><STRONG>  
  2.   第<%=intPage%>頁 共<%=intRowCount%>條記錄/共<%=intPageCount%>頁   
  3.  <%  
  4.  if(intPage<2){  
  5.  out.print("首頁 | 上一頁");  
  6.  }else{%>  
  7.               <a href="index.jsp?page=1">首頁</a> | <a href="index.jsp?page=<%=intPage-1%>">上一頁</a>  
  8. <%}%>|   
  9. <%  
  10.  if((intPageCount-intPage)<1)  
  11. {  
  12.   out.print("下一頁 | 尾頁");  
  13.  }else{%>  
  14. <a href="index.jsp?page=<%=intPage+1%>">下一頁</a> | <a href="index.jsp?page=<%=intPageCount%>">尾頁</a>   
  15. <%  }  
  16. %>  
  17.   </STRONG></div>  
  18. </div> 

還有一些JAVA 函數大家可一查閱網上材料。

        可能結構粗糙,但是對於初學者來說,這個可以很清楚的熟悉WEB開發和JSP項目的開發流程。摘自航碩網絡。

    歡迎多多給意見哈。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章