用jsp内置对象网站计数

来源:互联网 发布:淘宝300多的充气娃娃 编辑:程序博客网 时间:2024/06/02 04:24

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
原创粉丝点击