EL表达式。全名为Expression Language。它原本是JSTL 1.0为方便存取数据所自己定义的语言。
当时EL仅仅能在JSTL 标签中使用。JSP2.0后,EL成为JSP规范的一部分,并添加了新的特性。
一、可得到PageContext属性值
二、可直接訪问JSP的内置对象,如page,request, session,application等
三、运算符丰富。有关系运算符、逻辑运算符、算术运算符等扩展函数
四、可与JAVA类的静态方法相应
EL使用[]和.来訪问数据,当中${exprA.identifier}等价于${exprA[“identifier”]} 当要存取的属性名称中包括一些特殊字符,如 . 或 – 等并不是字母或数字的符号,就一定要使用 [
],EL表达式能够直接在JSP页面中使用,也能够作为元素属性的值,还能够在自己定义。但不能在脚本元素中使用。
EL表达式语句在运行时,会调用pageContext.findAttribute方法,用标识符为keyword,分别从page、request、session、application四个域中查找对应的对象。找到则返
回对应的对象,找不到则返回”” (注意,不是null,而是空字符串)。
EL 有 11 个保留标识符,相应于 11 个 EL 隐式对象。所谓保留标识符的意思是指变量在命名时,应该避开上述的名字,以免程序编译时错误发生。
详细的保留标识符例如以下
表所看到的:
以下是其源码:
el.java源码:
package com.el;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class el extends HttpServlet {
public el() {
super();
}
public void destroy() {
super.destroy();
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置页面编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
request.setAttribute("req1","这里是Servlet中的request容器");
request.getSession().setAttribute("sess1","这里是servlet中的session容器");
this.getServletContext().setAttribute("app1","这里是servlet中的application容器");
request.getRequestDispatcher("jsp1/ElJsp.jsp?name1=这里是參数").forward(request, response);
}
public void init() throws ServletException {
}
}
MyJsp.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
在容器中放入參数<br>
<%request.setCharacterEncoding("UTF-8"); %>
<%request.setAttribute("req","这里是request容器"); %>
<%session.setAttribute("sess","这里是session容器"); %>
<%application.setAttribute("app","这里是application容器"); %>
<jsp:forward page="ElJsp.jsp?name=这里是參数"></jsp:forward>
</body>
</html>
ElJsp.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ElJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<hr>
<h2>取出MyJsp.jsp页面中的数据</h2>
request:${requestScope.req }<br>
session:${sessionScope.sess }<br>
application:${applicationScope.app }<br>
param:${param.name }<br/>
<hr/>
<h2>取出servlet页面中的数据</h2>
request:${requestScope.req1 }<br>
session:${sessionScope.sess1 }<br>
application:${applicationScope.app1 }<br>
param:${param.name1 }<br/>
<hr>
<h2>算术运算</h2>
加法:${2+2 }<br/>
减法:${2-2 }<br/>
乘法:${2*2 }<br/>
除法:${2/2 }<br/>
<hr>
<h2>关系运算</h2>
大于:${2>3 }<br/>
小于:${2<3 }<br/>
等于:${2==3 }<br/>
不等于:${2!=3 }<br/>
<hr>
<h2>逻辑运算</h2>
或:${2>3||2<3 }<br/>
且:${2>3&&2<3 }<br/>
非:${!(2==3) }<br/>
<hr>
<h2>三目运算</h2>
2>3:${2>3?"回答正确!
":"回答错误。" }<br> <hr> </body> </html>
web.xml源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>jsp1/MyJsp.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>el</servlet-name>
<servlet-class>com.el.el</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>el</servlet-name>
<url-pattern>/el</url-pattern>
</servlet-mapping>
</web-app>
本人的项目名称是Test,所以直接訪问的路径例如以下所看到的:
http://localhost:8080/Test/jsp1/MyJsp.jsp
此时显示的结果如图所看到的:
当訪问的路径是:http://localhost:8080/Test/el,此时的执行结果例如以下所看到的:
由此我们能够看出。不管是否訪问相应页面中的session,此时的session仍然能够直接被其他页面所使用,除非关闭浏览器或者关闭相应的session。