JavaWeb學習之動態頁面技術(JSP/EL/JSTL)
JSP技術
1、jsp腳本和註釋
jsp腳本:
1、<% java代碼%> 內部的Java代碼翻譯到service方法的內部
2、<%=java 變量或表達式%> 會被翻譯成service方法內部out.print()
3、<%! java 代碼%> 會被翻譯成servlet的成員(成員變量或者成員方法)的內容
jsp註釋:不同的註釋可見範圍是不同的
1、Html註釋:<!--註釋內容--> 可見範圍有jsp源碼、翻譯後的servlet、頁面顯示Html源碼
2、Java註釋://單行註釋 /*多行註釋*/ 可見範圍jsp源碼、翻譯後的servlet
3、jsp註釋:<%註釋內容%> 可見範圍jsp源碼可見
2、jsp運行原理----jsp的本質就是servlet
jsp在第一次被訪問時會被web容器翻譯成servlet,再執行
過程:第一次訪問---->helloServlet.jsp----->helloServlet_jsp.java---->編譯運行
註:被翻譯後的servlet在Tomcat的work目錄中可以找到
3、jsp的三大指令、九大內置對象
jsp的指令是指導jsp翻譯和運行的命令
1、page指令-----屬性最多的指令(在實際開發中page指令默認),根據不同的屬性,指導整個頁面的特性
格式:<%@page 屬性名1=“屬性值1” 屬性名2=“屬性值2”...%>
常用屬性如下:
language:jsp腳本中可以嵌入的語言種類
pageEncoding:當前jsp文件的本身編碼--內部可以包括contentType
contentType:response.setContentType("text/html;charset=UTF-8");
session:是否jsp在翻譯時自動創建session
import:導入Java的包
errorPage:當 當前頁面出錯後跳轉到哪個頁面 (一般是發生500錯誤,當然狀態碼也可以認為的設置)
isErrorPage:當前頁面是一個處理錯誤的頁面
例如: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="true" import="java.util.ArrayList" errorPage="error.jsp"%>
2、include指令-----頁面包含(靜態包含)指令,可以將一個jsp頁面包含到另一個jsp頁面中
格式:<%@ include file="被包含的文件地址"%>
<%@ 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> <%@include file="/header.jsp"%> <h1>這是內容部分</h1> <%@include file="/footer.jsp"%> </body> </html>
3、taglib指令-----在jsp頁面中引入標簽庫(jstl標簽庫、struts2標簽庫)
格式:<%@taglib uri="標簽庫地址" prefix="前綴"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
jsp的內置/隱式對象(9個)
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類的實例(在普通類中的this) |
pageContext |
javax.servlet.jsp.PageContext |
JSP的頁面容器 |
exception |
java.lang.Throwable |
表示JSP頁面所發生的異常,在錯誤頁中才起作用 |
(1)out對象
out的類型:JspWriter
out作用就是想客戶輸出內容-----out.write()
out緩沖區默認8Kb 可以設置為0,代表關閉out緩沖區,內容會被直接寫到response緩沖區中。
<%out.write("aaa");%>
<%out.write("bbb"); %>
<%response.getWriter().write("asd"); %>
<%out.write("ddd"); %>
上述代碼的執行過程:當執行out.write();服務器會將裏面的aaa、bbb、ddd存入out緩沖區中,執行response.getWriter().writer()時服務器會將裏面的asd存入response緩沖區中,最後將out緩沖區中的內容刷到response緩沖區中,由於asd先進入緩沖區,所以最終結果為:asd、aaa、bbb、ddd
如果將buffer設置為0Kb,則關閉out緩沖區,out.write()裏面的數據找不out到緩沖區,會默認存到response緩沖區中。最後的結果應為:aaa、bbb、asd、ddd
(2)pageContext對象----是一個域對象
jsp頁面的上下文對象,page對象是設置頁面屬性的,例如導包、設置語言、數據編碼格式等,與pageContext對象不是一回事
pageContext同樣擁有域對象的三個方法
setAttribute(String name,Object obj)
getAttribute(String name)
removeAttribute(String name)
pageContext可以向指定的其他域中存取數據
setAttribute(String name,Object obj,int scope)
getAttribute(String name,int scope)
removeAttrbute(String name,int scope)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("name", "zhangsan"); %> <% pageContext.setAttribute("name", "lisi", pageContext.REQUEST_SCOPE); %> <% pageContext.setAttribute("name", "wangwu", pageContext.SESSION_SCOPE); %> <% pageContext.setAttribute("name", "xiaohong", pageContext.APPLICATION_SCOPE); %> <%=pageContext.getAttribute("name", pageContext.REQUEST_SCOPE)%> <% String name = (String) pageContext.getAttribute("name"); %> <%=name%> <%=pageContext.findAttribute("name")%> </body> </html>
findAttribute(String name)
依次從pageContext域,request域,session域,application域中獲取屬性,在某個域中獲取後將不再向後尋找
四大作用域的總結:
page域:當前jsp頁面範圍
request域:一次請求
session域:一次會話
application域:整個web應用
可以獲得其他8大隱式對象
例如: pageContext.getRequest()
pageContext.getSession()
jsp標簽(動作)
1)頁面包含(動態包含):<jsp:include page="被包含的頁面"/>
2)請求轉發:<jsp:forward page="要轉發的資源" />
靜態包含與動態包含的區別?
靜態包含,底層源碼只生成一個Java文件
動態包含,有幾個jsp,底層源碼就會生成幾個Java文件
二者執行效果一樣。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>include1</h1>
<!--靜態包含與動態包含--> <!-- <jsp:include page="/include2.jsp"></jsp:include> --> <%request.setAttribute("name", "zhangsna"); %> <jsp:forward page="/include2.jsp"/> </body> </html>
EL技術
1.EL 表達式
EL(Express Lanuage)表達式可以嵌入在jsp頁面內部,減少jsp腳本的編寫,EL 出現的目的是要替代jsp頁面中腳本的編寫。
2、從域中取出數據(掌握)
例如:jsp腳本:<%=request.getAttribute(name)%>
EL表達式替代:${requestScope.name}
EL技術重要的就是從四大域中取數據,格式:${EL表達式}
獲取pageContext域中的數據:${pageScope.key}
獲得request域中的數據:${requestScope.key}
獲得session域中的數據:${sessionScope.key}
獲得application域中的數據:${applicationScope.key}
EL從四個域中獲得某個值${key};
---同樣是依次從pageContext域,request域,session域,application域中 獲取屬性,在某個域中獲取後將不在向後尋找
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="com.oracle.demo01.User"%> <%@ 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.setAttribute("aaa", "bbb"); request.setAttribute("name", "張三"); User u = new User(); u.setId(1); u.setName("lisi"); u.setPwd("123"); session.setAttribute("user", u); List<User> list = new ArrayList<User>(); User u1 = new User(); u1.setId(1); u1.setName("wangwu"); u1.setPwd("456"); list.add(u1); User u2 = new User(); u2.setId(1); u2.setName("wangwu"); u2.setPwd("456"); list.add(u2); application.setAttribute("List", list); %> <%--使用jsp腳本取值 --%> <%=request.getAttribute("name") %> <hr> <%--使用EL表達式從域中取值 --%> ${requestScope.name } ${sessionScope.user.id } ${sessionScope.user.name } ${applicationScope.List[0].name } ${pageScope.aaa } </body> </html>
EL的11個內置對象
獲得JSP域中的數據:pageScope、requestScope、sessionScope、applicationScope
接收參數:param、paramValues 相當於request.getParameter()、request.getParameterValues()
獲取請求頭信息:header、headerValues 相當於request.getHeader(name)
獲取全局初始化參數:initParam 相當於this.getServletContext().getInitParameter(name)
WEB開發中的cookie:cookie 相當於request.getCookies()--cookie.getName()---cookie.getValue()
pageContext- WEB開發中的pageContext.
pageContext獲得其他八大對象
${pageContext.request.contextPath} 獲得web應用的名稱 相當於 <%=pageContext.getRequest().getContextPath%> 這句代碼不能實現
EL執行表達式
例如:
${1+1}
${empty user}
${user==null?true:false}
<%@ 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> <%--獲取請求的參數 --%> ${param.name } ${header["User-Agent"] } ${cookie.goods.name } ${cookie.goods.value } ${pageContext.request.contextPath} ${1+1} ${empty user} ${user==null?true:false} </body> </html>
JSTL技術
JSTL(JSP Standard Tag Library),JSP標準標簽庫,可以嵌入在jsp頁面中使用標簽的形式完成業務邏輯等功能。jstl出現的目的同el一樣也是要代替jsp頁面中的腳本代碼。JSTL標準標準標簽庫有5個子庫,但隨著發展,目前常使用的是他的核心庫Core
標簽庫 |
標簽庫的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 |
fn |
JSTL下載與導入
從Apache的網站下載JSTL的JAR包。進入 “http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/”網址下載 JSTL的安裝包,並解壓
使用jsp的taglib指令導入核心標簽庫
JSTL核心庫的常用標簽
1)<c:if test=””>標簽
其中test是返回boolean的條件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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> <%--test中只能寫表達式 結合EL表達式使用 --%> <c:if test="${1==1 }"> xxxxxx </c:if> <c:if test="${1!=1 }"> yyyyy </c:if> <c:forEach items="${List }" var="user"> ${user.name } ${user.pwd } </c:forEach> </body> </html>
2)<c:forEach>標簽
使用方式有兩種組合形式:
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>
<%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@page import="com.oracle.demo01.User"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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> <% //List<String> List<String> l1 = new ArrayList<String>(); l1.add("zhangsan"); l1.add("李四"); request.setAttribute("l1", l1); //List<User> List<User> l2 = new ArrayList<User>(); User u1 = new User(); u1.setId(1); u1.setName("zhang"); u1.setPwd("123"); l2.add(u1); User u2 = new User(); u2.setId(2); u2.setName("lisd"); u2.setPwd("123"); l2.add(u2); session.setAttribute("l2", l2); //Map<String,String> Map<String, String> m1 = new HashMap<String, String>(); m1.put("1", "chengyaojin"); m1.put("2", "houyi"); m1.put("3", "diaochan"); m1.put("4", "yase"); session.setAttribute("m1", m1); //Map<String,User> Map<String, User> m2 = new HashMap<String, User>(); User u3 = new User(); u3.setId(1); u3.setName("ahe"); u3.setPwd("123"); m2.put("1", u3); User u4 = new User(); u4.setId(2); u4.setName("dd"); u4.setPwd("123"); m2.put("2", u4); session.setAttribute("m2", m2); %> <%--遍歷List<String>類型 --%> <c:forEach items="${l1 }" var="str"> ${str }<br> </c:forEach> <hr> <%--遍歷List<User>類型 --%> <c:forEach items="${l2 }" var="user"> ${user.id }:${user.name }:${user.pwd }<br> </c:forEach> <hr> <%--遍歷Map<String,String>類型 --%> <c:forEach items="${m1 }" var="entry"> ${entry.key }:${entry.value }<br> </c:forEach> <hr> <%--遍歷Map<String,User>類型 --%> <c:forEach items="${m2 }" var="entry"> ${entry.key }:${entry.value.id }:${entry.value.name }:${entry.value.pwd }<br> </c:forEach> </body> </html>
JavaEE的開發模式
model1模式:
技術組成:jsp+javaBean
model1的弊端:隨著業務復雜性 導致jsp頁面比較混亂
model2模式
技術組成:jsp+servlet+javaBean
model2的優點:開發中 使用各個技術擅長的方面
servlet:擅長處理java業務代碼
jsp:擅長頁面的實現
JavaEE的三層架構
服務器開發時 分為三層
web層:與客戶端交互
service層:復雜業務處理
dao層:與數據庫進行交互
開發實踐時 三層架構通過包結構體現
MVC:---- web開發的設計模式
M:Model---模型 javaBean:封裝數據
V:View-----視圖 jsp:單純進行頁面的顯示
C:Controller----控制器 Servelt:獲取數據--對數據進行封裝--傳遞數據-- 指派顯示的jsp頁面
JavaEE三層架構+MVC
web層:收集頁面數據,封裝數據,傳遞數據,指定響應jsp頁面
service層:邏輯業務代碼的編寫
dao層:數據庫的訪問代碼的編寫
JavaWeb學習之動態頁面技術(JSP/EL/JSTL)