本節內容:javascript
動態頁面技術:就是在html中嵌入java代碼css
很早以前程序員在開發動態網頁時,是經過以下方法寫的:html
public class HtmlServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); //動態的響應html頁面 writer.write("<!DOCTYPE html>"); writer.write("<html>"); writer.write("<head>"); writer.write("<meta charset='UTF-8'>"); writer.write("<title>Insert title here</title>"); writer.write("</head>"); writer.write("<body>"); writer.write("<h1>這個頁面很繁瑣</h1>"); writer.write("</body>"); writer.write("</html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
這樣寫太麻煩了,若是像京東這種大網站。若是像在servlet中這樣一行一行寫,不使用jsp技術,那就完蛋了。前端
原本大多使用jsp開發了,可是隨着業務的複雜性,jsp很難解決。由於jsp就是個頁面,在頁面中嵌入n多java代碼,還要嵌入n多css,n多js,很麻煩。
之後開發java web項目,會結合servlet技術和jsp技術。java
1. jsp腳本程序員
【注意】:jsp都是新建在WebContent下的,和html文件在同一級別,不是新建在src下。 web
【示例】:新建個java web項目,而後建立index.jsp面試
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 第一種形式 --> <% int i=0; System.out.print(i); //這是在控制檯打印 %> <!-- 第二種形式 --> <%=i %> <!-- 這是在頁面輸出--> <%=1+1 %> <!-- 第3種形式 --> <%! String str = "nihao china!"; %> <%=str %> <!-- 第三種也是在頁面輸出 --> <h1>akdhakh</h1> </body> </html>
第二種和第三種都是在頁面輸出,有什麼區別?見下面jsp運行原理。sql
2. jsp註釋: 不一樣的註釋可見範圍是不一樣數據庫
<!-- html的註釋 --> <% //exception.getMessage() request.setAttribute("age", 30); session.setAttribute("name", "zhangsan"); //這是單行註釋 //int i=0; /* 多行註釋 */ /*System.out.print(i); System.out.print(j); */ %> <%-- jsp註釋 --%>
jsp本質就是servlet(面試)
jsp在第一次被訪問時會被Web容器翻譯成servlet,執行過程:
第一次訪問 --> helloServlet.jsp --> helloServlet_jsp.java --> 編譯運行。
【注意】:被翻譯後的servlet在Tomcat的work目錄中能夠找到。
好比上面那個示例,將項目運行起來,用戶訪問index.jsp時,會去work目錄找有沒有index_jsp.java文件,有的話用這個servlet響應;沒有的話會將index.jsp翻譯成index_jsp.java。
當你更新index.jsp時,再次訪問該jsp時,會從新翻譯成index_jsp.java。這部分翻譯時tomcat容器幫咱們作的。這個是tomcat裏面的全局web.xml配置好的:
如今講講上面jsp腳本3種格式的區別:
只要在body中寫的內容,直接的xxxx,好比:<h1>akdhakh</h1>,直接翻譯成out.write("xxxxx");
當遇到第一種<%java代碼%>,翻譯時也是什麼都沒幹,直接扔到service方法內部。
當遇到第二種形式,會翻譯成:out.print(i)
當遇到第三種形式,在service方法內部看不到定義的String str=「」; 這種形式的代碼會被翻譯到成員的位置。
因此在第三種方式下,咱們能夠在這種形式裏定義方法,由於它會被翻譯到成員的位置。好比:
<!-- 第3種形式 --> <%! String str = "nihao china!"; public void fn(){ } %>
jsp的指令是指導jsp翻譯和運行的命令,jsp包括三大指令:
1. page指令
屬性最多的一個指令,根據不一樣的屬性,指導整個頁面特性(實際開發中page指令默認)
格式:<%@ page 屬性名1= "屬性值1" 屬性名2= "屬性值2" ...%>
經常使用屬性以下:
2. include指令
頁面包含(靜態包含)指令,能夠將一個jsp頁面包含到另外一個jsp頁面中
格式:<%@ include file="被包含的文件地址"%>
前臺和美工幫咱們寫好頁面,作前端的不懂什麼是jsp,交給咱們的頁面都是html頁面,並且各個html裏面也沒有引用頭和尾部,每一個頁面頭部和尾部都是同樣的。咱們後端人員拿到html,首先要把html改爲jsp,怎麼改?
新建一個新的jsp,把整個html代碼copy過來,往裏面一粘貼就是jsp。注意把html頁面的頭和尾取出來單獨放到一個jsp中。
3. taglib指令
在jsp頁面中引入標籤庫(jstl標籤庫、struts2標籤庫)
格式:<%@ taglib uri="標籤庫地址" prefix="前綴"%>
筆試。
jsp被翻譯成servlet以後,service方法中有9個對象定義並初始化完畢,咱們在 jsp 腳本中能夠直接使用這9個對象。(由於直接能夠用了,全部稱爲隱式)
名稱 | 類型 | 描述 |
out | javax.servlet.jsp.JspWriter | 用於頁面輸出 |
request | javax.servlet.http.HttpServletRequest | 獲得用戶請求信息 |
response | javax.servlet.http.HttpServletResponse | 服務器向客戶端的迴應信息 |
config | javax.servlet.ServletConfig | 服務器配置,能夠取得初始化參數 |
session | javax.servlet.http.HttpSession | 用來保存用戶的信息 |
application | javax.servlet.ServletContext | 全部用戶的共享信息 |
page | java.lang.Object | 指當前頁面轉換後的Servlet類的實例 |
pageContext | javax.servlet.jsp.PageContext | JSP的頁面容器 |
exception | java.lang.Throwable | 表示JSP頁面所發生的異常,在錯誤頁中才起做用 |
【示例】:在jsp腳本中直接使用九大隱式對象。
<% //九大隱式對象,不用new,直接拿來用 //exception.getMessage() request.setAttribute("age", 30); session.setAttribute("name", "zhangsan"); %>
爲何在jsp腳本里直接能用呢? 由於jsp裏調用這九大隱式對象的代碼會被複制到servlet內部的service方法內執行。
request和response在service方法的參數裏。
1. out對象
out的類型:JspWriter
out做用就是向客戶端輸出內容 --out.write()
out緩衝區默認8kb。能夠設置成0,表明關閉out緩衝區,內容直接寫到respons緩衝器。
【示例】:向頁面輸出內容out.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> aaaaaaaaaaaaaaaaaaaaaaaaaaaaa <!--向頁面輸出內容方式一--> <% int i=10; out.write("bbbbbbbbbbbbbbb");//向頁面輸出內容方式二 response.getWriter().write("cccccccccccccc"); //向頁面輸出內容方式三 %> <%="dddddddddddddddddd" %> <!--向頁面輸出內容方式四--> <% System.out.print(i); %> </body> </html>
訪問這個頁面,獲得的順序是c a b d。由於輸出a、b、d的方式最終在翻譯時都會被轉換成out.write()。
如今調整下:
訪問下,獲得的結果是 a b c d。
2. pageContext對象
page對象與pageContext對象不是一回事。
jsp頁面的上下文對象,做用以下:
(1)pageContext是一個域對象
pageContext還能夠向指定的其餘域中存取數據:
四大做用域的總結:
(2)能夠得到其餘8大隱式對象
pageContext對象內部維護着其餘8大隱式對象的索引。
例如:
pageContext.getRequest() pageContext.getSession()
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //使用pageContext向request域存數據 //request.setAttribute("name", "zhangsan"); //pageContext.setAttribute("name", "sunba"); //pageContext.setAttribute("name", "lisi", PageContext.REQUEST_SCOPE); //pageContext.setAttribute("name", "wangwu", PageContext.SESSION_SCOPE); //pageContext.setAttribute("name", "tianqi", PageContext.APPLICATION_SCOPE); %> <%=request.getAttribute("name") %> <%=pageContext.getAttribute("name", PageContext.REQUEST_SCOPE)%> <!-- findAttribute會從小到大搜索域的範圍中的name --> <!-- page域<request域<session域<application域 --> <%=pageContext.findAttribute("name") %> <% pageContext.getRequest(); pageContext.getOut(); //method(pageContext); %> </body> </html>
1. 頁面包含(動態包含):<jsp:include page="被包含的頁面"/>
【示例】:靜態包含和動態包含。
靜態包含代碼:
include_1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>this is include1 page</h1> <%@ include file="/include_2.jsp" %> </body> </html>
include_2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>this is include2 page</h1> </body> </html>
動態包含代碼:
include1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>this is include1 page</h1> <!-- 包含include2 --> <jsp:include page="/include2.jsp"></jsp:include> </body> </html>
include2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>this is include2 page</h1> </body> </html>
運行程序,訪問的效果是如出一轍的。
靜態包含與動態包含的區別?
2. 請求轉發:<jsp:forward page="要轉發的資源" />
【示例】:
forward1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <jsp:forward page="/forward2.jsp"></jsp:forward> </body> </html>
forward2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>xxxxxxxxxxxxxxxxxxxxxxxxxxx</h1> </body> </html>
1. EL表達式概述
EL(Express Lanuage)表達式能夠嵌入在jsp頁面內部,減小jsp腳本的編寫,EL 出現的目的是要替代jsp頁面中腳本的編寫。
2. EL從域中取出數據
好比:
EL最主要的做用是得到四大域中的數據,格式:
${EL表達式}
【示例】:
EL得到普通字符串
EL得到User對象的值
EL得到List<String>的值
EL得到List<User>的值
EL得到Map<String,String>的值
EL得到Map<String,User>的值
<%@ page import="com.itheima.domain.User" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %><%-- Created by IntelliJ IDEA. User: jkzhao Date: 10/31/17 Time: 8:08 PM 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> <!-- 模擬域中的數據 --> <% //存儲字符串 request.setAttribute("company", "黑馬程序員"); //存儲一個對象 User user = new User(); user.setId(1); user.setName("zhangsan"); user.setPassword("123"); session.setAttribute("user", user); //存儲一個集合 List<User> list = new ArrayList<User>(); User user1 = new User(); user1.setId(2); user1.setName("lisi"); user1.setPassword("123"); list.add(user1); User user2 = new User(); user2.setId(3); user2.setName("wangwu"); user2.setPassword("123"); list.add(user1); application.setAttribute("list", list); %> <!-- jsp腳本方式取出域中的值 --> <%=request.getAttribute("company")%> <% User sessionUser = (User)session.getAttribute("user"); out.write(sessionUser.getName()); %> <!-- 使用EL表達式獲取域中的值 --> ${requestScope.company} ${sessionScope.user.name} <!-- .user實際上就是getUser(),EL簡化了表達 --> ${applicationScope.list[1].name} <!-- 取第2個元素 --> <!-- 使用EL表達式 全域查找,域有順序。這是之後使用最多的 --> ${company} ${user.name} ${list[1].name} </body> </html>
3. EL的內置對象(11個)
【示例】:
form.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/WEB18/el/form2.jsp" method="post"> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="checkbox" name="hobby" value="zq">足球 <input type="checkbox" name="hobby" value="pq">排球 <input type="checkbox" name="hobby" value="ppq">乒乓球<br> <input type="submit" value="提交"><br> </form> </body> </html>
form2.jsp
<%-- Created by IntelliJ IDEA. User: jkzhao Date: 10/31/17 Time: 10:09 PM 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> <!-- 得到表單的參數 --> <% String name = request.getParameter("username"); //... out.write(name); %> <!-- 使用el得到參數 --> ${param.username} ${header.User-Agent} ${header["User-Agent"]} <!-- 由於User-Agent中間有個-,因此必須加[],前面的.也就不須要了,不少時候.和[]是一樣的效果 通常用.的都能用[],只不過.很方便。可是某些包括特殊字符的必須用[] --> ${initParam.aaa} <!-- 獲取web.xml中某些值 --> ${cookie.name.value} <!-- 先運行cookie.jsp把cookie設置到瀏覽器 --> <!-- 經過el表達式獲取request對象 --> <%--${requestScope}--%> <!-- requestScope和request不是一回事,requestScope是域 --> ${pageContext.request} </body> </html>
cookie.jsp
<%-- Created by IntelliJ IDEA. User: jkzhao Date: 10/31/17 Time: 10:33 PM 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> <% Cookie cookie = new Cookie("name","rose"); response.addCookie(cookie); %> </body> </html>
EL內置對象由11個,之後工做中也不多用到,偶爾用到一個半個,沒人用爲何會出現呢?
這是基於jsp的開發過程。很早以前,先是servlet,而後發現它輸出頁面很麻煩,而後就發明了jsp。jsp出現之後,好多人都以爲這個jsp太好用了,人們就光用jsp了,就不用servlet了。因此人們都用jsp處理全部的事情。你把表單提交到服務端,服務端得有辦法獲取表單的數據,進行對象的封裝、傳遞等,由於那個年代人們高度使用jsp,當時jsp必須接受表單並進行處理。jsp中有9個隱式對象,request、response等。可是用這些處理也很麻煩,建議jsp中少嵌入java代碼,因此EL就出現了。這個11內置對象出現的目的就是爲解決jsp接收客戶端的某些數據問題。
如今不是所有jsp來處理了,現代開發中jsp只用來顯示數據。
得到WEB應用的名稱
$(pageContext.request.contextPath) 至關於 <%=pageContext.getRequest().getContextPath%>
可是<%=pageContext.getRequest().getContextPath%>這樣寫是有問題的,不成功的。request有兩種,一種是servletRequest,一種是httpServletRequest,pageContext.getRequest()得到是servletRequest,它不帶contentPath,這只是意思意思。
【示例】:
<%-- Created by IntelliJ IDEA. User: jkzhao Date: 10/31/17 Time: 10:58 PM To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Title</title> <link href="${pageContext.request.contextPath}/xxx.css"> <!--這些都是客戶端地址,它去服務器端獲取該資源 客戶端地址建議都把web應用的名稱寫上--> <script type="application/javascript" src="${pageContext.request.contextPath}/yyy.js"></script> </head> <body> <form action="${pageContext.request.contextPath}/el/form2.jsp" method="post"><!-- pageContext.request.contextPath 表明web應用的名稱,全部的客戶端地址都得寫它。--> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="checkbox" name="hobby" value="zq">足球 <input type="checkbox" name="hobby" value="pq">排球 <input type="checkbox" name="hobby" value="ppq">乒乓球<br> <input type="submit" value="提交"><br> </form> <img alt="" src="${pageContext.request.contextPath}/1.jpg"> <img alt="" src="${pageContext.request.contextPath}/2.jpg"> <img alt="" src="${pageContext.request.contextPath}/1.jpg"> <!-- 不寫pageContext.request.contextPath, 就是相對地址,有時候會有問題--> </body> </html>
4. EL執行表達式
例如:
<!-- el能夠執行表達式運算 --> ${1+1} ${1==1?true:false} ${empty user} <!--empty斷定某個對象是否爲null,是null爲true。先從域中取出user,再判斷user是否爲null-->
EL表達式語言不能進行if else等邏輯判斷,經過jstl去搞。
el+jstl簡化jsp中嵌入的java代碼。el只是負責取數據,jstl用if else、for等。
1. JSTL概述
JSTL(JSP Standard Tag Library),JSP標準標籤庫,能夠嵌入在jsp頁面中使用標籤的形式完成業務邏輯等功能。jstl出現的目的同el同樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標籤庫有5個子庫,但隨着發展,目前常使用的是他的核心庫
標籤庫 | 標籤庫的URI | 前綴 |
Core | http://java.sun.com/jsp/jstl/core | c |
I18N | http://java.sun.com/jsp/jstl/fmt | fmt |
SQL | http://java.sun.com/jsp/jstl/sql | sql |
XML | http://java.sun.com/jsp/jstl/xml | x |
Functions | http://java.sun.com/jsp/jstl/functions | fm |
這其中的某些子庫徹底被廢棄。最多的就是Core庫。前綴建議使用c,約定俗成。
2. JSTL下載與導入
從Apache的網站下載JSTL的JAR包。進入 「http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/」網址下載 JSTL的安裝包。jakarta-taglibs-standard-1.1.2.zip,而後將下載好的JSTL安裝包 進行解壓,此時,在lib目錄下能夠看到兩個JAR文件,分別爲jstl.jar和standard.jar(高版本只須要導入一個jstl.jar包)。 其中,jstl.jar文件包含JSTL規範中定義的接口和相關類,standard.jar文件包含用於 實現JSTL的.class文件以及JSTL中5個標籤庫描述符文件(TLD)。將兩個jar包導入咱們工程的lib中。
在jsp文件開頭引入:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
3. JSTL核心庫的經常使用標籤
(1)<c:if>標籤
代替jsp腳本中的if else
(2)<c:for>標籤
代替jsp腳本中的for循環
<%-- Created by IntelliJ IDEA. User: jkzhao Date: 11/1/17 Time: 10:36 AM To change this template use File | Settings | File Templates. --%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <!-- jstl標籤常常和el配合使用,el能夠從域中取東西,jstl有邏輯的功能 --> <!-- test表明的是返回boolean的表達式 --> <c:if test="${1==1}"> <!-- 注意if 沒有else,能夠把test=後面寫相反的表達式 --> xxxxx <!-- 這裏不能直接寫1==1,解析時候會有問題。test只接受true或者false,不能運算表達式,須要藉助el作表達式運算 --> </c:if> <c:if test="${1!=1}"> yyyyy </c:if> <% request.setAttribute("count",10); %> <c:if test="${count==10}"> xxxxxxxxxxx </c:if> <!-- 好比電商網站,登陸後,右上角應該顯示用戶名,而再也不是登陸和註冊,這就用到了c:if, 見header.jsp--> <!-- forEach模擬 for(int i=0; i<=5; i++){} --> <c:forEach begin="0" end="5" var="i"> ${i }<br /> <!--el表達式直接輸出i--> </c:forEach> <!-- 模擬加強for循環 for(Product product : productList){ System.out.print(product.getPname()); }--> <c:forEach items="${productList}" var="pro"> <!--items表示的是一個集合或數組,通常藉助el去域中取集合, var表明的是集合中的某一個元素--> ${pro.pname } </c:forEach> </body> </html>
【示例】:
1)遍歷List<String>的值
2)遍歷List<User>的值
3)遍歷Map<String,String>的值
4)遍歷Map<String,User>的值
5)遍歷Map<User,Map<String,User>>的值
entry.key-----User
entry.value------List<String,User>
<%-- Created by IntelliJ IDEA. User: jkzhao Date: 11/1/17 Time: 2:00 PM To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.*" %> <%@ page import="com.itheima.domain.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <% //模擬List<String> strList List<String> strList = new ArrayList<String>(); strList.add("itcast"); strList.add("itheima"); strList.add("boxuegu"); strList.add("shandingyu"); request.setAttribute("strList", strList); //遍歷List<User>的值 List<User> userList = new ArrayList<User>(); User user1 = new User(); user1.setId(2); user1.setName("lisi"); user1.setPassword("123"); userList.add(user1); User user2 = new User(); user2.setId(3); user2.setName("wangwu"); user2.setPassword("123"); userList.add(user1); application.setAttribute("userList", userList); //遍歷Map<String,String> Map<String,String> strMap = new HashMap<String,String>(); strMap.put("name","lucy"); strMap.put("age", "18"); strMap.put("addr","西三旗"); strMap.put("email","lucy@itcast.cn"); session.setAttribute("strMap", strMap); //遍歷Map<String,User> Map<String,User> userMap = new HashMap<String,User>(); userMap.put("user1", user1); userMap.put("user2", user2); request.setAttribute("userMap", userMap); %> <h1>取出strList數據</h1> <c:forEach items="${strList}" var="str"> ${str } <br/> </c:forEach> <h1>取出strList數據</h1> <c:forEach items="${userList}" var="user"> user的name:${user.name }-----user的password:${user.password }<br/> </c:forEach> <h1>取出strMap數據</h1> <c:forEach items="${strMap}" var="entry"> <!--Map裏面每一個元素是個鍵值對--> ${entry.key }=======${entry.value }<br/> </c:forEach> <h1>取出userMap數據</h1> <c:forEach items="${userMap}" var="entry"> <!--Map裏面每一個元素是個鍵值對--> ${entry.key }:${entry.value.name }---${entry.value.password }<br/> </c:forEach> </body> </html>
以前聽的23種設計模式是java基礎班的基礎設計模式。
1. 什麼是模式
模式在開發過程當中總結出的「套路」,總結出的一套約定俗成的設計模式
2. javaEE經歷的模式
model1模式:
技術組成:jsp+javaBean
model1的弊端:隨着業務複雜性,致使jsp頁面比較混亂
model2模式:
技術組成:jsp+servlet+javaBean
model2的優勢:開發中,使用各個技術擅長的方面
MVC:-- web開發的設計模式
【注意】:MVC什麼語言都有,屬於web開發的設計模式,PHP、.net都有。下面的講的三層架構只有JavaEE纔有。
3. javaEE的三層架構
服務器開發時,分爲三層:
開發實踐時,三層架構經過包結構體現。