一、介紹
1.定義:用於開發動態web資源地1技術,jsp實際上就是servlet
jsp = html + jsp
2.
html:靜態內容
servlet:適合編寫java邏輯代碼,不是和寫界面顯示部分
jsp:適合編寫輸出動態內容,但是不適合編寫java邏輯
3.原理:將jsp頁面先翻譯成.java代碼,然後在將.java代碼編譯成.class
三、基礎語法
1.html標籤
2.
a)<% %> 小腳本,裏面填寫Java代碼
b)<%= %> 表達式,相當於out.print();
c)<%! %>:表示將裏面的代碼,翻譯到類的裏面,也就是可以將變量變成全局變量,還可以聲明方法和靜態塊
3.jsp的註釋:
jsp註釋:<%--註釋的內容--%> :安全
網頁註釋::不安全
四、jsp的三個指令
1.<%@ page import ="所要導入的包名"%>
注:
1)
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
2)session默認值是true,不需要我們去開啓
errorPage:表示如果頁面有錯誤,就跳轉到指定資源
isErrorPage:是否創建throwable對象,默認是false
contextType:等同於response.setContextType("text/html;charset=utf-8");
2.Inclue
1).靜態包含:把其他資源包含到前頁面中
<%@ include file ="/其他頁面的名稱.jsp"%>
2)動態包含:
<jsp:include page="/其他頁面的名稱.jsp"></jsp:include>
3)區別:
1.翻譯的時間段不同
靜態:翻譯時就把兩個文件合併
動態:不會合並文件,當代碼執行到include時,才包含1另一個文件的內容
注:使用原則就是能用靜態的就不用動態的
3.taglib
1)作用:在jsp頁面中導入jstl標籤庫。替換jsp中的java代碼片段。
這裏我們需要導入jar包
a)jstl.jar standard.jar
b)https://mvnrepository.com/
格式:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
五、jsp的六個動作
<jsp:include>動態包含
<jsp:forward>請求轉發
<jsp:param>設置請求參數
<jsp:useBean>創建一個對象
<jsp:setProperty>給指定的對象屬性賦值
<jsp:getProperty>取出指定對象的屬性值
1.jsp動作標籤---轉發
攜帶參數相當於,在網址後面進行拼接:
/2.jsp?username=ws&passowrd=123
注:轉發的同時還可以攜帶請求的參數,不要在<jsp:forward動作中間加註釋(會編譯出錯)
2.代碼:
<%@ 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("page", "pageContex");
%>
<jsp:forward page="/2.jsp">
<jsp:param value="ws" name="username"/>
<jsp:param value="123" name="password"/>
</jsp:forward>
</body>
</html>
2.jsp中的代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%=
/*該域對象,只在本頁面有效,出了頁面獲取不到 */
pageContext.getAttribute("page")
%>
<%=request.getParameter("username") %>
<%=request.getParameter("password") %>
六、九個內置對象
1.request
2.response
3.session
4.application
5.exception
6.page
7.config
8.out
9.pageContext
1.查看pageContext中的方法
public void fun(PageContext pageContext) {
//強大的pageContext可以獲取任何域對象
//從小到大
pageContext.getRequest();
pageContext.getSession();
pageContext.getServletContext();
//強大的方法(el表達式,就依賴這個方法)
//這個方法可以搜索所有域對象,用key去取值
//按域的大小,從小到大,進行搜索,找到爲止,找到就輸出對應的value
//request session context 這個順序搜索
pageContext.findAttribute("key");
}
2.測試域中的查找順序
<%@ 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參數,一般會是常量 */
/*向pageContext域中添加一對鍵值對 */
/* 只要有pageContext,就能向各個域中添加值,
參數三,表示對應域的常量 */
pageContext.setAttribute("p", "pageContext",PageContext.PAGE_SCOPE);
pageContext.setAttribute("p", "request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("p", "session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("p", "appliction",PageContext.APPLICATION_SCOPE);
%>
<%=
/*從小到大找的 */
pageContext.findAttribute("p")
%>
</body>
</html>
七、el(expression language表達式
1.定義:表達式語言,要簡化jsp中java代碼開發,他不是一種開發語言,而是jsp中獲取數據的一種規範
2.功能
el表達式能獲取存在4個作用域中的數據
${u} 相當於 pageContext.findAttribute("u");
el獲取對於null這樣的數據,在頁面中表現爲空字符串
代碼:
4.jsp:測試把對象存到域對象中是否能獲取
<%@page import="com.lanou3g.Address"%>
<%@page import="com.lanou3g.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>
<%
User user = new User();
user.setUsername("ws");
user.setPassword("123");
//添加地址對象
Address address = new Address();
address.setCity("zh");
user.setAddress(address);
out.print(user.getAddress());
request.setAttribute("u", user);
%>
${u.username }
${u.address.city}
${u["username"] }
${u['username'] }
<%-- ${u[username] } --%>
</body>
</html>
5.jsp
測試:空字符串和沒有元素的集合
<%@page import="java.util.ArrayList"%>
<%@ 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>
<%
String str1 = null;
request.setAttribute("str1", str1);
String str2 = "";
request.setAttribute("str2", str2);
ArrayList list1 = new ArrayList();
list1.add("aaaa");
ArrayList list2 = new ArrayList();
request.setAttribute("list1", list1);
request.setAttribute("list2", list2);
ArrayList list3 = null;
request.setAttribute("list3", list3);
%>
${empty str1 }
${empty str2 }
${empty list1[0] }
${empty list2}
${empty list3}
${empty list3 ? "我是前面的":"我是後面的" }
<br>
</body>
</html>
八、測試隱式對象可以直接在el表達式中使用
<%
pageContext.setAttribute("p", "pageContext",PageContext.PAGE_SCOPE)
pageContext.setAttribute("p", "request",PageContext.REQUEST_SCOPE)
pageContext.setAttribute("p", "session",PageContext.SESSION_SCOPE)
pageContext.setAttribute("p", "appliction",PageContext.APPLICATION_SCOPE)
%>
${pageScope.p }
${requestScope.p }
${sessionScope.p }
${applicationScope.p }
8.jsp:pageContext代表對象,使用方法
${www }
<form action="${pageContext.request.contextPath }/9.jsp" method="post"><br>
<input type="text" name="username"><br>
<input type="checkbox" name="hobby" value="唱歌">唱歌
<input type="checkbox" name="hobby" value="籃球">籃球<br>
<input type="submit" value="提交">
</form>
9.jsp
<%
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
%>
${param.username }
${paramValues.hobby[0] }
${paramValues.hobby[1] }
${header["User-Agent"] }<br>
${cookie.JSESSIONID.value }
九、JSTL(JavaServerPages Standard Tag Library)表達式
1.jsp標準標籤庫
2.JSTL的作用
使用JSTL實現JSP頁面中邏輯處理。如判斷、循環等。
注:使用JSTL需要在JSP頁面添加taglib指令 添加核心庫
3.通用標籤: set、 out、 remove
代碼:
<%@ 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>
<c:set var="num" value="10" scope="session"></c:set>
<c:out value="${num }" default="aaaa"></c:out><br>
${num }
<c:remove var="num"/>
${num }
11.jsp:if choose條件標籤
<%@ 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>
<c:set var="n" value="10"></c:set>
<c:if test="${n<5}">
aaaaa
</c:if>
<c:if test="${n>5 }">
bbbb
</c:if>
<c:choose>
<c:when test="${n==5 }">5</c:when>
<c:when test="${n==10 }">10</c:when>
<c:otherwise>mm</c:otherwise>
</c:choose>
</body>
</html>
12.jsp:循環
<%@page import="java.util.ArrayList"%>
<%@ 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>
<%
ArrayList<String> list = new ArrayList();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
request.setAttribute("list", list);
%>
<c:forEach items="${list }" var ="l" step="1">
${l}<br>
</c:forEach>
<%
%>
</body>
</html>
3.13.jsp:值,索引,計數,第一個元素 最後一個元素
<%@page import="java.util.ArrayList"%>
<%@ 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>
<%
ArrayList<String>list = new ArrayList();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
request.setAttribute("list", list);
%>
<table border="1">
<tr>
<th>值</th>
<th>索引</th>
<th>計數</th>
<th>第一個元素</th>
<th>最後一個元素</th>
</tr>
<c:forEach items="${list }" var="l" varStatus="vs" >
<tr>
<th>${l }</th>
<th>${vs.index }</th>
<th>${vs.count }</th>
<th>${vs.first }</th>
<th>${vs.last }</th>
</tr>
</c:forEach>
</table>
</body>
</html>
十、MVC
代碼的設計模式(編寫代碼)
1.Model1模式
jsp + javaBean<jsp:useBean/> 模式
2.Model2模式(模擬MVC模式)
jsp(儘量寫頁面和展示數據) + servlet(邏輯處理) + javaBean(實體類) 模式
M:Model(數據模型)
V:View(視圖界面)
C:Controller(控制器)