JSP中的九大内置对象及四大作用域:
在JSP开发中,JSP提供了九个内置对象,这些内置对象将由容器为用户进行实例化,用户直接使用
即可。这九个内置对象分别是:pageContext、request、session、application、config、
out、page、exception。常用的是前面五个,需要熟练掌握。
在JSP开发中,可以保存数据,JSP提供了四种数据保存范围,分别是:page、request、session、application。
JSP四大作用域:
page:只在一个页面中保存数据:javax.servlet.jsp.PageContext(抽象类)
pagescape.jsp
<body>
<!-- page保存数据 key->value -->
<%
pageContext.setAttribute("name", "zhangsan");
pageContext.setAttribute("age", 18);
%>
<%
String name =(String)pageContext.getAttribute("name");
int age = (Integer)pageContext.getAttribute("age");
%>
<font>
name: <%=name %><br/>
age: <%=age %>
</font>
</body>
request:只在一次请求中保存数据:javax.servlet.http.HttpServletRequest(接口)
requestscape.jsp:
<body>
<!-- request保存数据 -->
<%
request.setAttribute("name", "zhangsan");
request.setAttribute("password", "123456");
%>
<jsp:forward page="tergen.jsp"></jsp:forward>
</body>
requesttergen.jsp :
<%@page import="java.util.Enumeration"%>
<%@ 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");
String password = (String)request.getAttribute("password");
//获取头信息
Enumeration enu= request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName = (String)enu.nextElement();
String headerValue = request.getHeader(headerName);
%>
<h4>headerName:<%=headerName %> headerValue:<%=headerValue %></h4>
<%
}
%>
<font>
name: <%=name %><br/>
password: <%=password %>
</font>
</body>
</html>
session:在一次会话中保存数据,仅供单个用户使用:javax.servlet.http.HttpSession(接口)
sessionscape.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>
<!-- session设置值,只要浏览器不关闭,保存的值就能取到 -->
<%
session.setAttribute("name", "zhangsan");
session.setAttribute("password", "123456");
%>
<h1>session值设置完毕</h1>
</body>
</html>
sessiontergen.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");
String password = (String)session.getAttribute("password");
%>
<font>
name: <%=name %><br/>
password: <%=password %>
</font>
</body>
</html>
application:在整个服务器上保存数据,所有用户共享:javax.servlet.ServletContext(接口)
applicationscape.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设置值,设置的值保存在服务器里面,不同的服务器都能访问 -->
<%
application.setAttribute("name", "zhangsan");
application.setAttribute("password", "123456");
%>
<h1>application值设置完毕</h1>
</body>
</html>
applicationtergen.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");
String password = (String)application.getAttribute("password");
%>
<font>
name: <%=name %><br/>
password: <%=password %>
</font>
</body>
</html>
responce对象:
Response内置对象和request内置对象是相对应的,response内置对象用于响应客户请求,
向客户端输出信息:javax.servlet.HttpServletResponse(接口)
1、自动刷新应用
<!-- 自动刷新应用 -->
<%
//每隔1秒刷新一次
response.setHeader("refresh", "1");
//获取当前时间
Date myData = new Date();
%>
当前时间:<%= myData.toLocaleString() %>
2、页面重定向应用 客户端跳转
<%
//重定向,客户端,页面跳转,不能带参数
response.sendRedirect("index.html");
%>
3、操作cookie应用 post/get方法比较 post放数据包里 get放Url后面 get数据量小,不安全
<%@ 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>
<%
String name = null;
String password = null;
Cookie[] cookies = request.getCookies();
for(int i = 0; cookies != null && i < cookies.length;i++){
if(cookies[i].getName().equals("uasernameAndPwd")){
name = cookies[i].getValue().split("-")[0];
password = cookies[i].getValue().split("-")[1];
}
}
if(name == null){
name = "";
}
if(password == null){
password = "";
}
%>
</head>
<body>
<div align="center">
<form action="userlogin.jsp" method="post">
<table>
<tr>
<td>UserName:</td>
<td><input type="text" id="username" name="username" value="<%= name %>"/></td>
</tr>
<tr>
<td>PassWord</td>
<td><input type="password" id="password" name="password" value="<%= password %>"/></td>
</tr>
<tr>
<td>记住密码</td>
<td><input type="checkbox" id="remeber" name="remeber" value="remember-me"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ 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("username"); //获取用户账号
String password = request.getParameter("password"); //获取用户密码
String remeber = request.getParameter("remeber"); //记住密码
if("remember-me".equals(remeber)){
Cookie uasernameAndPwd = new Cookie("uasernameAndPwd",name+"-"+password);//创建Cookie
uasernameAndPwd.setMaxAge(1*60); //设置Cookie保存时间,默认是一秒
response.addCookie(uasernameAndPwd); // 保存cookie
System.out.print("Cookie设置成功!");
}
response.sendRedirect("index.jsp");
%>
</body>
</html>
4、cookie和session的比较:cookie信息是存在客户端 session信息存放在服务器中
out对象:
Out内置对象主要用来向客户端输出各种类型的数据,同时还可以管理应用服务器上的输出缓冲区。所以out内置
对象的方法是向客户端输出数据和管理缓存区:javax.servlet.jsp.JspWriter(抽象类)
config对象:
Config内置对象是JSP页面通过JSP容器进行初始化时被传递的对象:javax.servlet.ServletConfig
在Servlet初始化的时候,JSP引擎通过config向它传递出信息,这种信息可以是属性名和属性值匹配的参数,也可以
是通过ServletContext对象传递的服务器的有关信息。
exception对象:
Exception内置对象用来处理JSP文件在执行时发生的所有异常,它是java.lang.Throwable类的一个对象
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="Exception02.jsp"%>
<!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>
<%
int a = 1;
int b = 0;
out.print(a/b);
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!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>
<!-- 当Exception01.jsp发生错错误了会跳转到该页面 -->
<%
if(exception != null){
out.print("<h1>");
out.print("Error Jsp!");
out.print("</h1>");
}
%>
</body>
</html>
pageContext对象:
PageContext内置对象是一个比较特殊的对象。它相当于页面中所有对象功能的集合,即使用它可以访问到本页面
中所有对象。pageContext内置对象由Jsp容器创建并初始化,pageContext对象提供了对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>
<%
pageContext.setAttribute("name01", "pageInfo");
request.setAttribute("name02", "requestInfo");
session.setAttribute("name03", "sessionInfo");
application.setAttribute("name04", "applicationInfo");
out.println("使用pageContext<br/>");
out.println("page的属性值:" + pageContext.getAttribute("name01")+"<br/>");
out.println("request的属性值:" + pageContext.getRequest().getAttribute("name02")+"<br/>");
out.println("session的属性值:" + pageContext.getSession().getAttribute("name03")+"<br/>");
out.println("application的属性值:" + pageContext.getServletContext().getAttribute("name04")+"<br/>");
%>
</body>
</html>