八 -----------------------------------------表达式语言----------------------------------------------------------------
表达式语言是JSP2.0中新增的功能,使用表达式语言可以方便的访问标志位,(在jsp中一共提供page(pageContext),request,session和application四种标志位)
表达式语言使用 ${ 属性名称 }如果输出的是null则自动使用空字符串""表示
----------------------------------------表达式语言的内置对象------------------------------------------------------
在表达式语言中提供了许多的内置对象:
pageContext 表示pageContext对象
pageScope 表示从page范围查找输出属性
requestScope 表示从request属性范围查找输出属性
sessionScope 表示从session属性范围查找输出属性
applicationScope 表示从application属性范围查找输出属性
param 接受传递到本页面的参数
paramValues 接受传递到本页面的一组参数
header 接受一个头信息数据
headerValues 接受一组头数据信息
cookie 取出cookie中的数据
initParam 取得配置的初始化参数
${ 属性名称 }可以从不同的属性保存范围中取得属性,默认的顺序是page->request->session->application
如果想要从指定的范围中查找属性可以使用 ${ requestScope 属性名称 } 格式
调用内置对象:使用pageContext可以获得request,session,application的实例
pageContext.session 格式 通过反射机制完成
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<h3>IP地址:${pageContext.request.remoteAddr}</h3>
<h3>SESSION ID:${pageContext.session.id}</h3>
<h3>是否是新session:${pageContext.session.new}</h3>
</body>
</html>
接收请求参数${param.参数名称 } ${paramValues.参数名称 }
接收单个参数时可以直接输出,多个参数则需要指定下标
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title></title></head>
<body>
<%
request.setCharacterEncoding("GBK") ;
%>//参数名为inst
<h3>第一个参数:${paramValues.inst[0]}</h3>
<h3>第二个参数:${paramValues.inst[1]}</h3>
<h3>第三个参数:${paramValues.inst[2]}</h3>
</body>
</html>
集合操作
输出collication
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title> </title></head>
<body>
<%
List all = new ArrayList() ;
all.add("qwe") ;
all.add("www.MLDNJAVA.cn") ;
all.add("[email protected]") ;
request.setAttribute("allinfo",all) ; // 集合保存在request范围
%>
<h3>第一个元素:${allinfo[0]}</h3>
<h3>第二个元素:${allinfo[1]}</h3>
<h3>第三个元素:${allinfo[2]}</h3>
</body>
</html>
输出map 可以通过${info.mldn}或${info["mldn"]}方式
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title> </title></head>
<body>
<%
Map map = new HashMap() ;
map.put("qwe","qwe") ;
map.put("www","www.M.cn") ;
map.put("email","[email protected]") ;
request.setAttribute("info",map) ; // 集合保存在request范围
%>
<h3>KEY为qwe的内容:${info["qwe"]}</h3>
<h3>KEY为www的内容:${info.www}</h3>
<h3>KEY为email的内容:${info["email"]}</h3>
</body>
</html>
在MVC中应用表达式语言,可以通过反射直接访问保存在属性范围中的Java对象内容
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.lxh.eldemo.vo.*"%>
<html>
<head><title> </title></head>
<body>
<% // 现在假设这些代码是由Servlet完成
Dept dept = new Dept() ;
dept.setDeptno(10) ;
dept.setDname(" 教学部") ;
dept.setLoc(" 西城区") ;
request.setAttribute("deptinfo",dept) ;
%>
<h3>部门编号:${deptinfo.deptno}</h3>
<h3>部门名称:${deptinfo.dname}</h3>
<h3>部门位置:${deptinfo.loc}</h3>
</body>
</html>
通过Servlet传递request属性
import com.hsd.eldemo.vo.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class ELServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException,ServletException{
Dept dept = new Dept() ;
dept.setDeptno(10) ;
dept.setDname(" 教学部") ;
dept.setLoc(" 西城区") ;
request.setAttribute("deptinfo",dept) ;
request.getRequestDispatcher("dept_info.jsp").forward(request,response) ;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws java.io.IOException,ServletException{
this.doGet(request,response) ;
}
}
//JSP接收
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title> </title></head>
<body>
<h3>部门编号:${deptinfo.deptno}</h3>
<h3>部门名称:${deptinfo.dname}</h3>
<h3>部门位置:${deptinfo.loc}</h3>
</body>
</html>