EL表达式基础

<%@ page language="java" pageEncoding="utf-8" import="com.study.vo.*,java.util.*" %>
<html>
    <head>
    
    </head>
    <body>
        <h2>使用普通方法获得作用域对象</h2>
        <b><%=request.getParameter("name")%></b><br/>
        <b><%=request.getAttribute("str")%></b><br/>
        <b><%=((User)request.getAttribute("user")).getAddress().getAddr()%></b><br/>
        <b><%=((ArrayList<User>)request.getAttribute("list")).get(0).getAddress().getAddr() %></b><br/>
        <b><%=((HashMap<String,User>)request.getAttribute("map")).get("user").getAddress().getAddr()%></b>
        <hr/>
        <h2>使用EL表达示获得作用域对象(优点:不用导包,阅读方法,代码更少)</h2>
        <b>${param.name}</b><br/>
        <b>${str}</b><br/>
        <b>${user.address.addr}</b><br/>
        <b>${list[0].address.addr}</b><br/>
        <b>${map.user.address.addr}</b><br/>
        
        <h2>EL表达式获取对象默认顺序</h2>
        <%
            pageContext.setAttribute("hello","pagecontext");
            request.setAttribute("hello","request");
            session.setAttribute("hello","session");
            application.setAttribute("hello","application");
        %>
        <b>pageContext-->request-->session-->application</b><br>
        <b>${pageScope.hello}</b><br>
        <b>${requestScope.hello}</b><br>
        <b>${sessionScope.hello}</b><br>
        <b>${applicationScope.hello}</b><br>
        <h2>EL表达式的算术运算</h2>
        <strong>不支持字符串连接</strong><br/>
         ${1+2}--${1-2}--${1>2}--${1==2?0:0}--${1+"2"}
         <h2>判断是否为空</h2>
         <b>${empty str}</b>
         <b>${empty test}</b>
         <h2>获得请求头数据 heard(了解)</h2>
         <b>${header }</b><br>
         <b>${header["cookie"] }</b><br>
         <b>${headerValues["cookie"]}</b>
         <h2>获得cookie数据</h2>
         <b>${cookie }</b><br>
         <b>${cookie.JSESSIONID }</b><br>
         <b>${cookie.JSESSIONID.name }</b><br>
         <b>${cookie.JSESSIONID.value }</b><br>
    </body>
</html>
    @Override
    public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        request.setAttribute("str","javase");
        User u=new User(1, "javase", "javaee", "javame", new Address("china"));
        request.setAttribute("user",u);
        List<User> list=new ArrayList<User>();
        list.add(u);
        request.setAttribute("list", list);
        Map<String,User> map=new HashMap<String,User>();
        map.put("user",u);
        request.setAttribute("map",map);
        request.getRequestDispatcher("/ELStudy.jsp").forward(request, response);
    }
原文地址:https://www.cnblogs.com/lastingjava/p/9899032.html