Jsp内置对象对象是web容器创建的一组对象,无需使用new关键词实例化,即可调用的内置对象。
9大内置对象包括以下:
- 1.输出对象out –JspWriter
- 2.请求对象request –ServletRequest
- 3.响应对象reponse –ServletResponse
- 4.配置对象config –ServletConfig
- 5.会话对象session –HttpSession
- 6.应用程序对象application –ServlerContext
- 7.页面对象page –HttpJspPage
- 8.页面上下文对象pageContext –PageContext
- 9.异常对象exception –Exception
1、out对象
JspWriter类实例,是向客户端负责输出内容的。
常用方法如下:
- println();
- clear (),如果在flush()之后调用会抛出异常。
- clearBuffer();
- flush();
- isAutoFlush();
例子:
out内置对象
<%
out.println(“
静夜思
“);out.println(“床前明月光
”);
out.println(“疑是地上霜
”);
out.flush();
//out.clear();//这里会抛出异常。
out.clearBuffer();//这里不会抛出异常。
out.println(“举头望明月
”);
out.println(“低头思故乡
”);
%>
缓冲区大小:<%=out.getBufferSize() %>byte<br>
缓冲区剩余大小:<%=out.getRemaining() %>byte<br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><BR>
“`
2、request对象
客户端的请求被封装在request对象中,通过它可以了解客户端的请求,然后作出响应,request请求具有request请求域。
常用方法:
- getParameter(String name)
- getParamterValues(String name)
- setAttribute(String name,Onject o)
- getAttribute(string name)
- getContetType();
- getProtocol()
-
getServerName();
举个例子:用户注册提交数据给request.jsp,在request.jsp页面根据request对象可以获取提交过来的数据。
reg.jsp 注册jsp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
request.jsp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
3、response对象
response对象包含了响应客户端请求的有关信息,它具有页面作用域,该页面的作用域只对该页面有效。常用方法:
- getCharacterEncoding()
- setContentType();
- getWriter();该方法打应输出流总是前于 out.println();
- sendRedirect(String location)
请求重定向和请求转发:
- 请求重定向:客户端行为:response.sendDirect();两次请求,前一次请求的请求对象不会保存,地址栏的url地址会发生改变
- 请求转发:服务器行为,request.getResuestDispatcher().forward();一次请求,转发后请求对象会保存,地址栏url地址不会变。
4、session对象
一些基本概念:
- session表示客户端与服务器的一次会话
- web中session指的是用户在浏览某个网站,是进入网站到关闭浏览器这段时间
- 它是保存在服务器的内存中,不同用户有不同的session
- 它在第一个jsp页面被装载时自动创建,完成会话期管理。
它的的一些常用方法:
- getCreationTime();
- String getId();
- setAttribute(String name,Object o);
- getAttribute(String name);
- String[] getValueNames();
- int getMaxInactivieInterval();单位 秒
- setMaxInactiveInterval();
举个例子:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
session的生命周期:
- 创建: 当客户端第一次访问某个页面jsp或者servlet,服务器会创建一个 sessionId,每次客户端向服务器发送请求时,都会将sessionId携带过去,服务器会对sessionId进行校验。
- 活动: 当客户端通过超链接打开新页面属于同一次会话;当浏览页面全部关闭,重新打开属于一次新的会话。
- 销毁:调用sesson.invalidate();session过期,默认是30分钟;服务器重启;
5、application对象
- application实现了用户数据共享,可存放全局变量。
- application 开始于服务器的重启,终止于服务器的关闭
- application 是ServletContext实例。
常用方法:
- setAttribute(String ,Object);
- getAttribute(String);
- Enumeration getAttributeNames();
- getServerInfo();返回Jsp 引擎名和版本号
举个例子:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
6、page对象
page对象就是指当前jsp页面本身,有点像this指针,它是Java.lang.Object类的实例。常用的方法就是Object 类的方法。
- getClass()
- hashCode();
- equals();
- copy();
- clone()
- toString();
- notify();
- notifyAll();
- wait();
7、pageContext对象
- pageContext 提供了对jsp页面所有的对象及名字空间的访问
- 它可以取application 某一属性,也可以取session;
- 相当于页面所有功能的集大成者。
方法:
- getOut()
- geSession();
- getPage();
- getReuest();
- getResponse();
- setAttribute();
- getAttibute();
- getAttributeScope();
- forward();
- include();
8、config对象
它是在一个servlet初始化时,jsp页面用它传递信息,比如servlet初始化参数;以及服务器的有关信息。
- ServletContext getServletContext();
- getInitParameter(String);
- Enumeration getInitParameterNames();
9、exception对象
即异常对象。如果一个jsp想要用此对象,就必须把isErrorPage 设为true.