網域查詢: www.
返回首頁
當前位置: 首頁 > 站長學院 > 網站編程 > JSP教程 >

用JSP打造一個留言板(下)

時間:2010-02-17 04:48來源: 作者: 點擊:
一.創建數據庫 1).打開Access2000,創建一個新的數據庫,我將這個數據庫命名為 foxdb.mdb,存在C:\tomcat\fox\global\foxdb.mdb。接下來在 eagle.mdb中創建一個表,命名為 foxtable,表中有五個字段,全
  
  一.創建數據庫

  1).打開Access2000,創建一個新的數據庫,我將這個數據庫命名為 foxdb.mdb,存在C:\tomcat\fox\global\foxdb.mdb。接下來在 eagle.mdb中創建一個表,命名為 foxtable,表中有五個字段,全為文本格式︰

  其中“URL”用于記錄留言者的 IP 。至于各字段的長度,我把“留言”定為200,其它四個各為20。

  2).指定ODBC數據源,其名為foxdb ,指向 C:\tomcat\fox\global\foxdb.mdb。

  二.編寫用戶的留言界面 foxnote.html,存于C:\tomcat\fox\foxnote.html︰

  <html>
  <body>
   <form method="post" action="foxnoteinsert.jsp">
    <br>姓名︰
    <input name=username size=15 value="">
    <br>郵箱︰
    <input name=email size=15 value="">
    <br>留言︰
    <br>
    <textarea name=doc rows="5" cols="40">
    </textarea>
    <br>
    <input type=submit value="遞交">
    <input type=reset value="重填">
   </form>
  </bocy>
  </html>

  在IE中鍵入 http://ip/fox/foxnote.html 看看是否顯示正常(ip是你機器的ip地址)



  三.編寫 foxnoteinsert.jsp ,將用戶的留言寫進數據庫表中︰

  <body bgcolor="#FFFFFF">
  <%@ page import="java.sql.*,MyUtil,java.util.*"%>
  <%
   Connection con=null;
   String username=MyUtil.gb2312ToUnicode(request.getParameter("username"));
   String email=MyUtil.gb2312ToUnicode(request.getParameter("email"));
   String doc=MyUtil.gb2312ToUnicode(request.getParameter("doc"));
   String url=request.getRemoteAddr();
   try {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");                         con=DriverManager.getConnection("jdbc:odbc:foxdb","","");
     String str="insert into foxtable values(?,?,?,?);";
     PreparedStatement pstmt=con.prepareStatement(str);
     pstmt.setString(1,username);
     pstmt.setString(2,email);
     pstmt.setString(3,doc);
     pstmt.setString(4,url);
     pstmt.executeUpdate();
     pstmt.close();
     con.close();
    }
   catch(Exception e) {
     out.println(e.getMessage());
    }
  %>

  這個程序中有一些要說明的地方,就是其中用到了一個 JavaBean ︰MyUtil.class 。

  MyUtil 的作用是字符串之間的轉換。必需關注的是JSP的字符串以Unicode碼表示,而留言板界面的表單卻是以 gb2312 碼表示。所以將用戶的留言寫進數據庫還需要碼間的轉換。如果不轉換而把留言直接寫到數據庫表,則會產生亂碼。下面是 MyUtil 的原代碼,存于C:\tomcat\fox\WEB-INF\classes\MyUtil.java ,編譯後的MyUtil.class文件也存于此。

  import java.io.*;
  public class MyUtil{
  public static String gb2312ToUnicode(String s){
   try{
     return new String(s.getBytes("ISO8859_1"),"gb2312");
    }
   catch(UnsupportedEncodingException uee){
     return s;
    }
   }
  public static String unicodeTogb2312(String s){
   try{
    return new String(s.getBytes("gb2312"),"ISO8859_1");
   }
   catch(UnsupportedEncodingException uee){
    return s;
    }
   }
  }


  四.編寫 foxnoteview.jsp ,用于瀏覽數據庫表中已有的留言,存于C:\tomcat\fox\foxnoteview.jsp ,代碼如下︰

  <html>
  <body>
  <%@ page contentType="text/html;charset=big5" language="java" import="java.sql.*"%>
  <%
   Connection con=null;
   try
    {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     con=DriverManager.getConnection("jdbc:odbc:foxdb","","");
     Statement statement=con.createStatement();
     ResultSet rs=statement.executeQuery("select * from foxtable");

   %>
  <table border="1" width="100%" cellspacing="0" cellpadding="0" align="center" bordercolorlight="#CCCCFF" bordercolordark="#FFFFFF">
  <tr bgcolor="#FFFFFF">
  <td width="15%" height="25" align="center"><i>作者</i></td>
  <td width="28%" height="25" align="center"><i>發表時間</i></td>
  <td width="22%" height="25" align="center"><i>Email</i></td>
  <td width="35%" height="25" align="center"><i>留言內容</i></td>
  <%
   while(rs.next()){
    out.println("<TR><td align=center><font size=2 color=#999999>"+rs.getString("作者")+"</TD>");
    out.println("<TD><font size=2color=#999999>"+rs.getString("Email")+"</font></TD>");
    out.println("<TD><font size=2 color=#999999>"+rs.getString("留言")+"</font></TD>");
    out.println("<TD><font size=2 color=#999999>"+rs.getString("URL")+"</font></TD></TR>");
   }
   rs.close();
   con.close();
   }
   catch(Exception e)
   {
    out.println(e.getMessage());
   }
  %>
  </table>
  </body>
  </html>

  到此,整個留言板程序就算是完工了。留言板在數據庫應用中比較簡單,但是加以變化則可以編寫出各種各樣的應用程序,操作各種各樣的數據庫^_^



頂一下
(0)
0%
踩一下
(0)
0%
------分隔線----------------------------
最新評論 查看所有評論
發表評論 查看所有評論
請自覺遵守互聯網相關的政策法規,嚴禁發佈色情、暴力、反動的言論。
評價:
表情:
用戶名: 密碼: 驗證碼:
推薦內容