JavaWeb-03 cookie 重定向 application 四大范围对象

Cookie

javaWeb:
        请求转发:地址栏不变,数据保留,1次请求。
        重定向:地址栏变,数据不保留,2次请求,2次响应
    cookie:服务端产生,发送给客户端保存,本地缓存. key-v 对
    方法:
        Cookie
        getName() :就是key
        getValue():
        setMaxAge():
        给客户端发送Cookie
            response.addCookie
        客户端获取Cookie:
            request.getCookies()
        不能获取单个Cookie,只能获取全部Cookie

 服务端产生Cookie,发送给客户端。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
	//服务端增加Cookie
	Cookie cookie1 = new Cookie("name","zs");
	Cookie cookie2 = new Cookie("pwd","abc");

	response.addCookie(cookie1);
	response.addCookie(cookie2);
	
	//页面跳转到客户端
	response.sendRedirect("result.jsp");
%>
</body>
</html>

客户端打印Cookie

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		//客户端拿Cookie
		Cookie[] cookie = request.getCookies();
		for(Cookie cookies:cookie){
			out.print(cookies.getName()+"----</br>"+cookies.getValue());
		}
	%>
</body>
</html>

例子:自动记住用户名,密码

客户端login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<%!
		String name;
	%>
	<%
		//页面跳转之后拿Cookie
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie:cookies){
			if(cookie.getName().equals("name"));
			name = cookie.getValue();
		}
	
	%>

	<form action="check.jsp" method="post">
		用户名:<input type="text" name="uname" value="<%=(name==null?"":name)%>">
		密码:<input type="password" name="upwd">
		<input type="submit" value="登陆">
	
	</form>
</body>
</html>

服务端check.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<%
	
	//服务端
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("upwd");
		
		//将用户名密码加入到Cookie中
		Cookie cookie1 = new Cookie("name",name);
		//Cookie cookie2 = new Cookie("pwd",pwd);
		
		response.addCookie(cookie1);
		response.sendRedirect("A.jsp");
		
	%>
</body>
</html>

流程:logion.jsp->check.jsp->A.jsp(返回给客户端cookie)

session(会话)

开始-结束
        客户端第一次请求服务端时,服务端会产生一个session对象,每个session对象会有一个唯一的sessionid,
        服务端产生一个cookie,cookie的 name= JSESSIONID,value=服务端sessionid的值;然后把该Cookie发送给客户端;
        第二次之后的请求,服务端会先用客户端cookie的JSESSIONID,去服务端的session中匹配Sessionid

Session的方法:
            String getID();
            boolean isNew()
            void invalidate();使session失效
            setMaxInactiveInterval():有效活动时间

模拟登陆,使用session使session失效

login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<form action="check.jsp" method="post">
	用户名:<input type="text" name="uname">
	密码:<input type="password" name="upwd">
	<input type="submit" value="登陆">
	</form>
	
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<%
	
	//服务端
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("upwd");
		if(name.equals("zs") && pwd.equals("abc"))
		{
			//登陆成功给一个session,登陆成功有一个
			session.setAttribute("uname",name);
			session.setAttribute("upwd",pwd);
			//设置最大有效活动时间
			session.setMaxInactiveInterval(10);
			//跳转到欢迎页面
			request.getRequestDispatcher("welcome.jsp").forward(request, response);
			
			
		}else{
			response.sendRedirect("login.jsp");
		}
		
	%>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<%
	
	//服务端
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("upwd");
		if(name.equals("zs") && pwd.equals("abc"))
		{
			//登陆成功给一个session,登陆成功有一个
			session.setAttribute("uname",name);
			session.setAttribute("upwd",pwd);
			//设置最大有效活动时间
			session.setMaxInactiveInterval(10);
			//跳转到欢迎页面
			request.getRequestDispatcher("welcome.jsp").forward(request, response);
			
			
		}else{
			response.sendRedirect("login.jsp");
		}
		
	%>
</body>
</html>

        request:只在同一次请求有效,地址栏回车相当于发起第二次请求,所以数据报空指针。
        session:同一次会话共享,同一个浏览器session共享。

        application:整个项目的全局对象
            getRealPath():

//重定向
response.sendRedirect("");

    四个范围对象:
        pageContext:页面容器,当前页面有效(页面跳转无效)
        request:同一次请求有效,其他请求无效(请求转发有效,重定向无效)
        session:同一次会话有效,无论页面怎么跳转,都有效,关闭/切换浏览器后无效。登陆->退出之间有效
        application    :全局有效,整个项目期间有效(切换浏览器仍然有效),关闭服务 其他项目无效。多个项目仍有效JNDI

尽量使用小的范围对象,大的范围对象开销更大。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值