计数1

String ip=request.getRemoteAddr();

获取用户ip

记录新用户

<jsp:useBean id="mycount" class="com.pp.db.CountOnline "></jsp:useBean> <%
     String ip=request.getRemoteAddr();
    
    
     //获得用户ip地址
     mycount.setUserip(ip);
     ResultSet rs=mycount.checkuser();
     rs.last();
     int num=rs.getRow();
   %>
   <table>
  
    <tr bgcolor="lightgrey">
     <td>
      访问者ip地址
     </td>
       <td>
       访问次数
     </td>
   
    </tr>  
    <%
     rs.beforeFirst();
     while(rs.next()){
     %>
   
     <tr>
     <td>
       <%=rs.getString("user_ip") %>
     </td>
      <td>
     <%=rs.getInt("user_times") %>
     </td>
   
    </tr>  
  
   <%} %>
   <tr>
    <td colspan="2">
       你的ip为:<%=ip %>
       <br>
       你的访问次数为:<%=mycount.getTimes() %>次
       共有<%=num %>个新用户访问过本页
     </td>
  
   </tr>
   </table>


    package com.pp.db;
import java.sql.*;
import java.sql.SQLException;


public class CountOnline {
 
 private String userip;
 private String nowdate;
 private int times;
 private DBOperation db=new DBOperation();
 public CountOnline()
 {}
 public ResultSet checkuser(){
  ResultSet rs=null;
  String sql="select * from usercount where user_ip='"+this.userip+"'";
 
  try {
   rs=  db.executeQuery(sql);
   if(rs.next())
   {
    this.times=rs.getInt("user_times")+1;
    sql="update usercount set user_times="+this.times+" where user_ip='"+this.userip+"'";
    db.executeUpdate(sql);
   }
   
   else
   {
    this.times=1;
    sql="insert into usercount(user_ip,user_times) values('"+this.userip+"',1)";
    db.executeUpdate(sql);
   }
   rs=db.executeQuery("select * from usercount");
   
   
  } catch (SQLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  
  
  return rs;
  
 }

 
 public void dbclose()
 {
  db.closeConnection();
  
 }
 public String getNowdate() {
  return nowdate;
 }
 public void setNowdate(String nowdate) {
  this.nowdate = nowdate;
 }
 public int getTimes() {
  return times;
 }
 public void setTimes(int times) {
  this.times = times;
 }
 public String getUserip() {
  return userip;
 }
 public void setUserip(String userip) {
  this.userip = userip;
 }

}

String ip=request.getRemoteAddr();

获取用户ip

记录新用户

<jsp:useBean id="mycount" class="com.pp.db.CountOnline "></jsp:useBean> <%
     String ip=request.getRemoteAddr();
    
    
     //获得用户ip地址
     mycount.setUserip(ip);
     ResultSet rs=mycount.checkuser();
     rs.last();
     int num=rs.getRow();
   %>
   <table>
  
    <tr bgcolor="lightgrey">
     <td>
      访问者ip地址
     </td>
       <td>
       访问次数
     </td>
   
    </tr>  
    <%
     rs.beforeFirst();
     while(rs.next()){
     %>
   
     <tr>
     <td>
       <%=rs.getString("user_ip") %>
     </td>
      <td>
     <%=rs.getInt("user_times") %>
     </td>
   
    </tr>  
  
   <%} %>
   <tr>
    <td colspan="2">
       你的ip为:<%=ip %>
       <br>
       你的访问次数为:<%=mycount.getTimes() %>次
       共有<%=num %>个新用户访问过本页//用一个ip来算一个计数,用此num来表示
     </td>
  
   </tr>
   </table>


    package com.pp.db;
import java.sql.*;
import java.sql.SQLException;


public class CountOnline {
 
 private String userip;
 private String nowdate;
 private int times;
 private DBOperation db=new DBOperation();
 public CountOnline()
 {}
 public ResultSet checkuser(){
  ResultSet rs=null;
  String sql="select * from usercount where user_ip='"+this.userip+"'";
 
  try {
   rs=  db.executeQuery(sql);
   if(rs.next())
   {
    this.times=rs.getInt("user_times")+1;
    sql="update usercount set user_times="+this.times+" where user_ip='"+this.userip+"'";
    db.executeUpdate(sql);
   }
   
   else
   {
    this.times=1;
    sql="insert into usercount(user_ip,user_times) values('"+this.userip+"',1)";
    db.executeUpdate(sql);
   }
   rs=db.executeQuery("select * from usercount");
   
   
  } catch (SQLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  
  
  return rs;
  
 }

 
 public void dbclose()
 {
  db.closeConnection();
  
 }
 public String getNowdate() {
  return nowdate;
 }
 public void setNowdate(String nowdate) {
  this.nowdate = nowdate;
 }
 public int getTimes() {
  return times;
 }
 public void setTimes(int times) {
  this.times = times;
 }
 public String getUserip() {
  return userip;
 }
 public void setUserip(String userip) {
  this.userip = userip;
 }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值