Main.java:
package org.jsoft.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jsoft.topic.Topic; import org.jsoft.topic.TopicOP; public class Main extends HttpServlet { int pageSize = 24;//每页显示记录数 int startRow = 0; //开始显示记录的编号 int pageNo=0;//需要显示的页数 int counterStart=0;//每页页码的初始值 //每页页码记录的初始值 int counterEnd=0;//显示页码的最大值 //每页页面记录的最大值 int recordCount=0;//总记录数; int maxPage=0;//总页数 int prevStart=0;//前一页 int nextPage=0;//下一页 int lastRec=0; //最后一页记录的标记 int lastStartRecord=0;//最后一页开始显示记录的编号 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取需要显示的页数,由用户提交 if(request.getParameter("pageNo")==null){ //如果为空,则表示第1页 if(startRow == 0){ pageNo = startRow + 1; //设定为1 } }else{ pageNo = Integer.parseInt(request.getParameter("pageNo")); //获得用户提交的页数 startRow = (pageNo - 1) * pageSize; //获得开始显示的记录编号 } //因为显示页码的数量是动态变化的,假如总共有一百页,则不可能同时显示100个链接。而是根据当前的页数显示 //一定数量的页面链接 //设置显示页码的初始值!! if(pageNo % pageSize == 0){ counterStart = pageNo - (pageSize - 1); }else{ counterStart = pageNo - (pageNo % pageSize) + 1; } counterEnd = counterStart + (pageSize - 1); TopicOP topicOP=new TopicOP(); //获取总记录数 recordCount=topicOP.queryCountRecord(); List<Topic> list=topicOP.queryPageAll(startRow, pageSize); request.getSession().setAttribute("list",list); //获取总页数 if(recordCount % pageSize == 0){ maxPage = recordCount / pageSize; }else{ maxPage = recordCount/pageSize+1; } request.setAttribute("pageNo",pageNo); request.setAttribute("startRow",startRow); request.setAttribute("pageSize",pageSize); request.setAttribute("counterStart",counterStart); request.setAttribute("counterEnd",counterEnd); request.setAttribute("recordCount",recordCount); request.setAttribute("maxPage",maxPage); String loginusername=(String)request.getSession().getAttribute("username"); request.getSession().setAttribute("loginusername", loginusername); String[] userInformation=topicOP.queryUserInformation(loginusername); request.setAttribute("userInformation", userInformation); request.getRequestDispatcher("main.jsp").forward(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
TopicOP.java:
package org.jsoft.topic; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.jsoft.util.MyConnection; public class TopicOP { Connection conn = null; Statement st = null; ResultSet rs = null; public List<Topic> queryALL() { List<Topic> list = new ArrayList<Topic>(); try { conn = MyConnection.getConnection(); st = conn.createStatement(); String sql="select" +" ttopic,tclickcount,ttime,username ,tid "+" FROM bbsuser b ,(select tid,userid,ttopic,ttime ,tclickcount "+"FROM bbstopic group by tid ) a "+"WHERE a.userid=b.userid"; rs = st.executeQuery(sql); while (rs.next()) { Topic topic = new Topic(); topic.setTclickcount(rs.getInt(2)); topic.setTid(rs.getInt(5)); topic.setTtopic(rs.getString(1)); topic.setUsername(rs.getString(4)); topic.setTtime(rs.getDate(3)); list.add(topic); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { MyConnection.close(rs, st, conn); } return list; } //查询要显示的页面的所有记录。limit条件。返回一个List。 public List queryPageAll(int startRow,int pageSize){ List<Topic> list = new ArrayList<Topic>(); try { conn = MyConnection.getConnection(); st = conn.createStatement(); rs = st.executeQuery("select" +" ttopic,tclickcount,ttime,username ,tid "+" FROM bbsuser b ,(select tid,userid,ttopic,ttime ,tclickcount "+"FROM bbstopic group by tid ) a "+"WHERE a.userid=b.userid ORDER BY a.tid ASC LIMIT " +startRow+", "+pageSize); while (rs.next()) { Topic topic = new Topic(); topic.setTclickcount(rs.getInt(2)); topic.setTid(rs.getInt(5)); topic.setTtopic(rs.getString(1)); topic.setUsername(rs.getString(4)); topic.setTtime(rs.getDate(3)); list.add(topic); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ MyConnection.close(rs, st, conn); } return list; } public Topic queryByID(int id) { Topic topic = new Topic(); topic.setTid(id); try { conn = MyConnection.getConnection(); st = conn.createStatement(); String sql = "SELECT tcontents,ttime,ttopic,username FROM bbstopic a,bbsuser b WHERE a.userid=b.userid and tid= " + id; rs = st.executeQuery(sql); if (rs.next()) { topic.setTtime(rs.getDate("ttime")); topic.setTcontents(rs.getString("tcontents")); topic.setTtopic(rs.getString("ttopic")); topic.setUsername(rs.getString("username")); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return topic; } public int addTopic(int userid,String ttopic,String tcontents,String ttime) { int tag = 0; try { conn = MyConnection.getConnection(); st = conn.createStatement(); String sql = "INSERT INTO bbstopic(userid,ttopic,tcontents,ttime) VALUES("+userid+",'"+ttopic+"','"+tcontents+"','"+ttime+"')"; tag = st.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } return tag; } public String[] queryUserInformation(String username){ Connection conn = null; Statement st = null; ResultSet rs = null; String[] str = new String[2]; try { conn = MyConnection.getConnection(); st = conn.createStatement(); String sql = "SELECT email FROM bbsuser WHERE username='"+username+"'"; rs = st.executeQuery(sql); while(rs.next()){ str[1] = rs.getString("email"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { MyConnection.close(rs, st, conn); } str[0]=username; return str; } //获取总记录数 public int queryCountRecord(){ Connection conn = null; Statement st = null; ResultSet rs = null; int recordCount = 0; try { conn = MyConnection.getConnection(); st = conn.createStatement(); rs = st.executeQuery("select count(*) from bbstopic") ; rs.next(); recordCount = rs.getInt(1); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return recordCount; } //public static void main(String[] args) { // TopicOP op=new TopicOP(); //// op.queryByID(3); // op.queryCountRecord(); // //} }
jdbc.properties
url=jdbc:mysql:///bbs username=root password=root driverclass=com.mysql.jdbc.Driver
main.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page import="org.jsoft.topic.TopicOP" %> <% request.setCharacterEncoding("utf-8"); String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% int pageNo=(Integer)request.getAttribute("pageNo"); int startRow=(Integer)request.getAttribute("startRow"); int pageSize=(Integer)request.getAttribute("pageSize"); int counterStart=(Integer)request.getAttribute("counterStart"); int counterEnd=(Integer)request.getAttribute("counterEnd"); int recordCount=(Integer)request.getAttribute("recordCount"); int maxPage=(Integer)request.getAttribute("maxPage"); int prevStart=0; int nextPage=0; int lastRec=0; int lastStartRecord=0; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'main.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="css/mainstyle.css"> </head> <body> <div id="container"> <div id="top"> <div id="topleft"></div> <div id="topmiddle"> <h1 id="topich1"><font color="#DAD3A9">欢迎来到雪狼论坛</font></h1> </div> <div id="topright"> <table id=userinformation border="1px"> <tr> <td id="userinformationtd1">用户名</td> <td id="userinformationtd2">${userInformation[0]}</td> </tr> <tr> <td>邮箱</td> <td>${userInformation[1]}</td> </tr> <tr> <td colspan="2" height="40px"> <a href="changeInformation.jsp" id="changePersonalInformation_a">亲,点此更改个人资料!!!</a> </td> </tr> </table> <div id="topicrightreply"> <a href="reply.jsp" id="topicrightreply_a">亲,点此可发帖哦!!!</a> </div> </div> </div> <div id="gap">假如你没有惊天动地的大事情可以做,那么就做一个小人物,给一个可爱的小孩作父母,给一对老人做孝顺的子女,给你的另一半一个简单而幸福的人生。</div> <div id="middle"> <div id="middleleft"></div> <div id="middlecenter"> <marquee><font color="r#EAD1A8">点击标题进行回复 点击标题进行回复 点击标题进行回复 点击标题进行回复 点击标题进行回复</font></marquee> <table align="center" border="1px"> <tr><td align="center" width="325px">帖 子 标 题</td><td align="center" width="175px">帖 子 作 者</td><td align="center" width="100px">发 表 时 间</td><td align="center" width="100px">点 击 次 数</td></tr> <tr><%="总共"+recordCount+"条记录 - 当前页:"+pageNo+"/"+maxPage %></tr> <c:forEach var = "topic" items="${list}"> <tr> <td align="center"><a href="ReplyTo?tid=${topic.tid }">${topic.ttopic }</a></td> <td align="center">${topic.username }</td> <td align="center">${topic.ttime }</td> <td align="center"> ${topic.tclickcount }</td> </tr> </c:forEach> </table> <div id="page"> <font size=4 > <% //显示第一页或者前一页的链接 //如果当前页不是第1页,则显示第一页和前一页的链接 if(pageNo != 1){ prevStart = pageNo - 1; out.print("<a href=Main?pageNo=1>第一页 </a>: "); out.print("<a href=Main?pageNo="+prevStart+">前一页</a>"); } %> [ <% //打印需要显示的页码 for(int c=counterStart;c<=counterEnd;c++){ if(c <maxPage){ if(c == pageNo){ if(c %pageSize == 0){ out.print(c); }else{ out.print(c+" ,"); } }else if(c % pageSize == 0){ out.print("<a href=Main?pageNo="+c+">"+c+"</a>"); }else{ out.print("<a href=Main?pageNo="+c+">"+c+"</a> , "); } }else{ if(pageNo == maxPage){ out.print(c); break; }else{ out.print("<a href=Main?pageNo="+c+">"+c+"</a>"); break; } } } %> ] <% if(pageNo < maxPage){ //如果当前页不是最后一页,则显示下一页链接 nextPage = pageNo + 1; out.print("<a href=Main?pageNo="+nextPage+">下一页</a>"); } %> <% //同时如果当前页不是最后一页,要显示最后一页的链接 if(pageNo < maxPage){ lastRec = recordCount % pageSize; if(lastRec == 0){ lastStartRecord = recordCount - pageSize; } else{ lastStartRecord = recordCount - lastRec; } out.print(":"); out.print("<a href=Main?pageNo="+maxPage+">最后一页</a>"); } %> </font> </div> </div> <div id="middleright"> <div id="backtopbutton"> <input type="button" width="100" height="100" onclick="location.href='#top'" value="回到顶部" /> </div> </div> </div> <div id="bottom"> <p>笑笑更幸福</p> <p> 某人上街买了一坛好酒,放在小院走廊上,第二天,他发现酒少了5分之1,便在酒桶上贴了不许偷酒四个字。 第三天酒又少了5分之2,他非常生气又贴了偷酒者重罚5个字。 第四天,酒还是被偷,他肺都快气炸了。他朋友知道了就对他说;你不会在酒桶上贴尿桶二字啊,看谁还偷喝。他觉得很有道理就照办了。第五天天他哭了。桶满了。 ……等等,故事还没完,第六天,他再一次在酒桶上贴了不许偷酒四个字。 那一天很多人都哭了。 </p> </div> </div> </body> </html>