目录
1.Jsp九大内置对象概述:
在 Jsp 开发中,Jsp 提供了 9 个内置对象,这些内置对象将由容器为用户进行实例化,用户直接使用即可。这个 9 个内置对象分别是:pageContext,request,response,session,application,config,out,page,exception;常用的是前面 5 个;
2.Jsp四大作用域概述:
在 Jsp 开发中,可以保存数据,Jsp 提供了四种数据保存范围;分别是 page,request,session,application;
Page 范围:只在一个页面中保存数据; javax.servlet.jsp.PageContext(抽象类)
Request 范围:只在一个请求中保存数据; javax.servlet.http.HttpServletRequest(接口)
Session 范围:在一次会话范围中保存数据,仅供单个用户使用;javax.servlet.http.HttpSession(接口)
Application 范围:在整个服务器上保存数据,所有用户共享;javax.servlet.ServletContext(接口)
2.1 page范围
Page 范围:只在一个页面中保存数据; javax.servlet.jsp.PageContext(抽象类)
page范围类似于看短信,只能在本台手机上看短信;
<%@ 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>
<%
//设置两个page范围内的数据,key -> value
pageContext.setAttribute("name", "Tom");
pageContext.setAttribute("age",22);
%>
<%
//取值
String name=(String)pageContext.getAttribute("name");
int age=(Integer)pageContext.getAttribute("age");
%>
<font>姓名:<%=name %></font>
<font>年龄:<%=age %></font>
</body>
</html>
运行结果:
已经成功获取到了信息!
2.2 request范围
Request 范围:只在一个请求中保存数据; javax.servlet.http.HttpServletRequest(接口)
request范围类似于发短信,可以在另一台手机上接受短信;
requestScope.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范围内的数据,key -> value
request.setAttribute("name", "request Tom");
request.setAttribute("age",22);
%>
<jsp:forward page="requestTarget.jsp"></jsp:forward>
</body>
</html>
requestTarget.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=(String)request.getAttribute("name");
int age=(Integer)request.getAttribute("age");
%>
<font>姓名:<%=name %></font>
<font>年龄:<%=age %></font>
</body>
</html>
运行结果:
2.3 session范围
Session 范围:在一次会话范围中保存数据,仅供单个用户使用;javax.servlet.http.HttpSession(接口)
session范围类似于打电话,可以随时在另一台手机上查看本台手机内容,默认时间为半小时!
<%@ 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>
<%
//设置两个session范围内的数据,key -> value
session.setAttribute("name", "session Tom");
session.setAttribute("age",22);
%>
<h1>session值设置完毕!</h1>
</body>
</html>
sessionTarget.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=(String)session.getAttribute("name");
int age=(Integer)session.getAttribute("age");
%>
<font>姓名:<%=name %></font>
<font>年龄:<%=age %></font>
</body>
</html>
期间只要sessionScope.jsp的运行页面一直打开,且用同一个浏览器,就可以接收到数据了;
2.4 application范围
Application 范围:在整个服务器上保存数据,所有用户共享;javax.servlet.ServletContext(接口)
applicationScope.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>
<%
//设置两个application范围内的数据,key -> value
application.setAttribute("name", "application Tom");
application.setAttribute("age",22);
%>
<h1>application值设置完毕!</h1>
</body>
</html>
applicationTarget.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=(String)application.getAttribute("name");
int age=(Integer)application.getAttribute("age");
%>
<font>姓名:<%=name %></font>
<font>年龄:<%=age %></font>
</body>
</html>
运行结果: