jsp EL表达式详解

一、EL表达式介绍

Expression Language表达式语言

是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据)

在JSP2.0开始引入概念

语法格式

在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值

二、EL获取数据(从四大域中获取属性)
EL只能从四大域中获取属性
1、如果没有使用EL的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值

二、EL获取数据(从四大域中获取属性)

EL只能从四大域中获取属性

1、如果没有使用EL的内置对象,则查找数据顺序是依次按照由小到大范围从四大域中查找指定名称的属性值

- pageContext<request<session<application <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("name", "linjie"); request.setAttribute("name", "lucy"); session.setAttribute("name", "king"); application.setAttribute("name", "bilibili"); %> name=${name } </body> </html>

这里写图片描述

可以看出没有使用EL内置对象时查找顺序是由小到大,所以最先显示的是pageContext域中的

2、使用EL内置对象,从指定域中获取数据,提高了查找效率

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("name", "linjie"); request.setAttribute("name", "lucy"); session.setAttribute("name", "king"); application.setAttribute("name", "bilibili"); %> name=${applicationScope.name } </body> </html>

这里写图片描述

可以看出,使用applicationScope即可指定application域中的name输出,当然其他域也是类似,下文会说这四大域属性相关的内置对象

三、EL中的内置对象

EL有11个内置对象,这里主要讲域属性相关的4个和其他4个
EL的11个内置对象,除了pageContext以外,其他10个内置对象的类型都是java.util.Map类型

1、域属性相关(4个)

pageScope:从page范围域属性空间中查找指定的key

requestScope:从request范围域属性空间中查找指定的key

sessionScope:从session范围域属性空间中查找指定的key

applicationScope:从application范围域属性空间中查找指定的key

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("name", "linjie"); request.setAttribute("name", "lucy"); session.setAttribute("name", "king"); application.setAttribute("name", "bilibili"); %> name=${applicationScope.name }<br> name=${pageScope.name }<br> name=${sessionScope.name }<br> name=${requestScope.name }<br> </body> </html>

这里写图片描述

2、其他重要内置对象(4个) 1、pageContext

该pageContext与JSP内置对象pageContext是同一个对象。通过该对象,可以获取到request、response、session、servletContext、servletConfig等对象注意:这些对象在EL里不是内置对象,这些对象只能通过pageContext获取

在EL中直接${pageContext.request}即可获取request对象,其底层调用的是pageContext.getRequest()方法。同理,也可以通过类似方法获取其他对象

重点:其中最常用的:${pageContext.request.contextPath },代表web应用下的根,可以看出下面action中的路径可读性更强了

Regster.java

package linjie.com; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Regster extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

index.jsp

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsgyyp.html