當1個人在沉思的時候,他其實不是在閑著。有看得見的勞動,也有看不見的勞動。 —— 雨果
java server page 運行在服務器真個頁面. ==> 本質就是servlet.
運行流程:
jsp =(第1次訪問時)=> .java ==> .class ==> 運行
JSP中的腳本:
<% java內容 %> 該腳本包裹的代碼會出現在 service方法中
<%=表達式 %> 該腳本用于輸出內容.out.print();方法包裹輸出內容.
<%! 內容 %>(了解): 該腳本包裹的內容會出現在類定義中.
代替 腳本 => 輸出腳本 <%= %>
格式: ${表達式}
EL表達式可以在4個域中取數據 => 4個內置對象 applicationScope/requestScope/sessionScope/pageScope
從指定域取值: ${requestScope.name}<br>
${applicationScope.name}<br>
不指定域取值: ${name}<br> => 從小域到大域中查找.顯示最早找到的.
分類
page屬性包括在“<%@ page ”和“%>”
之間。
page 指令
include指令
taglib 指令
功能:
page : 描寫頁面的1些屬性.
include: 靜態包括指令
taglib : 導入標簽指令
page : <%@ page language="java" import="java.util.*" pageEncoding="UTF⑻"%>
page指令用于指定頁面1些基本屬性.
language="java" 頁面中使用的語言為java.
*import="java.util.*" 就是導包. 是所有屬性中唯逐一個可以在頁面中出現屢次的屬性.
*pageEncoding="UTF⑻" 頁面保存到硬盤編碼.
*contentType="text/html; charset=UTF⑻" 發送給閱讀器的編碼 .
以上兩個碼表最好1致. 但是1般設置1個屬性便可.另外1個屬性自動設置.
autoFlush="true" 如果緩沖區裝滿是不是自動刷新到閱讀器. 如果裝滿并沒有選擇自動刷新,那末會拋出異常.
buffer="8kb" 決定jsp輸出緩沖區大小為8kb
errorPage="" 配置當前頁面的毛病頁面
isErrorPage="false" 指定當前頁面是不是是1個毛病頁面
開發中,我們可使用以下配置統1配置毛病頁面 比上面的方式要省事:
<error-page>
<error-code>500</error-code>
<location>/page/B.jsp</location>
</error-page>
extends="" 決定當前jsp的父類是誰.父類必須是servlet的子類.
info="" getServletInfo 剛方法的返回值.
isELIgnored="false" 決定當前頁面能否使用 EL表達式. 默許值就是支持el.
session="true" 當前jsp頁面是不是可以直接使用session對象.默許值就是true.
<%@ include file=“filename” %>
<% String url="index.html" ; %>
<%@ include file = "<%= url %>" %>
<%@ include file = "jw.jsp?nm=browser" %>
比如
包括的是目標文件的源碼;包括過來,1起翻譯
main.jsp
<%
String s = “abc”;
%>
<%include file=“part.jsp” %>
part.jsp
<%=s %> 沒有定義變量s
雖然part.jsp本身會有毛病
但是運行main.jsp就能夠正確引入part.jsp
指的在jsp中不加以聲明就能夠直接使用的9個對象.
原理: 由于我們的代碼是寫在jsp對應java的service方法中的.所以在service方法中聲明的變量,我們可以直接使用.
public void _jspService(HttpServletRequest 1>request, HttpServletResponse 2>response)
throws java.io.IOException, ServletException {
PageContext 3>pageContext = null;
HttpSession 4>session = null;
Throwable 5>exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);
if (exception != null) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ServletContext 6>application = null;
ServletConfig 7>config = null;
JspWriter 8>out = null;
Object 9>page = this;
*本身是1個域對象. 在pageContext對象上有1個map. 這個Map就是Page域.
操作: 4個操作.
范圍: 就只在當前頁面中有用.
作用: 在jsp中應避免在頁面上書寫任何java代碼.
//pageContext.setAttribute(name, value);
// pageContext.getAttribute(name);
//pageContext.removeAttribute(name);
//pageContext.getAttributeNamesInScope(scope);
*pageContext對象還可以操作其他3個域
目的就是為了方便.
pageContext.setAttribute("name", "applicationTom",PageContext.APPLICATION_SCOPE );
pageContext.setAttribute("name", "sessionTom",PageContext.SESSION_SCOPE );
pageContext.setAttribute("name", "requestTom",PageContext.REQUEST_SCOPE );
//pageContext.getAttribute(name, scope);
//pageContext.removeAttribute(name, scope);
//pageContext.getAttributeNamesInScope(scope);
//pageContext.findAttribute("name") 會從所有域中查找鍵. 從小到大
*持有其他8個內置對象的援用.根據這個對象可以取得其他8個內置對象
pageContext.getRequest();
pageContext.getResponse();
pageContext.getSession();
pageContext.getServletContext();
pageContext.getException();
pageContext.getServletConfig();
pageContext.getOut();
pageContext.getPage();
out 對象是jsp當中的輸出對象.
代碼:
out.print("a");
response.getWriter().print("b");
out.print("c");
response.getWriter().print("d");
輸出: bd ac
原理:
在輸出到閱讀器時,會先把兩個流合并,再輸出.
合并時response的字符流在前.
JSPWriter在后. 所以不管代碼書寫順序如何.終究
response流的內容總會在JSPwriter流的內容之前
結論: 在jsp中輸出使用out(JSPWriter)輸出,不要使用response.getWriter輸出.
JSP標簽也稱之為Jsp Action(JSP動作)元素,它用于在Jsp頁面中提供業務邏輯功能,避免在JSP頁面中直接編寫java代碼,造成jsp頁面難以保護。
語法
<jsp:include page={"relativeURL" | "<%= expression %>"} />
<jsp:include page={"relativeURL" | "<%= expression %>"} >
<jsp:param name="PN"
value="{PV | <%= expression %>}" /> *
</jsp:include>
<jsp:useBean>
使用1個ID和1個給定作用范圍和同1ID的JavaBean相干聯
<jsp:setProperty>
設置JavaBean的屬性值
<jsp:getProperty>
獲得JavaBean的屬性值
<jsp:include>
要求時文件包括
<jsp:forward>
接受用戶輸入并將要求分派給另外一頁面
<jsp:param>
forward標簽詳解
</head>
<body>
<%-- JSP動作標簽
分擔jsp頁面的java代碼
--%>
<jsp:forward page="/index.jsp"></jsp:forward>
<%-- //下面的代碼相當于上面的標簽
request.getRequestDispatcher("/index.jsp").forward(request, response);
--%>
</body>
</html>
include標簽詳解
<body>
<%-- JSP動作標簽
分擔jsp頁面的java代碼
--%>
<jsp:include page="/index.jsp"></jsp:include>
<%--
// jsp 動態包括
request.getRequestDispatcher("/index.jsp").include(request, response);
--%>
</body>
<jsp:include>
與include指令的比較
<jsp:include>
標簽是動態引入, <jsp:include>
標簽觸及到的2個JSP頁面會被翻譯成2個servlet,這2個servlet的內容在履行時進行合并。 <jsp:include>
標簽,還是include指令,它們都會把兩個JSP頁面內容合并輸出,所以這兩個頁面不要出現重復的HTML全局架構標簽,否則輸出給客戶真個內容將會是1個格式混亂的HTML文檔。 1.自定義轉換器
public class MyDateConverter implements Converter {
//參數2 : 待轉換的類型傳遞進來. => 2012⑴2⑴2
// 轉換好以后,使用返回值返回 => Date對象
//參數1: 告知你要轉換成甚么類型的
public Object convert(Class arg0, Object arg1) {
//創建格式化器
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//使用格式化器將參數格式化成日期類型
try {
Date date = format.parse(arg1.toString());
//沒出異常.就返回日期對象.
return date;
} catch (ParseException e) {
e.printStackTrace();
//出現異常返回null
return null;
}
}
}
2. 轉換器注冊
//注冊我們自定義的轉換器
//參數1 自定的轉換器
//參數2 注冊轉換的類型
ConvertUtils.register(new MyDateConverter(), Date.class);
完成如上兩步便可.
首先創建1個學生類
//javaBean
//1.要求為屬性提供get/set方法任意之1
//2.需要有空參構造
//3.實現串行化接口(可選)
public class User {
private String name;
private String password;
private int age;
private Date hiredate;
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + ", age=" + age
+ ", hiredate=" + hiredate + "]";
}
}
1個小Demo
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 創建User對象
User u = new User();
// 將表單參數封裝到User對象
//參數1: 填寫User對象
//參數2: 填寫需要封裝到User對象的參數Map
try {
//如果我們需要BeanUtils支持非8大基本數據類型.我們只要給BeanUtils添加類型轉換器便可
//注意:注冊類型轉換器,必須寫在populate方法之前.
ConvertUtils.register(new MyDateConverter(), Date.class);
//BeanUtils在封裝時可以完成類型轉換. 自動轉換的范圍 只限于 8個基本數據類型
BeanUtils.populate(u,request.getParameterMap());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(u);
/* //1 取得參數
String name = request.getParameter("name");
String password = request.getParameter("password");
//2 封裝參數 User
User u = new User();
u.setName(name);
u.setPassword(password);*/
//-------------------------------------------------------
//3 將User對象交給業務類處理
//4 根據處理結果
//成功=>在session加入成功標識,并重定向到成功頁面
//失敗=>回到登錄頁面.提示毛病信息
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
用于代替,簡化頁面中的java代碼.
Java standard Tag Library => java標準標簽庫
apache組織提供1套已開發好的標簽庫.
這套標簽庫在javaee 5.0版本后已納入標準.(使用不需要導包)
該套標簽庫1共分為4個庫:
core:核心庫(用的最多的)****
fmt:格式化庫(2個)
xml:xml的操作相干(廢棄)
sql: 與數據庫操作相干(廢棄)
<%-- <c:if>(經常使用) 判斷
test="${num1 > num2 }" 填寫返回值為boolean的表達式
var="if" 鍵
scope="page" 域 將判斷結果以var屬性值為鍵放入該屬性指定的域中.
--%>
<%
request.setAttribute("num1", 1000);
request.setAttribute("num2", 10000);
%>
<c:if test="${num1 > num2 }" var="if" scope="page" >
num1 利害!
</c:if>
${pageScope.if}
<hr>
if else
<%-- 判斷標簽.支持if else情勢
<c:choose>
<c:when>
test: 填寫返回值為boolean的表達式
<c:otherwise>
--%>
<c:choose>
<c:when test="${num1 > num2 }">
num1 利害!
</c:when>
<c:otherwise>
num2 利害!
</c:otherwise>
</c:choose>
<hr>
遍歷標簽
<%-- <c:forEach>(經常使用) 遍歷標簽
items="${requestScope.list}" 要便利的集合設置給該屬性
var="abc" 每次遍歷集合中元素 該屬性值作為鍵放入page域
varStatus="st" 每次遍歷的狀態,會封裝成1個對象 以該屬性值為鍵 放入page域
數數的功能
begin="1" 從幾開始數
end="100" 數到幾
step="1" 每次數幾個數
var="num" 將當前數的數以該屬性值作為鍵放入page域
--%>
<%
List list = new ArrayList();
list.add("tom");
list.add("jerry");
list.add("jack");
list.add("rose");
request.setAttribute("list", list);
%>
<table border="1">
<tr>
<th>名稱</th>
<th>是不是是集合中第1個元素</th>
<th>是不是是集合中最后1個元素</th>
<th>顯示當前遍歷的索引</th>
<th>顯示當前遍歷的計數</th>
</tr>
<c:forEach items="${requestScope.list}" var="abc" varStatus="st" >
<tr id="${st.index%2==0?"even":"odd"}" >
<td>${pageScope.abc}</td>
<td>${pageScope.st.first}</td>
<td>${pageScope.st.last}</td>
<td>${pageScope.st.index}</td>
<td>${pageScope.st.count}</td>
</tr>
</c:forEach>
</table>
<hr>
<!-- ----------------------------------------------------------------------- -->
<c:forEach begin="1" end="100" step="1" var="num" >
${num}
</c:forEach>
fmt庫 格式化庫
格式化日期
<!--
格式化日期
fmt:formatDate
-->
<fmt:formatDate value="<%=new Date() %>"
pattern="yyyy/MM/dd hh:mm:ss" var="date" scope="request" />
${requestScope.date}
格式化數字
<!--
格式化數字
fmt:formatNumber
-->
<fmt:formatNumber value="3.1415926" pattern="0000.00000000000" var="num1" scope="request" ></fmt:formatNumber>
<fmt:formatNumber value="3.1415926" pattern="####.###########" var="num2" scope="request" ></fmt:formatNumber>
${requestScope.num1}<br>
${requestScope.num2}<br>
路徑總結
條件: 所有路徑都應以”/”開頭.
項目名:day10-jsp
資源名:AServlet
客戶端路徑 => 給閱讀器用的路徑 => 填寫項目名稱
<form action="/day10-jsp/AServlet" >
<img src="http://www.jyygyx.com/upload/caiji/20160922//day10-jsp/AServlet" >
<a href="/day10-jsp/AServlet" >
response.sendRedirect("/day10-jsp/AServlet")
服務器端路徑 => 給服務器端使用的路徑 => 填寫項目下的路徑
request.getRequestDispatcher("/AServlet")
errorPage="/AServlet"
<location>/AServlet</location>
上一篇 C++11學習
下一篇 Oracle分頁查詢性能優化