EL表达式(十一大内置对象)

这是一个内置对象可以直接拿来使用,不需要再去声明。

1、读取页面上下文:

(1)pageContext对象

获取URL和URI:

<body>
URI:${pageContext.request.requestURI};<br>
URL:${pageContext.request.requestURL};<br>
</body>

 应用:

动态获取web应用的名称:

<body>
${pageContext.request.contextPath}
</body>

运行结果为web应用的名称。

<center>
    <h3>注册</h3>
    <form action="${pageContext.request.contextPath}/el.jsp" method="post">
        用户名:<input type="text" name="account" size="12"><br><br>
        密码:<input type="password" name="password" size="12">
        <input type="submit" value="注册">
        <input type="reset" value="取消">
    </form>
</center>

将其放入action中,即使项目名称发生改变,依旧能够正常运行。

其中pageContext.request为获得request对象。

2、四个域(不是对象):

(2)page域:相当于this关键字

  (3)  request域:对服务器请求,一次请求内有效,可以在一次请求(用户在发出请求之后没有被修改,即:地址栏发生变化)内存储数据

  (4)  session域

  (5)  application域:pageScrop、requestScrop、sessionScrop、applicationScrop

表示当前服务器运行的应用

3、读取客户端表单或查询字符串参数

(6)param:获取单一参数:

获取表单的数据:

<body bgcolor="aqua">
<center>
    <h3>注册</h3>
    <form action="${pageContext.request.contextPath}/el.jsp" method="post">
        用户名:<input type="text" name="account" size="12"><br><br>
        密码:<input type="password" name="password" size="12">
        <input type="submit" value="注册">
        <input type="reset" value="取消">
    </form>
</center>
<body>
账户名:${param.account}
密码:${param.password}
</body>

 (7)paramValues获取表单数据

4、读取request请求头

(8)header:获取单一数据:

</head>
<body bgcolor="#7fffd4">
${header["User-Agent"]}<br>
${header.Host}
</body>

 (9)headerValues:获取多个数据

5、读取(10)Cookie:

(1)先创建一个JSP创建一个Cookie:

<body>
<%
Cookie cookie=new Cookie("name","zhai");
response.addCookie(cookie);
%>
</body>

(2) 获取Cookie中的信息:

${cookie.name.value}

 6、读取初始化参数:(11)

(1)先在web.xml中配置初始化数据:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>zhai</param-name>
        <param-value>1997</param-value>
    </context-param>
</web-app>

(2)获取:

<body bgcolor="#7fffd4">
${initParam.zhai}
</body>

原文地址:https://www.cnblogs.com/zhai1997/p/11622388.html