标签:el
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方法,用标识符为关键字,分别从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 { } }
<%@ 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>
<%@ 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>
<?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>
http://localhost:8080/Test/jsp1/MyJsp.jsp
此时显示的结果如图所示:
当访问的路径是:http://localhost:8080/Test/el,此时的运行结果如下所示:
由此我们可以看出,无论是否访问对应页面中的session,此时的session仍然可以直接被其它页面所使用,除非关闭浏览器或者关闭对应的session。
标签:el
原文地址:http://blog.csdn.net/zzy1078689276/article/details/46412825