<%--
Created by IntelliJ IDEA.
User: Vae
Date: 2019/1/2
Time: 12:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>许嵩许嵩</p> <%
//JSP的四大作用域
pageContext.setAttribute("msg","pageContext");
request.setAttribute("msg","request");
session.setAttribute("msg","session");
application.setAttribute("msg","application");
%> <h3>获取每一个作用域中的值</h3>
pageContext:<%=pageContext.getAttribute("msg")%><br>
request:<%=request.getAttribute("msg")%><br>
session:<%=session.getAttribute("msg")%><br>
application:<%=application.getAttribute("msg")%><br> <hr>
<h3>pageContext的findAttribute方法(只有pageContext才有)</h3>
<%=pageContext.findAttribute("msg")%><br>
<%=pageContext.findAttribute("msg")==null?"":pageContext.findAttribute("msg")%><br>
${msg}<br> </body>
</html>
EL ${msg} 是从作用域从小往大找的,也可以设置为只从某个作用域查询
<!--EL的四大作用域对象-->
${pageScope.msg}<br>
${requestScope.msg}<br>
${sessionScope.msg}<br>
${applicationScope.msg}<br>
EL获取Java Bean数据
新建一个包,新建一个Servlet类和Person类
package main.com.vae.EL; import lombok.Getter;
import lombok.Setter; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Setter@Getter
public class Person {
private String name="许嵩";
private Integer age=32;
private String[] hobbys={"音乐","遛狗","D"};
private List<String> list=Arrays.asList("list1","list2");
private Map<String,Object> map=new HashMap<String,Object>(){
{
this.put("6","青年晚报");
this.put("7","寻宝游戏");
this.put("www.vae.com","Vae+");
}
}; }
package main.com.vae.EL; 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 java.io.IOException;
@WebServlet("/el")
public class ELServlet extends HttpServlet { @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Person person=new Person();
req.setAttribute("person",person);
req.getRequestDispatcher("/jspDemo/EL/el.jsp").forward(req,resp);
}
}
再建一个jsp叫el
<%--
Created by IntelliJ IDEA.
User: Vae
Date: 2019/1/9
Time: 13:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>Person对象的信息</h3><br>
name:${person.name}-->${person.getName()}-->${person["name"]}<br>
age:${person.age}<br>
hobbys:${person.hobbys[0]}-->${person.hobbys[1]}<br>
list:${person.list[0]}-->${person.list[1]}<br>
map:${person.map["6"]}-->${person.map["www.vae.com"]}<br> </body>
</html>
结果:
像map里面的 www.vae.com 这个显然就不能使用 person.map.www.vae.com来调用了,不认识的,只能使用[""]来调用了。