JSP 数据交互 使用application记录访问人数+cookie的使用

来源:互联网 发布:用单片机测量电能 编辑:程序博客网 时间:2024/05/16 07:10

1cookie:实现session基础

2.cookie分类:会话级别:没有设置期限(setMaxAge())

                           硬盘级别:有设置期限(setMaxAge())

cookie不是内置对象

cookie形成在服务器端,保存在客户端



page>request>response>application   都是内置对象一共9个


<%@ 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 'myapp.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>   <%   Integer count=(Integer)application.getAttribute("count");   if(count!=null){   count++;     }else{   count=1;   }   application.setAttribute("count", count);       %>   一共有<%=count %>位访问  </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 'login.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>  <%  Cookie[] cookies=request.getCookies();  if(cookies!=null){  for(int i=0;i<cookies.length;i++){  if(cookies[i].getName().equals("cookieName")){  response.sendRedirect(path+"/sccece.jsp");  }  }   }  %>  <%=path %>    <form name="form1" method="post" action="do.jsp">   用户名:<input type="text" name="username">      密码:<input type="password" name="pwd">   <input type="submit" value="登录">   </form>  </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 'do.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>  <%="=====================do==============" %> <br>       <%   request.setCharacterEncoding("utf-8");   String name=request.getParameter("username");   String pwd=request.getParameter("pwd");     if(name.equals("1")&&pwd.equals("1")){//request.setAttribute("name", name);//request.setAttribute("pwd", pwd);session.setAttribute("name", name);//创建cookie  Cookie cookie=new Cookie("cookieName",name);//设置cookie的过期时间  cookie.setMaxAge(60*5);  //将cookie响应给客户端  response.addCookie(cookie);    request.getRequestDispatcher("/sccece.jsp").forward(request, response);     }else{   //转发request.setAttribute("name", name); request.getRequestDispatcher("login.jsp").forward(request, response);   //重定向   response.sendRedirect(path+"login.jsp");   }   %>  </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 'sccece.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>   <%="=======================sccece===========" %>  </body></html>

限定时间 登录一次可以保存的时间是5分钟  如果在5分钟内你登录了 直接跳转到sccece页面 ,如果你5分钟后登录   你就得重新登录才可以

不懂私信我

0 0
原创粉丝点击