文章

EL表达式的使用

什么是EL表达式? EL表达式能干什么? EL表达式如何使用? EL拥有大多编程语言的运算符,但是EL也有自己比较独特的运算符: EL使用说明: 比如: 存取数据: EL表达式的保留字: EL表达式的便捷: EL隐含的对象: 比如: OK,接下来做一个小例子: 首先是通过一个form表单提交用户名和密码:<%@ page langu_form 表达式 el提交

EL表达式的使用

文章信息

  • 原文链接:https://jiayq.blog.csdn.net/article/details/77657879
  • 发布时间:2017-08-28 21:32:27
  • 阅读量:694
  • 分类:java专栏收录该内容, 订阅专栏

摘要

文章浏览阅读694次。什么是EL表达式? EL表达式能干什么? EL表达式如何使用? EL拥有大多编程语言的运算符,但是EL也有自己比较独特的运算符: EL使用说明: 比如: 存取数据: EL表达式的保留字: EL表达式的便捷: EL隐含的对象: 比如: OK,接下来做一个小例子: 首先是通过一个form表单提交用户名和密码:<%@ page langu_form 表达式 el提交</div>


什么是EL表达式?
EL表达式介绍
EL表达式能干什么?
这里写图片描述
EL表达式如何使用?
这里写图片描述
EL拥有大多编程语言的运算符,但是EL也有自己比较独特的运算符:
这里写图片描述
EL使用说明:
这里写图片描述
比如:
这里写图片描述
存取数据:
这里写图片描述
EL表达式的保留字:
这里写图片描述
EL表达式的便捷:
这里写图片描述
EL隐含的对象:
这里写图片描述
比如:
这里写图片描述
OK,接下来做一个小例子:
首先是通过一个form表单提交用户名和密码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ 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 'index.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">
    -->
    <script type="text/javascript" src="jquery/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("form:first>input:last").click(function() {
                alert("在servlet中会把这些数据存入不同的域中");
            });
        });
    </script>
  </head>

  <body>
    测试EL表达式<br>
    首先通过form表单提交一部分数据:<br>
    <form action="TestServlet" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>
        <input type="submit">
    </form>
    <form action="param.jsp" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>
        <input type="submit" value="点击测试param">
    </form>
  </body>
</html>

这里写图片描述
接下来测试表单提交的数据通过EL表达式获取,就是使用第二个表单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ 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 'param.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>
    username:${param.username }<br>
    password:${param.password }<br>
    <a href="index.jsp">返回</a>
  </body>
</html>

结果如下:
这里写图片描述
接下来在servlet中在不同的域对象中存入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HttpSession session = request.getSession();
        ServletContext application = getServletContext();
        String username = null;
        String password = null;
        if(request.getParameter("username") != null){
            username = request.getParameter("username");
        }
        if(request.getParameter("password") != null){
            password = request.getParameter("password");
        }
        System.out.println(username);
        System.out.println(password);
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        request.setAttribute("username", username);
        request.setAttribute("password", password);
        application.setAttribute("username", username);
        application.setAttribute("password", password);
        request.getRequestDispatcher("test.jsp").forward(request, response);

输入如下:
这里写图片描述
结果如下:
这里写图片描述
在控制台输出的结果如下:
这里写图片描述
跳转后的页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<%@ 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 'test.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">
    -->
<style type="text/css">
a{
    text-decoration: none;
}
</style>
  </head>

  <body>
    \${requestScope.username }:${requestScope.username }<br>
    \${requsetScope.password }:${requestScope.password }<br>
    \${sessionScope.username }:${sessionScope.username}<br>
    \${sessionScope.password }:${sessionScope.password}<br>
    \${applicationScope.username }:${applicationScope.username }<br>
    \${applicationScope.password }:${applicationScope.password }<br>
    \${pageContext.request.queryString}:
        ${pageContext.request.queryString}</br>
   \${pageContext.request.requestURL}:
        ${pageContext.request.requestURL}</br>
   \${pageContext.request.contextPath}:
        ${pageContext.request.contextPath}</br>
   \${pageContext.request.method}:
      ${pageContext.request.method}</br>
   \${pageContext.request.protocol}:
      ${pageContext.request.protocol}</br>
   \${pageContext.request.remoteUser}:
      ${pageContext.request.remoteUser}</br>
   \${pageContext.request.remoteAddr }:
      ${pageContext.request.remoteAddr}</br>
   \${pageContext.session.id}:
      ${pageContext.session.id}</br>
      这是测试空的EL表达式,返回应该是空字符串:<br>
      \${request.asdasd }:${request.asdasd }<br>
    <a href="index.jsp">返回</a>
  </body>
</html>

显示如下:
这里写图片描述

本文由作者按照 CC BY 4.0 进行授权