用jsp内置对象网站计数

jianli.jsp(写的很粗糙,找空改进下!)

<%@ 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>
<title>简历</title>
<style type="text/css">
input{
  width:100%; height:100%; border: none;
}
</style>
</head>

<body>
<h3>简历</h3>
<form action="requestdemo.jsp" method="post">
<table cellpadding="0" cellspacing="0" border="1">
  <tr>
    <td width="100" align="center">姓名</td>
    <td width="100" align="center"><input type="text" name="name" /></td><!--input标签里有name跳转到的requestdemo里才能request.getParameter("name")-->
    <td width="100" align="center">性别</td>
    <td width="100" align="center"><input type="text"  name="sex"/></td>
    <td width="100" align="center">出生年月</td>
    <td width="100" align="center"><input type="text" /></td>
    <td width="100" rowspan ="4" align="center"></td>
  </tr>
  <tr>
    <td width="100" align="center">民族</td>
    <td width="100 align="center""><input type="text" /></td>
    <td width="100" align="center">政治面貌</td>
    <td width="100" align="center"><input type="text" /></td>
    <td width="100" align="center">身高</td>
    <td width="100" align="center"><input type="text" /></td>
  </tr>
  <tr>
    <td width="100" align="center">学制</td>
    <td width="100" align="center"><input type="text" /></td>
    <td width="100" align="center">学历</td>
    <td width="100" align="center"><input type="text" /></td>
    <td width="100" align="center">户籍</td>
    <td width="100" align="center"><input type="text" /></td>
  </tr>
  <tr>
    <td width="100" align="center">专业</td>
    <td width="100" align="center"><input type="text" /></td>
    <td width="200" colspan="2" align="center">毕业学校</td>
    <td colspan="2" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td colspan="7" align="center">技能、特长或爱好</td>
  </tr>
  <tr>
    <td width="100" align="center">外语等级</td>
    <td colspan="2" align="center"><input type="text"/></td>
    <td width="100" align="center">计算机</td>
    <td colspan="3" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td colspan="7" align="center">个人履历</td>
  </tr>
  <tr>
    <td width="100" align="center">时间</td>
    <td colspan="2" align="center">单位</td>
    <td colspan="4" align="center">经历</td>
  </tr>
  <tr>
    <td width="100" align="center"> 20<input type="text" style="width: 15; height: 100%" /><input type="text" style="width: 15; height: 100%" /></td>
    <td colspan="2" align="center"><input type="text"/></td>
    <td colspan="4" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td width="100" align="center"> 20<input type="text" style="width: 15; height: 100%" /><input type="text" style="width: 15; height: 100%" /></td>
    <td colspan="2" align="center"><input type="text"/></td>
    <td colspan="4" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td width="100" align="center"> 20<input type="text" style="width: 15; height: 100%"/><input type="text" style="width: 15; height: 100%"/></td>
    <td colspan="2" align="center"><input type="text"/></td>
    <td colspan="4" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td colspan="7" align="center">联系方式</td>
  </tr>
  <tr>
    <td align="center">通讯地址</td>
    <td colspan="3" align="center"><input type="text"/></td>
    <td align="center">联系电话</td>
    <td colspan="2" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td align="center">email</td>
    <td colspan="3" align="center"><input type="text"/></td>
    <td align="center">邮编</td>
    <td colspan="2" align="center"><input type="text"/></td>
  </tr>
  <tr>
    <td colspan="7" align="center">自我评价</td>
  </tr>
  <tr>
    <td colspan="7" align="center"><input type="text"/></td><!--用<textarea row="3" style="width: 100%;border: 0;"></textarea>看起来好看点前面的工作经历也可以用这个标签改-->
  </tr>

</table>
<input type="submit"  value="注册" style="width:50; height:25;">
</form>
</body>
</html>

这里写图片描述

textarea标签修改过后的,row设置不一样但是显示不知道为什么是一样宽。。。

requestdemo.jsp

<%@ 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>
    <title>requestdemo</title>
  </head>

  <body>
    <%!
      ArrayList<String> list = new ArrayList<String>();
      ArrayList<Long> time = new ArrayList<Long>();
      ArrayList<String> id=new ArrayList<String>();
      %><!--对象列表的定义不能和放在后面的<%...%>里面,否则每次跳转都会重新定义一次对象列表,之前存的IP时间都会不存在!同一个IP每次进去不管时间间隔都会加一!-->
      <%
      request.setCharacterEncoding("UTF-8");
      String username=request.getParameter("name");
      String sex=request.getParameter("sex");
      String ip=request.getRemoteAddr();
      long Time = System.currentTimeMillis();
      if(application.getAttribute("count")==null&ip.isEmpty()){
        application.setAttribute("count",new Integer(0));

      }
      int a=(Integer)application.getAttribute("count");


      if(!list.contains(ip)){

        list.add(ip);
        time.add(Time);
        application.setAttribute("count",a+1);
      }
      else{
        int i = list.indexOf(ip);
        Long time1 = time.get(i);
        time.set(i,Time);
        if(Time - time1 > 10000){
          application.setAttribute("count",a+1);
        }
      }
    }
    %>
    你是第<%=application.getAttribute("count")%>位访问者</br>


    姓名:<%=username %></br>
    性别:<%=sex %></br>
  </body>
</html>

参考另一种写法requestdemo.jsp

<%!ArrayList<String> id=new ArrayList<String>();
    ArrayList<String> ip=new ArrayList<String>();
    ArrayList<Long> time=new ArrayList<Long>();
    %>
    <%
       Integer count = (Integer)application.getAttribute("count");
        if (application.getAttribute("count") == null&&ip.isEmpty()) {
           application.setAttribute("count",0);
        } 
        int a=(Integer)application.getAttribute("count");
        String IP=request.getRemoteAddr();
        if(!ip.contains(IP)){
        ip.add(IP);
        application.setAttribute("count",a+1);
        time.add(session.getCreationTime());
        }
        else{
        int i=ip.indexOf(IP);
        Long oldtime=time.get(i);
        Long newtime=session.getCreationTime();
        time.set(i,newtime);
        if((newtime-oldtime)>20000){  

        String sessionID=session.getId();
        boolean contain=id.contains(sessionID);
        if(!contain){
        id.add(sessionID);
         application.setAttribute("count",a+1);
         }
        }
        }     
        out.println("你好,你是第"+application.getAttribute("count")+"访问的人");
%>

同一个IP10秒内不管换不换浏览器访问量都不增加
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值