out:用于页面输出
request:得到用户请求的信息
response:服务器向客户端的回应信息
config:服务器的配置,可以取得初始化参数
session:用来保存客户的信息
applicati:所有用户的共享信息
pageContext:jsp页面容器
exception:jsp页面发生的异常,在错误页面才可以使用
out对象:负责往页面输出,往客户端输出内容
out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲 器(buffer)
<%@ 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> aaaaaaa <% out.write("bbbbbbb"); response.getWriter().write("cccccc"); %> <%="dddddd" %> </body> </html>
顺序为:cabd 先走response缓冲区,在走out缓冲区,当servlet执行结束时,关闭out缓冲区把它们拿下来放在response缓冲区的底下,就形成了cabd(仅提供给自己看 - -)
pageContext范围只是仅仅在本页当中 它是一个域对象!
格式:
setAttribute(String name,Object obj)
getAttribute(String name)
removeAttrbute(String name)
pageContext可以向指定的其他域中存取数据
setAttribute(String name,Object obj,int scope)(int值:指的范围)
getAttribute(String name,int scope)
removeAttrbute(String name,int scope)
<%@ 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("goods", "奶瓶"); /*向request域中设置值*/ pageContext.setAttribute("goods","脑袋",pageContext.REQUEST_SCOPE); /*向Session域中设置值*/ pageContext.setAttribute("goods","方便面",pageContext.SESSION_SCOPE); /*向application域中设置值*/ pageContext.setAttribute("goods","快乐水",pageContext.APPLICATION_SCOPE); %> <% 从request域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.REQUEST_SCOPE)); 从Session域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.SESSION_SCOPE)); 从application域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.APPLICATION_SCOPE)); %> </body> </html>
findAttribute(String name)(所以尽量键的name别重复!)
---依次从pageContext域,request域,session域,application域中获取属性,在某个域中获取后将不在向后寻找
<%@ 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("goods", "奶瓶"); /*向request域中设置值*/ pageContext.setAttribute("goods","脑袋",pageContext.REQUEST_SCOPE); /*向Session域中设置值*/ pageContext.setAttribute("goods","方便面",pageContext.SESSION_SCOPE); /*向application域中设置值*/ pageContext.setAttribute("goods","快乐水",pageContext.APPLICATION_SCOPE); %> <% String name=(String)pageContext.findAttribute("goods"); System.out.println(name); %> </body> </html>
四大作用域的总结:
pageContext域:当前jsp页面范围
request域:一次请求
session域:一次会话
application域:整个web应用
所有的 EL 都是以 ${ 为起始,以 } 为结尾
<%@page import="java.util.ArrayList"%> <%@page import="com.oracle.domain.User"%> <%@ 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("name","张三"); /*向域中存储对象*/ User user=new User(); user.setName("张三"); user.setAge(2); request.setAttribute("user",user); /*向域中存储集合*/ User user2=new User(); user2.setName("李四"); user2.setAge(3); ArrayList<User> arr=new ArrayList<User>(); arr.add(user); arr.add(user2); application.setAttribute("list",arr); %> <!--el表达式取值--> ${name} <!--key.属性名取值--> ${user.name}... ${user.age} ${list[0].name} ${list[1].age} <!--获取Web应用名称--> ${pageContext.request.contextPath} <br> ${1+2 } <!--判断key值--> ${empty name} <!--判断name属性里面的值是不是为空!--> ${name==null?"用户名不存在":"用户名已存在"} </body> </html>