jsp内置对象application
在更大范围内保存数据
作用域对比:
request 单次请求
session N次请求 一次对话
application 整个应用 N次对话
作用域比较
page <request<session<application
cookie:
cookie是web服务器保存在客户端的一系列文本信息、
每个浏览器独享各自的cookie 不能交叉访问
内存级别 :关闭浏览器cookie生命周期结束
硬盘级别:有效的生命周期内 永久保留
cookie的作用
对特定对象的追踪
统计网页浏览次数
简化登录
安全性能:容易信息泄露
cookie只能存储字符串
不能存储中文如果存可以转化为Unicode编码
cookIe 对象的几个常用方法
设置cookie的有效期 以秒为单位
//记录
Cookie cookie=new Cookie("txtname",name);
Cookie cookpwd=new Cookie("txtpwd",pwd);
//添加
response.addCookie(cookie);
response.addCookie(cookpwd);
<%@ 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>开始页面</title>
</head>
<body>
<form action="do.jsp" method="get">
用户名:<input type="text" name="txtname"/>
密码: <input type="password" name="txtpwd"/>
<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+"/";
%>
<%
//1.防止乱码
request.setCharacterEncoding("utf-8");
//2.接收数据
String name=request.getParameter("txtname");
String pwd=request.getParameter("txtpwd");
//3.跳转
if("admin".equals(name)&&"admin".equals(pwd))
{
//认证通过
//转发到success.jsp
//记录session ,用Session记录登录身份
session.setAttribute("names", name);
//session.setMaxInactiveInterval(60);
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{response.sendRedirect("begin.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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
欢迎您 <%=session.getAttribute("names") %>
<hr/>
<a href="<%=path %>/logout.jsp">注销</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
//1.清除session
session.removeAttribute("txtname");
//2.跳转到登录
response.sendRedirect("/day03/begin.jsp");
%>