ElServlet.javahtml
public class ElServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //從請求中獲取數據 String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); System.out.println("name="+name+",pwd="+pwd); //設置屬性的值爲字符串類型 request.setAttribute("city","beijing"); //Object類型 User user = new User(1,"lisi",new Address("hubei","wuhan","hongshan")); request.setAttribute("user",user); //List類型 ArrayList<User> list = new ArrayList<>(); list.add(user); request.setAttribute("list",list); //HashMap類型 HashMap<String, String> hm = new HashMap<>(); hm.put("china","beijing"); hm.put("hubei","wuhan"); request.setAttribute("map",hm); HashMap<String, User> hm1 = new HashMap<>(); hm1.put("user",user); request.setAttribute("suhm",hm1); //經過請求轉發方式跳到某jsp頁面 request.getRequestDispatcher("el.jsp").forward(request,response); } }
el.jspjava
<%@ page import="com.syf.entity.User" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--使用傳統方式獲取做用域中的值--%> name:<%=request.getParameter("name")%> pwd:<%=request.getParameter("pwd")%> <br> city:<%=request.getAttribute("city")%> town:<%=((User)request.getAttribute("user")).getAddress().getTown()%> <br> <%=((User)((ArrayList)request.getAttribute("list")).get(0)).getAddress().getTown()%> <br> <%=((HashMap)request.getAttribute("map")).get("china")%> <br> <%=((HashMap)request.getAttribute("suhm")).get("user")%> </body> </html>
從上面代碼能夠看到,使用傳統方式得到request對象中的屬性值有如下缺點:express
EL表達式能夠解決上述問題
一、概念cookie
二、用法
使用EL表達式進行上述輸出session
<html> <head> <title>Title</title> </head> <body> <%--使用EL表達式獲取做用域中的值--%> name:${param.name} pwd:${param.pwd}<br> city:${city}<br> town:${user.address.town}<br> ${list[0].address.town}<br> ${map.china}<br> ${suhm.user} </body> </html>
三、EL表達式做用域的順序
JSP的四大做用域,範圍由小到大爲:pageContext-->request-->session-->application
若是四個做用域中都有相同的key,獲取值的順序是怎樣的?app
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <!--在四大做用域中設置屬性值--> <% pageContext.setAttribute("key","this is pageContext"); request.setAttribute("key","this is request"); session.setAttribute("key","this is session"); application.setAttribute("key","this is application"); %> <%--獲取做用域中的值--%> key:${key} </body> </html>
上述代碼獲取到的是pageContext,把pageContext這行註釋掉,再執行程序,獲取到的是request,同理往下依次是session,application。
這說明不一樣做用域中有相同key,獲取key的值的順序是由小做用域到大做用域。
如何獲取指定的key的值?jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <!--在四大做用域中設置屬性值--> <% pageContext.setAttribute("key","this is pageContext"); request.setAttribute("key","this is request"); session.setAttribute("key","this is session"); application.setAttribute("key","this is application"); %> <%--獲取指定做用域中key的值--%> pageContext:${pageScope.key}<br> request:${requestScope.key}<br> session:${sessionScope.key}<br> application:${applicationScope.key} </body> </html>
四、EL表達式進行運算this
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${1+1}<br><%--2--%> ${2-1}<br><%--1--%> ${2*3}<br><%--6--%> ${6/2}<br><%--3.0--%> ${7%3}<br><%--1--%> ${1>2?"好":"很差"}<br><%--很差--%> ${1+"2"}<br><%--3--%> <%--${1+"abc"}<br> 報錯--%> <%--${"abc"+"cdb"} 報錯--%> <%--邏輯運算--%> ${true&&false}<br><%--false--%> ${true||false}<br><%--true--%> </body> </html>
EL表達式能夠進行算術運算與關係運算,關係運算返回true或false。須要注意的是,EL表達式中的+,表示加法,不是字符串鏈接符。
五、EL表達式其餘用法spa
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--獲取請求頭數據--%> ${header}<br> ${header["host"]}<br> ${headerValues["accept-language"][0]}<br> <hr> <%--獲取cookie數據--%> ${cookie}<br> ${cookie.JSESSIONID}<br> ${cookie.JSESSIONID.name}<br> ${cookie.JSESSIONID.value}<br> </body> </html>