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

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分钟后登录   你就得重新登录才可以

不懂私信我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值