客户端在第一次请求服务端时,如果服务端发现 此请求没有 JSESSIONID,则会创建一个 name=JSESIONID的cookie 并返回给客户端
Cookie: a.不是内对对象,要使用必须new b.但是,服务端会 自动生成一个(服务端自动new一个cookie) name=JSESIONID的cookie 并返回给客户端
JSP9大内置对象
<%="当前项目的虚拟路径:" +application.getContextPath() +"<br/>" %>
<%="虚拟路径对应的绝对路径:" +application.getRealPath("/MyJspProject") +"<br/>" %>四种范围对象(小->大)
以上4个对象共有的方法:
a. pageContext 当前页面有效 (页面跳转后无效) 案例: pageContext.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>
<%
pageContext.setAttribute("hello", "world");
request.getRequestDispatcher("pc1.jsp").forward(request, response);
%>
</body>
</html>pc1.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>
pc1.jsp<br>
<%=pageContext.getAttribute("hello") %>
</body>
</html>
b. request 同一次请求有效;其他请求无效 (请求转发后有效;重定向后无效)

案例(一):request请求转发取值 request.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.setAttribute("hello", "world");
request.getRequestDispatcher("rq1.jsp").forward(request, response);
%>
</body>
</html>rq1.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>
pq1.jsp<br>
<%=request.getAttribute("hello") %>
</body>
</html>
案例(二):request重定向取值 把上面案例(一)的请求转发改为重定向
<%
request.setAttribute("hello", "world");
//request.getRequestDispatcher("rq1.jsp").forward(request, response);
response.sendRedirect("rq1.jsp");
%>再次访问:

重定向为两次请求,所以request获取不到值。c. session 同一次会话有效 (无论怎么跳转,都有效;关闭/切换浏览器后无效 ; 从 登陆->退出 之间 全部有效) session案例: session.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>
<%
session.setAttribute("hello", "world");
//request.getRequestDispatcher("rq1.jsp").forward(request, response);
response.sendRedirect("ss1.jsp");
%>
</body>
</html>ss1.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>
ss1.jsp<br>
<%=session.getAttribute("hello") %>
</body>
</html>
切换浏览器后直接访问ss1.jsp无效:

d. application
<%@ 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>
<%
application.setAttribute("hello", "world");
//request.getRequestDispatcher("rq1.jsp").forward(request, response);
response.sendRedirect("ap1.jsp");
%>
</body>
</html>ap1.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>
ap1.jsp<br>
<%=application.getAttribute("hello") %>
</body>
</html>
切换浏览器访问也有效

重启Tomcat后无效:

->多个项目共享、重启后仍然有效 :JNDI
1.以上的4个范围对象,通过 setAttribute()赋值,通过getAttribute()取值; 2.以上范围对象,尽量使用最小的范围。因为 对象的范围越大,造成的性能损耗越大。