JSP实现留言板的JavaBean类和分页显示的源码

发表于:2007-05-25来源:作者:点击数: 标签:
//留言bean package userinfobean; /** * pTitle: /p * pDescription: /p * pCopyright: Copyright (c) 2004/p * pCompany: /p * @author not attributable * @version 1.0 */ public class memoBean { private String id;//1.用户名 private String email;/

//留言bean
package userinfobean;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */


public class memoBean {


  private String id;//1.用户名
  private String email;//2.密码
  private String content;//3.留言 


  public memoBean() {
  }
  //1.用户名
  public void setId(String s){
    this.id=s;
  }
  //2.密码
  public void setEmail(String s){
    this.email=s;
  }
  //3.留言
   public void setContent(String s){
    this.content=s;
  }
 


  //1.用户名
   public String getId( ){
     return this.id;
   }
   //2.密码
   public String getEmail(){
     return this.email;
   }
   //3.留言 
   public String getContent(){
     return this.content;
   }
}
//// 用于数据库操作的bean          
package userinfobean;


import userinfobean.*;
import java.io.*;
import java.util.*;
import java.sql.*;



public class databaseBean{
  private String dbName;
  private String dbUser;
  private String dbPass;
   private Vector memoVector;


   Connection connection;


  public databaseBean(){
    dbName=new String("×××××××");
    dbUser=new String("××××××");
    dbPass=new String("×××××××");
    String connectionUrl="jdbc:mysql://localhost/"+dbName;
    try{
      Class.forName("org.gjt.mm.mysql.Driver");
      connection=DriverManager.getConnection(connectionUrl,dbUser,dbPass);
    }
    catch(Exception e){
      System.out.println(e.toString());
      }
  }


  public void dbQueryMemos(){
    try{
       memoVector=new Vector();
      Statement stmt=connection.createStatement();
      ResultSet rs=stmt.executeQuery("select * from memo;");
      while(rs.next()){
        memoBean temp=new memoBean();
        temp.setId(rs.getString(2));
        temp.setEmail(rs.getString(3));
        temp.setContent(rs.getString(4));
        memoVector.add(temp);
      }


   }
   catch(SQLException e){
     System.out.println(e.toString());
   }


 


  }

    public void setDbName(String s){
    this.dbName=s;
  }
  public void setDbUser(String s){
    this.dbUser=s;
  }
  public void setDbPass(String s){
    this.dbPass=s;
  }


  public String getDbName(){
    return this.dbName;
  }
  public String getDbUser(){
    return this.dbUser;
  }
  public String getDbPass(){
    return this.dbPass;
  }
    public Vector getMemoVector(){
    return this.memoVector;


  }


}

///以下是网页源码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" %>
<%@ page  import="java.text.*" %>
<%@ page  import="userinfobean.*" %>
<jsp:useBean id="memoInfo" scope="page" class="userinfobean.memoBean"/>
<jsp:useBean id="memoQuery" scope="page" class="userinfobean.databaseBean"/>
<%!
int totalPages;
int countPerPage=3;
int totalCount;
int currentPage=0;
int currentStart;
%>
<%
memoQuery.dbQueryMemos();
Vector vt=memoQuery.getMemoVector();
totalCount=vt.size();
totalPages=totalCount/countPerPage;
if(totalCount%countPerPage>0)totalPages=totalPages+1;
if(request.getParameter("page")==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("page"));
if(currentPage>totalPages)currentPage=totalPages;
if(currentPage<1)currentPage=1;
}
currentStart=totalCount-1-countPerPage * (currentPage-1);
%>
<html>
<head>
<title>NetGreen</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <link rel="stylesheet" href="main.css">                            
</head>
<script language="JavaScript" >
function page_check()
{
if(document.pageForm.page.value=="")
{alert("请输入页数");
document.pageForm.page.focus();
return false;
}
return true;
}</script>
<body bgcolor="ffffff"  >


<center>
  <table width="800" border="0" height="622">
    <tr>
      <td height="71" align="center" valign="middle"> <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC">
          <tr>
            <td width="19%" height="60" align="left" valign="bottom" bgcolor="#FFFFFF"><b><font size="4" color="#00clearcase/" target="_blank" >cc66" ><img src="image/logo.gif" width="156" height="60"></font></b></td>
            <td width="57%" align="left" valign="bottom" bgcolor="#FFFFFF"><img src="image/banner.gif" width="100%" height="60"></td>
            <td width="24%" align="left" valign="bottom"> <table width="102%" height="60" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC" bgcolor="#FFFFFF">
                <tr>
                  <td width="50%" height="27" align="center">上 传</td>
                  <td width="50%" align="center">收藏本页</td>
                </tr>
                <tr>
                  <td height="21" align="center" bgcolor="#FFFFFF" >退 出 </td>
                  <td align="center"><a href="memoForm.htm" target="_self">我要留言</a></td>
                </tr>
              </table></td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td height="31"><table width="100%" border="1" bordercolor="#0099CC">
          <tr bgcolor="#00CC66">
            <td width="12%" height="23" align="center" bgcolor="#CCFFFF"><a href="index.htm" >首页</a></td>
            <td width="12%" align="center" bgcolor="#CCFFFF"><a href="loginForm.htm" >登陆</a></td>
            <td width="12%" align="center" bgcolor="#CCFFFF">&nbsp;</td>
            <td width="12%" align="center" bgcolor="#CCFFFF">&nbsp;</td>
            <td width="12%" align="center" bgcolor="#CCFFFF" >&nbsp; </td>
            <td width="12%" align="center" bgcolor="#CCFFFF">&nbsp;</td>
            <td width="12%" align="center" bgcolor="#CCFFFF"><a href="memoDisplay.jsp" >留言板</a></td>
            <td width="12%" align="center"  bgcolor="#CCFFFF"><a href="registForm.htm">注册</a></td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td height="435" align="center" valign="top" nowrap>
        <table width="100%"  border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC">
          <tr>
            <td width="18%" height="431" align="center" valign="middle" bgcolor="#CCFFFF">
            </td>
            <td width="83%" align="center" valign="top" bgcolor="#FFFFFF"><table width="100%" border="1"><form name="pageForm" method="post"  onSubmit="return page_check();"action="memoDisplay.jsp">
                <tr>
                  <td width="36%">共有留言<%out.print(totalPages);%> 页,现在是第<%out.print(currentPage);%>  页</td>
                    <td width="64%">
     <a href="memoDisplay.jsp?page=<%=currentPage-1%>" >上一页 </a>
     <a href="memoDisplay.jsp?page=<%=currentPage+1%>" >下一页  </a>
     <a href="memoDisplay.jsp?page=1" >首页 </a>
      <a href="memoDisplay.jsp?page=<%=totalPages%>" >尾页  </a>
      <input type="submit" name="Submit" value="G0">
                      <input name="page" type="text" size="10">
                      页</td>
                </tr>
              </form></table>
              <%
    
    for(int i=0;i<countPerPage&&currentStart-i>=0;i++)
    {
     memoInfo.setId(((memoBean)vt.get(currentStart-i)).getId());
     memoInfo.setEmail(((memoBean)vt.get(currentStart-i)).getEmail());
     memoInfo.setContent(((memoBean)vt.get(currentStart-i)).getContent());
     
     
     
   out.print("<table width='100%' border='1' ><tr><td width='19%'>");
   out.print(memoInfo.getId());
   out.print("</td><td width='81%'>");
   out.print(memoInfo.getEmail());
   out.print("</td></tr><tr><td height='44' colspan='2'>");
   out.print(memoInfo.getContent());
   out.print("</td></tr></table> ");
   out.print("<br>");
     
    }
  
   %>
   </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td height="73" align="center" valign="top" nowrap>
        <table width="100%" border="0">
          <tr>
            <td height="21" align="center" bgcolor="#FFFFFF"><hr width="85%" noshade  color="#00CC66"></td>
          </tr>
          <tr>
            <td height="12" align="center">Copyright @ HomeLee. All rights reserved.
            </td>
          </tr>
          <tr>
            <td height="12" align="center" bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
        </table></td>
    </tr>
  </table>
  </center></body>
</html>

原文转自:http://www.ltesting.net