JavaWeb简单案例——EL(表达式语言)

EL:Expression Languang,表达式语言。EL的语法非常简单,${表达式},就可以获取到表达式中的内容。在实际的使用中,EL语法可以帮助我们简化JSP页面中的获取域中数据的Java代码部分,帮助我们提高代码的开发效率,下面我们用两个示例来更好的理解:

HTML页面获取数据:

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2022/10/24
  Time: 11:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>提供数据</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/ServletEL">
  <table>
    <tr> <td>用户名:</td>  <td><input type="text" name="username"></td></tr>
    <tr> <td>密码:</td>  <td><input type="password" name="password"></td></tr>
    <tr> <td><button type="submit">提交</button></td></tr>
  </table>
</form>
</body>
</html>

Java代码获取数据:这个Servlet不仅要接收数据,还要将数据使用重定向的方式转发到展示数据的JSP页面

package work;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebFault;
import javax.xml.ws.WebServiceClient;
import java.io.IOException;
@WebServlet(name = "ServletEL" , value = "/ServletEL")
public class ServletEL extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        req.setAttribute("username",username);
        req.setAttribute("password",password);
        req.getRequestDispatcher("/show.jsp").forward(req,resp);
        resp.sendRedirect("/show.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

Java代码展示数据:

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2022/10/24
  Time: 11:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>展示数据</title>
</head>
<body>
<%
  String username = (String)request.getAttribute("username");
  String password = (String)request.getAttribute("password");
  response.getWriter().write("用户名为:"+username+"<br>");
  response.getWriter().write("密码为:"+password);
%>
</body>
</html>

 可以看出,可以达到效果,但是获取数据的方式有些繁琐,使用EL表达式可以将代码简化一些:

使用EL表达式简化之后的JSP文件:

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2022/10/24
  Time: 11:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>展示数据</title>
</head>
<body>
<p>
    用户名为:${username}<br>
    密码为:${password}
</p>
</body>
</html>

展示效果:

可以看到,仅仅使用两个单词就把数据获取并展示出来了,大大的简化了我们获取数据的流程,所以,在实际开发中建议使用EL表达式的方式获取数据

除此之外,EL表达式也支持基本的运算符,获取对象数据,数组数据等获取数据的方式

在上面的代码示例中,需要将第一个界面中获取用户输入的数据再存储进域中,也是非常的麻烦,所以EL就提供了两个隐式对象来获取访问JSP页面时携带的参数:

param:获取用户请求参数的某一个值,如果没有值则返回一个空字符串而不是null

paramValues:如果用户的请求参数有多个值,可以通过paramValues.参数名[参数下标]的方式获取请求参数的所有值

JSP页面输入数据:

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2022/10/24
  Time: 11:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>提供数据</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/show.jsp">
  <table>
    <tr> <td>用户名:</td>  <td><input type="text" name="username"></td></tr>
    <tr> <td>密码:</td>  <td><input type="password" name="password"></td></tr>
    <tr> <td><button type="submit">提交</button></td></tr>
  </table>
</form>
</body>
</html>

JSP页面获取数据:

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2022/10/24
  Time: 11:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>展示数据</title>
</head>
<body>
<p>
    用户名为:${param.username}<br>
    密码为:${param.password}
</p>
</body>
</html>

注意点:

在输入数据的时候,form标签的action属性指向的就不再是一个Servlet类了,而是指向用来展示数据的JSP页面。在展示数据的页面,使用param内置对象.属性名的方式就可以获取到用户访问时协传递的参数信息了

EL除了有获取用户访问时参数的内置对象,还有获取Cookie的内置对象和获取Web应用初始化数据的内置对象等,可以查阅官网进行深(♂)入(♂)了(♂)解(♂)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值