JSP技術:html
jsp腳本:java
1)<%java代碼%> ----- 內部的java代碼翻譯到service方法的內部服務器
2)<%=java變量或表達式> ----- 會被翻譯成service方法內部out.print()session
3)<%!java代碼%> ---- 會被翻譯成servlet的成員的內容oracle
jsp註釋: 不一樣的註釋可見範圍是不一樣app
1)Html註釋:<!--註釋內容--> ---可見範圍 jsp源碼、翻譯後的servlet、頁面 顯示html源碼dom
2)java註釋://單行註釋 /*多行註釋*/ --可見範圍 jsp源碼 翻譯後的servletjsp
3)jsp註釋:<%--註釋內容--%> ----- 可見範圍 jsp源碼可見ui
<%@ 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> <!--這是HTML註釋 --> <% //這是java當行註釋 /*這是java多行註釋*/ int i=100; System.out.println(i); %> <%--這是jsp註釋 --%> <%=i %> <%! String str="袁一鳴"; %> <%=str %> </body> </html>
jsp運行原理:jsp本質就是servletthis
jsp指令(3個)
jsp的指令是指導jsp翻譯和運行的命運。
一、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:噹噹前頁面出錯後跳轉到哪一個頁面
isErrorPage:當前頁面是一個處理錯誤的頁面
<%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" errorPage="error.jsp"%> <!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 y=1/0; HttpSession session=request.getSession(); session.setAttribute("goods", "naiping"); %> <% ArrayList<String> arr=new ArrayList<String>(); %> </body> </html>
二、include指令
格式:<%@ 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> 這是頭部 </body> </html>
<%@ 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>
這是尾部
</body>
</html>
<%@ 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" %><br>
這是內容<br>
<%@ include file="footer.jsp" %>
</body>
</html>
三、taglib指令:
格式:<%@ taglib uri="標籤庫地址" prefix="前綴"%>
四、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頁面所發生的異常,在錯誤頁中才起做用 |
out對象:
out的類型:JspWriter
out做用就是向客戶端輸出內容----out.write()
out緩衝區默認8kb 能夠設置成0 表明關閉out緩衝區 內容直接寫到respons緩衝器
<%@ 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> aaaaaaa <% out.write("bbbbbbbb"); response.getWriter().write("ccccccc"); %> <%="dddddddd" %> </body> </html>
jsp頁面的上下文對象,做用以下:
page對象與pageContext對象不是一回事
1)pageContext是一個域對象
setAttribute(String name,Object obj)
getAttribute(String name)
removeAttrbute(String name)
pageContext能夠向指定的其餘域中存取數據
setAttribute(String name,Object obj,int scope)
getAttribute(String name,int scope)
removeAttrbute(String name,int scope)
findAttribute(String name)
---依次從pageContext域,request域,session域,application域中獲取屬性,在某個域中獲取後將不在向後尋找
<%@ 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("goods", "奶瓶"); //向request域中設置值 pageContext.setAttribute("goods", "腦殼", pageContext.REQUEST_SCOPE); //向session域中設置值 pageContext.setAttribute("goods", "方便麪", pageContext.SESSION_SCOPE); //向application域中設置值 pageContext.setAttribute("goods", "快樂水", pageContext.APPLICATION_SCOPE); %> <% System.out.println((String)pageContext.findAttribute("goods")); %> <% /* //從request域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.REQUEST_SCOPE)); //從session域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.SESSION_SCOPE)); //從application域中取值 System.out.println(pageContext.getAttribute("goods",pageContext.APPLICATION_SCOPE)); */ %> </body> </html>
pageContext對象能夠得到其餘8大隱式對象。
一、頁面包含(動態包含):<jsp:include page="被包含的頁面"/>
二、請求轉發:<jsp:forward page="要轉發的資源" />
EL技術:
EL(Express Lanuage)表達式能夠嵌入在jsp頁面內部,減小jsp腳本的編寫,EL 出現的目的是要替代jsp頁面中腳本的編寫
jsp腳本:<%=request.getAttribute(name)%>
EL表達式替代上面的腳本:${requestScope.name}
EL最主要的做用是得到四大域中的數據,格式${EL表達式}
EL得到pageContext域中的值:${pageScope.key};
EL得到request域中的值:${requestScope.key};
EL得到session域中的值:${sessionScope.key};
EL得到application域中的值:${applicationScope.key};
EL從四個域中得到某個值${key};
---一樣是依次從pageContext域,request域,session域,application域中 獲取屬性,在某個域中獲取後將不在向後尋找
JSTL技術:
使用jsp的taglib指令導入核心標籤庫<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="">標籤
<%@ 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:if test="${1==1 }"> 正確 </c:if> <c:if test="${1!=1 }"> 錯誤 </c:if> --%> <!--普通for--> <c:forEach begin="0" end="10" var="i"> ${i } </c:forEach> </body> </html>
<c:forEach>標籤
<%@page import="com.oracle.domain.User"%> <%@page import="java.nio.channels.SeekableByteChannel"%> <%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@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> <% //建立List<User> User user1=new User(); user1.setAge(8); user1.setName("袁一鳴"); User user2=new User(); user2.setAge(10); user2.setName("熊大"); ArrayList<User> list=new ArrayList<User>(); list.add(user1); list.add(user2); request.setAttribute("UserList", list); //建立Map<String,User> Map<String,User> map=new HashMap<String,User>(); map.put("1", user1); map.put("2", user2); application.setAttribute("UserMap", map); //建立List<String> ArrayList<String> arr=new ArrayList<String>(); arr.add("a"); arr.add("b"); session.setAttribute("str", arr); %> <c:forEach items="${str }" var="s"> ${s } </c:forEach> <!-- for(User user:UserList){ System.out.println(user.getName()+"..."+user.getAge()); } --> <c:forEach items="${UserList }" var="user"> ${user.name }...${user.age } </c:forEach> <c:forEach items="${UserMap }" var="entry"> ${entry.key }...${entry.value.name }...${entry.value.age } </c:forEach> </body> </html>