servlet实现分页功能

2013-02-05  付民 

代码如下:
package com.interfaceinfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetInterfaceInfoServlet extends HttpServlet {

private static final long serialVersionUID = 1436950164431989523L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/web";
String username = "root";
String password = "721123";
//String sql = "select * from ADDINTERFACEINFO order by id desc";
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>接口信息显示页面</title></head>");
//搜索区域
out.println("<body>");
out.println("<a href='"+request.getContextPath()+"/servlet/UpdateClassifyTypeServlet' title='新增接口信息'>【新增接口信息】</a>

");
out.println("<center><form id='form1' name='form1' method='post' action='"+request.getContextPath()+"/servlet/QueryServlet'>");
out.println("<table><tr><td><strong>搜索:</strong></td>");
out.println("<td>分类<select name='classify' id='classify'><label for='classify'></label>");
out.println("<option value='' checked>请选择</option>");
List<Integer> classify_id = (List<Integer>)request.getAttribute("classify_id");
   List<String> classify = (List<String>)request.getAttribute("classify");
   for(int i = 0; i < classify_id.size(); i++) {
out.println("<option value='"+classify.get(i)+"'>"+classify.get(i)+"</option>");
   }
   out.println("</select></td>&nbsp;&nbsp;");
   
out.println("<td>类型<select name='type' id='type'><label for='type'></label>");
out.println("<option value='' checked>请选择</option>");
List<Integer> type_id = (List<Integer>)request.getAttribute("type_id");
   List<String> type = (List<String>)request.getAttribute("type");
   for(int i = 0; i < type_id.size(); i++) {
out.println("<option value='"+type.get(i)+"'>"+type.get(i)+"</option>");
   }
   out.println("</select></td>&nbsp;&nbsp;");
   
out.println("<td>标题<input type='text' name='title' id='title' /></td>");
out.println("<td><input type='submit' name='submit' id='submit' value='查询' /></td>");
out.println("</tr></table>");
out.println("</form></center>");
out.println("<foot size='8'><strong>信息显示:</strong></foot>

");
out.println("</body>");
out.println("</html>");

/*=====================分页=============*/  
int pageSize = 10;  //一页显示的记录数  
int pageNow = 1;   //希望显示第几页  
int rowCount = 0;   //共有几条记录(查表)  
int pageCount = 0;  //共有几页(计算)  
//通过参数pageNow实现页数的链接  
String sPageNow = request.getParameter("pageNow");  
if(sPageNow != null){  
 //用户不是第一次进入wel  
 pageNow = Integer.parseInt(sPageNow);  
}  
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement pstmt = conn.prepareStatement("select * from ADDINTERFACEINFO order by id desc");
ResultSet rs = (ResultSet) pstmt.executeQuery();
if(rs.next()){  
  rowCount = rs.getInt(1);  
}  
             
//计算pageCount  
if(rowCount%pageSize == 0){  
  pageCount = rowCount/pageSize;  
}else {  
  pageCount = rowCount/pageSize + 1;  
}  
             
//从数据库中取出记录  
//第pageNow页的内容为从数据库中第pageSize*(pageNow - 1)+1条记录开始取出pageSize条记录  
pstmt = conn.prepareStatement("select * from ADDINTERFACEINFO order by id desc limit " + pageSize * (pageNow - 1) + ", "+ pageSize+"");  
rs = pstmt.executeQuery();  
             
//以表格形式在页面显示数据库中的记录  
out.println("<center><table width='1207px' border='1' style='word-break: break-all;overflow:hidden;'>" );
out.println("<tr bgcolor='#808080'><td width='50px' border='1' bordercolor='000000' align='center'><strong>序号</strong></td>");
out.println("<td width='150px' border='1' bordercolor='000000' align='center'><strong>标题</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>分类</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>类型</strong></td>");
out.println("<td width='400px' border='1' bordercolor='000000' align='center'><strong>URL</strong></td>");
out.println("<td width='200px' border='1' bordercolor='000000' align='center'><strong>备注</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>操作</strong></td>");
out.println("<td width='85px' border='1' bordercolor='000000' align='center'><strong>创建时间</strong></td></tr>"); 
//表的内容  
while(rs.next()){  
out.println("<tr><td width='50px' bordercolor='000000'><center>"+rs.getInt("id")+"</center></td>");
out.println("<td width='150px' bordercolor='000000'>"+rs.getString("title")+"</td>");
out.println("<td width='100px' bordercolor='000000'>"+rs.getString("classify")+"</td>");
out.println("<td width='100px' bordercolor='000000'>"+rs.getString("type")+"</td>");
out.println("<td width='400px' bordercolor='000000'>"+rs.getString("urladdress")+"</td>");
out.println("<td width='200px' bordercolor='000000'>"+rs.getString("content")+"</td>");
out.println("<td width='100px' bordercolor='000000'><center><a href='"+rs.getString("urladdress")+"'  target='_blank'>查看</a>&nbsp;<a href='/Interfaceinfo/servlet/UpdateInterfaceInfoServlet?id="+rs.getInt("id")+"'  target='_blank'>修改</a></center></td>");
out.println("<td width='85px' bordercolor='000000'><center>"+rs.getString("time")+"</center></td></tr>");
}  
             
out.println("</table>
");  
out.println("<center><table>"); 
//显示页数超链接  
if(pageNow > 1){  
//当前页不是第一页时显示”上一页“的超链接  
    out.println("<a href='QueryClassifyTypeServlet?pageNow="+(pageNow-1)+"'>上一页</a>");
}  
             
//从当前页开始显示2页的超链接  
for(int i = pageNow; i <= pageNow; i++){  
    out.println("<a href='QueryClassifyTypeServlet?pageNow="+i+"'>"+i+"</a>");  
}  
if(pageNow < pageCount){  
//当前页不是最后一页时显示”下一页“的超链接  
    out.println("<a href='QueryClassifyTypeServlet?pageNow="+(pageNow+1)+"'>下一页</a>"); 
}  
out.println("</table></center>"); 
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

}

472°/4722 人阅读/0 条评论 发表评论

登录 后发表评论