javaweb -jsp九大内置对象(3)

session会话对象

客户端第一次请求服务端时,(id匹配失败)服务端会产生一个session对象(用于保存该客户的信息)
并且每个Session对象都会有一个唯一的sessionId(用于区分其他Session)

服务端会产生一个cookie,并且 该cookie的name=JSESSIONID,value= 服务端sessionId的值
然后 服务端会在响应客户端的同时 将该cookie发送给客户端,至此 客户端就有了一个cookie(JSESSIONID)

因此,客户端的cookie就可以和服务端的session一一对应(JSESSIONID - session)

客户端第二/n请求服务端时:服务端会先用客户端的JSESSIONID与服务端中匹配sessionid,如果匹配成功
说明此用户不是第一次访问

在这里插入图片描述
session
a· session储存在服务端
b· session是在同一个用户(不同页面)请求时共享
c· 实现机制
Cookie:
a。不是内置对象,要使用必须new
b。但是,服务端会自动生成(服务端自动new一个Cookie) 一个name=JSESSIONID的cookie 并返回给客户端

session方法

String getId();获取sessionId
boolean idNew();判断是否为新用户(第一次访问)
void invalidate():使session失效(退出登陆,注销)

void setAttribute();
Object getAttribute();

void setMaxInactiveInterval(秒):设置最大有效非活动时间
int getMaxInacticeInterval();: 获取最大有效非活动时间

示例:登陆

如果服务端发现此请求没有JSESSIONID,则会创建一个name为JSESSIONId的cookie 并返回给客户端

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="check.jsp" method = "post">
		用户名:<input type="text" name="uname"><br/>
		密码:<input type="text" name="upassword"><br/>
		提交:<input type="submit" value="登陆"><br/>
	</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String password = request.getParameter("upassword");
		if(name == null){
			response.sendRedirect("login.jsp");
		}else{
			if(name.equals("yuanqi3366")&&password.equals("123456")){
				session.setAttribute("uname", name);
				session.setAttribute("upassword", password);
				session.setMaxInactiveInterval(300);
				request.getRequestDispatcher("success.jsp").forward(request,response);
				
			}else{
				out.print("用户名或密码错误");
			}
		}

	%>
	<form action="login.jsp"method = "post">
		<input type="submit" value = "back">
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	登陆成功!
	用户名:
	<%
		//String name = request.getParameter("uname");
		String name = (String)session.getAttribute("uname");
		if(name == null){
			response.sendRedirect("login.jsp");
		}
		out.print(name);
	%>
	
	<form action="newpage.jsp" method="post">
		<input type="submit" value= "check">
	</form>
</body>
</html>

tip++

session与cookie的区别
                                    session                            cookie

保存的位置                             服务端                              客户端
安全性:                               较安全                             较不安全
保存的内容                             Object                             string

application 全局对象

String getContextPath()虚拟路径
String getRealPath(STring name);绝对路径(虚拟路径相对的绝对路径)

   <%=application.getContextPath() + "<br/>"%>
   <%=application.getRealPath("/myweb/index1.jsp") %>

config 配置对象 (服务器配置对象)
out 输出对象
page 当前jsp页面对象(相当于java的this)
exception 异常对象

tip++

四种范围对象:(小->大)

pageContext jsp页面容器 (page对象) 当前页面有效(跳转后无效)
request: 请求对象 同一次请求有效(请求转发后仍有效)(重定向无效)
session会话对象 同一次会话有效
application 全局对象 全局有效(整个项目有效)

以上4个对象共有的方法:
Object getAttribute(String name);根据属性名。或属性值
void setAttribute(STring name, Object obj);设置属性值(新增,修改)
void removeAttribute(String name);根据属性名,删除对象

1.以上的4个范围对象,通过setattribute()赋值,通过getAttribute()取值
2.以上范围对象,尽量使用最小的范围,因为对象的范围越大,造成的性能消耗也越大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值