纯JSP分页代码之Mysql
//运行图:
//连接字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName(driverName).newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
//每页显示记录数
int PageSize = 8;
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;//最后一页开始显示记录的编号
//获取需要显示的页数,由用户提交
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);
%>
<html>
<head>
<title>分页显示记录</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<%
//获取总记录数
ResultSet rs = statement.executeQuery("select count(*) from items" );
rs.next();
RecordCount = rs.getInt(1);
rs = statement.executeQuery("SELECT image_url,author,price,item_id from items ORDER BY item_id DESC LIMIT "
+StartRow+", "+PageSize);
//获取总页数
MaxPage = RecordCount % PageSize;
if(RecordCount % PageSize == 0){
MaxPage = RecordCoun
相关文档:
Java代码:
package com.zhaipuhong.common.util;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
public class PageBean { & ......
在本篇文章中将使用过滤器进行编码转换、登陆验证、防站外提交的过程,为了演示,我把编码转换放在一个filter中,登陆验证和防站外提交放在另一个filter中,两个filter进行串联工作。
1、为了演示,先制作这两个filter
//这个filter是为了做编码转换,只要访问.jsp网页都要功过这个filter
/**类名:filter.Encoding
*作 ......
注意:jsp:useBean动作,用表单为Bean的属性赋值时,也就是jsp:setProperty动作直接收请求中表单的信息为使用的Bean的属性进行赋值,也就是说jsp:useBean和jsp:setProperty不能够出现在为这个Bean的属性赋值的form表单的页面上。
JSP的异常处理
<%@page errorPage="xxx.jsp"%> 指定本页面出现异常后要转到的页面
& ......
JSP2.0中的表达式语言(EL表达式)
EL语法
EL的语法很简单,他最大的特点就是使用上很方便
例:
${sessionScope.user.sex}
所有EL都是以 ${ 为起始、以} 为结尾的。
上述EL范例的意思是:从Session取得用户的性别。如果使用之前JSP代码的写法如下:
<%
User user = (User)session.getAttribute("user");
& ......