ServletContext对象
1.什么是ServletContext对象
ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,ServletContext对象一个web应用只有一个
问题:一个web应用有几个servlet对象?----多个
ServletContext对象的生命周期?
创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状 态))
销毁:web应用被卸载(服务器关闭,移除该web应用)
2.怎样获得ServletContext对象
1)ServletContext servletContext = config.getServletContext();
2)ServletContext servletContext = this.getServletContext();
## 3.ServletContext的作用
(1)获得web应用全局的初始化参数
web.xml中配置初始化参数
通过context对象获得参数
(2)获得web应用中任何资源的绝对路径(重要 重要 重要)
方法:String path = context.getRealPath(相对于该web应用的相对地址);
(3)ServletContext是一个域对象(重要 重要 重要)
什么是域对象?什么是域?
存储数据的区域就是域对象
ServletContext域对象的作用范围:整个web应(所有的web资源都可以随意向 servletcontext域中存取数据,数据可以共享)
域对象的通用的方法:
setAtrribute(String name,Object obj);
getAttribute(String name);
removeAttribute(String name);
EL技术
1.EL 表达式概述
EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写。
2.EL从域中取出数据(EL最重要的作用)
jsp脚本:
1)<%java代码%> ----- 内部的java代码翻译到service方法的内部
2)<%=java变量或表达式> ----- 会被翻译成service方法内部out.print()
3)<%!java代码%> ---- 会被翻译成servlet的成员的内容
page域:当前jsp页面范围
request域:一次请求
session域:一次会话
application域:整个web应用
jsp脚本:<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:${requestScope.name}
EL最主要的作用是获得四大域中的数据,格式
E
L
表
达
式
E
L
获
得
p
a
g
e
C
o
n
t
e
x
t
域
中
的
值
:
{EL表达式} EL获得pageContext域中的值:
EL表达式EL获得pageContext域中的值:{pageScope.key};
EL获得request域中的值:
r
e
q
u
e
s
t
S
c
o
p
e
.
k
e
y
;
E
L
获
得
s
e
s
s
i
o
n
域
中
的
值
:
{requestScope.key}; EL获得session域中的值:
requestScope.key;EL获得session域中的值:{sessionScope.key};
EL获得application域中的值:
a
p
p
l
i
c
a
t
i
o
n
S
c
o
p
e
.
k
e
y
;
E
L
从
四
个
域
中
获
得
某
个
值
{applicationScope.key}; EL从四个域中获得某个值
applicationScope.key;EL从四个域中获得某个值{key};
—同样是依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找
1)获得普通字符串
2)获得User对象的值
3)获得List的值
3.EL的内置对象11个
pageScope,requestScope,sessionScope,applicationScope
---- 获取JSP中域中的数据
param,paramValues - 接收参数.
相当于request.getParameter() rrquest.getParameterValues()
header,headerValues - 获取请求头信息
相当于request.getHeader(name)
initParam - 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)
cookie - WEB开发中cookie
相当于request.getCookies()—cookie.getName()—cookie.getValue()
pageContext - WEB开发中的pageContext.
pageContext获得其他八大对象
${pageContext.request.contextPath}
相当于
<%=pageContext.getRequest().getContextPath%> 这句代码不能实现
获得WEB应用的名称
4.EL执行表达式
例如:
${1+1}
${empty user}
${user==null?true:false}
JSTL核心库的常用标签
1)<c:if>标签
2)<c:for>标签