ServletContext的5大作用:
1.获取web的上下文路径
String getContextPath();
2.获取全局的参数
String getInitParameter(String name);
Enumeration getInitParameterNames();
3.和域对象相关的
void setAttribute(String name,Onject object);
Object getAttribute(String name);
void removeAttribute(String name);
域对象(域对象就是在不同资源之前来共享数据,保存数据,获取数据)
ServletContext是我们学习的第一个域对象(Servlet共有三个域对象ServletContext、HttpServletRequest、HttpSession)
4. 请求转发的
RequestDispatcher getRequestDispatcher(String path);
5.读取web项目的资源文件
String getRealPath(String path);
InputStream getResourceAsStream(String path);
URL getResource(String path);
EL:
语法:
${EL表达式}
与JSP表达式区别:
JSP表达式语法:<%=变量%> 输出的脚本中变量
EL输出的是作用域中的变量
<body>
<%
int m=5;
//把变量放到作用域
pageContext.setAttribute("m", 10);
%>
JSP表达式输出:<%=m%> <br/>
EL输出:${m}
</body>
EL表达式的作用:
获取作用域中的数据
用于计算,如:算术运算,逻辑运算,关系运算
获取数据
在四个域中从小到大自动查找: ${变量名}
指定域获取数据
页面域 <%=pageContext.getAttribute(“变量名”)%> ${pageScope.变量名}
请求域 <%=request.getAttribute(“变量名”)%> ${requestScope.变量名}
会话域 <%=session.getAttribute(“变量名”)%> ${sessionScope.变量名}
上下文域 <%=application.getAttribute(“变量名”)%> ${applicationScope.变量名}
自动查找 <%=pageContext.findAttribute(“变量名”)%> ${变量名}