分页实现以及访问量的实现

访问首页   
        内置很多子页   访问子页也增加访问量以及子页的访问量     子页中或许会碰到下一页 上一页  首页   尾页  转到选定页    详细方法请看如下代码

    首页

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
   if (application.getAttribute("count") == null) {
     application.setAttribute("count", new Integer(1));
   }
   Integer count = (Integer) application.getAttribute("count");
   application.setAttribute("count", new Integer(count.intValue() + 1));
%>
<html>
<head>
<title>页面被访问数统计</title>
</head>
<body bgcolor="#ffffff">
<form action="fwl.jsp">
<!--输出访问次数 -->
<h1>   此页面访问量
:&nbsp;<%=count%>   次
</h1>
<a href="tushu.jsp">图书</a>
<%
Object count1=application.getAttribute("count1");
if(count1==null){%>
<%=0%>
<%}else{%>
<%=count1 %>
<%}%><br/>
<a href="bijiben.jsp">笔记本</a>
<%
Object count2=application.getAttribute("count2");
if(count2==null){%>
<%=0%>
<%}else{%>
<%=count2 %>
<%}%>
</form>
</body>

</html>

子页一   

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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="styles.css">
    -->
  </head>
<title></title>
<body>

<%!  int record = 2;
     int total = 0;
%>

<%
    Connection conn;
    Statement stmt;
    String url = "jdbc:mysql://localhost:3306/3g";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url, "root", "");
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        String sql="select * from users order by id";
        ResultSet rs =  stmt.executeQuery(sql);
        rs.last();
        int ototal = rs.getRow();
        if(ototal % record == 0){
            total=ototal / record;
        }else{
            total=ototal / record +1;
        }
        int current = 1;
%>



<%

    //获取指定页面
    String appoint = request.getParameter("current");
    if (appoint == null){
        appoint = "1";
    }
    
    //类型转换
    try{
        current = Integer.parseInt(appoint);
    }
    catch (NumberFormatException ex){
        current = 1;
    }
    if(current <=1){
        current = 1;
    }
    else if(current >= total){
        current =  total;
    }
 
    int posion = (current -1 ) * record + 1;
    rs.absolute(posion);
   String cou=request.getParameter("flag");
   if(cou==null){
   if (application.getAttribute("count1") == null) {
     application.setAttribute("count1", new Integer(0));
   }
   Integer count1 = (Integer) application.getAttribute("count1");
   application.setAttribute("count1", count1+1);
   }
%>


<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>商品号</th>
<th>商品名称</th>
<th>详细信息</th>
</tr>
<%
   int i =0;
   while(i<record && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%rs.next();i++;}%>
</table>


<table width="700">


<tr>
<td>
<%if(current < total){%>
    <a href="tushu.jsp?current=<%=current+1%>&flag=1">下一页</a>
<%}%>
</td>
<td><%if(current > 1){%>
    <a href="tushu.jsp?current=<%=current-1%>&flag=1">上一页</a>
    <%}%>
</td>

<td> 共<%=total%>页 </td>
<td> 第<%=current%>页  </td>
<td> <a href="tushu.jsp?current=1&flag=1">首页</a> </td>
<td> <a href="tushu.jsp?current=<%=total%>&flag=1">尾页</a> </td>
<td></td>
<td>

</td>
</tr>

</table>
<%
conn.close();
  }catch (ClassNotFoundException e1)
   {out.print(e1.getMessage());
  }catch(SQLException e2)
   { out.print(e2.getMessage());}
%>
</body>
</html>

子页二

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'bijiben.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="styles.css">
    -->

  </head>
 
  <body>
   <h1>笔记本</h1>
       <%
   if (application.getAttribute("count2") == null) {
     application.setAttribute("count2", new Integer(0));
   }
   Integer count2 = (Integer) application.getAttribute("count2");
   application.setAttribute("count2", count2+1);
%>
  </body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值