jsp制作计数器,自动登录,定时刷新等简单小应用

页面访问量计数器

<body>

<h1>实现网页计数器</h1>

<%

 out.print("设置计数值");

Integer c;

if(application.getAttribute("count")==null){

   c=1;

}else{

   c=(Integer.parseInt(application.getAttribute("count").toString()));

}

application.setAttribute("name","zyq");

application.setAttribute("count",c);

out.print("set name = zyq");

out.print("</br>set count ="+c+"</br>");

%>

<ahref="<%=basePath%>jsp/jsp/gateppatter.jsp">计数器页面</a>

</body>

 

gateppatter.jsp

<body>

计数器页面

<br>

<%

int c=(Integer.valueOf(application.getAttribute("count").toString()).intValue());  

out.print(c);

application.setAttribute("count",Integer.toString(c+1));

 

%>

</body>

 

自动登录(用户名和密码:)<使用cookie技术>

<body>

<%

String welcome="首次访问" ;

String[] info = new String[]{ "",""};

Cookie[] cookie =request.getCookies();

if(cookie!=null){

  

   for(inti=0;i<cookie.length;i++){

     if(cookie[i].getName().equals("zyqcookie")){

        info =cookie[i].getValue().split("#");

     }

   } 

}

 

%>

<formaction ="<%=basePath%>jsp/jsp/valid.jsp"method="post">

      <table>

         <tr><th>用户名</th>

                <td><inputtype="text"name="name"id="name"value="<%=info[0]%>"/></td>

         </tr>

         <tr><th>密码</th>

                <td><inputtype="text"name="password"id="password"value="<%=info[1]%>"/></td>

         </tr>

         <tr>

            <inputtype="submit"value="提交"/>

         </tr>

 

      </table>

</form>

 

</body>

 

valid.jsp

 

<body>

<%

 String name=request.getParameter("name");

Stringpassword=request.getParameter("password");

if(name.equals("zyq")&& password.equals("123")){

   Cookiezyqcookie = newCookie("zyqcookie",name+"#"+password);

   zyqcookie.setMaxAge(60*60*24*30);//一个月时间

   response.addCookie(zyqcookie);

%>

<script>

alert("用户登录成功");

</script>

<%

}

else{

   %>

<script>

alert("用户登录失败");

<%

}

%>

</script>

</body>

 

定时刷新页面

<body>

<h1>自动刷新页面</h1>

<%

response.setHeader("Refresh","60");

out.print(new Date());

%>

</body>

猜你喜欢

转载自blog.csdn.net/qi95719/article/details/53368422
今日推荐