編譯指令
page
include
編譯指令是通知jsp引擎的消息,作用是設(shè)置jsp程序的屬性,以及由該jsp生成的servlet的屬性比如指定jsp文件使用的腳本語言、文件編碼等凌那,它不直接生成輸出阻问,編譯指令都有默認(rèn)值芭梯,無需為編譯指令設(shè)置值险耀。
page
一般位于頁面的最上方,一個頁面可以有多個page指令
<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.util.ArrayList,java.util.LinkedList"%>
<%@page errorPage="handle.jsp"%>
contentType有兩個作用:1.設(shè)置MIME類型 2.設(shè)置網(wǎng)頁編碼
pageEncoding為設(shè)置腳本語言的編碼
import用來引入包玖喘,多個包用逗號分隔
errorPage為設(shè)置當(dāng)jsp應(yīng)用內(nèi)發(fā)生錯誤時跳轉(zhuǎn)到的頁面
include
用來引入外部文件甩牺,如果是jsp文件則會先解析jsp語句,被引入的文件不能出現(xiàn)重定義源jsp文件中的變量否則會報錯累奈,實質(zhì)是編譯指令include會執(zhí)行引入文件的編譯指令贬派,將其融合成到一個servlet中
實例:在html中引入html
index.jsp
<%@ include file ="time.html"%>
<div style="width:500px;height:200px;overflow:scroll;">
<%@include file="quote.html" %>
</div>
time.html
<p id="current_time"></p>
<script type="text/javascript">
document.getElementById("current_time").innerText=new Date().toLocaleString();
setInterval(function(){
document.getElementById("current_time").innerText=new Date().toLocaleString();
},1000)
</script>
quote.html
<%@page pageEncoding="utf-8"%> //告訴jsp引擎,頁面編碼是utf-8澎媒,不然中文會亂碼
<pre>
一些代碼搞乏。。戒努。
</pre>
動作指令
forward
include
useBean
forward
用來重定向頁面
<jsp:forward page="login.jsp">
<jsp:param value="haha" name="username"/>
<jsp:param value="123" name="password"/>
</jsp:forward>
include
靜態(tài)導(dǎo)入時被導(dǎo)入頁面的編譯指令會起作用请敦,動態(tài)導(dǎo)入時被導(dǎo)入頁面的編譯指令不會起作用,并且可以帶參
<jsp:include page="color_block.jsp">
<jsp:param value="red" name="bgcolor"/>
</jsp:include>
useBean
什么是javaBean储玫,從名字直譯來看就是豆子侍筛,而豆子都是圓滾滾的看起來都一樣,所以所有的javaBean都遵循相同的約定撒穷,使用方式上也是差不多的匣椰,這樣javaBean就可以很容易被重用或者被插入到其他應(yīng)用程序中
javaBean必須符合的約定:
1.有一個公共的默認(rèn)的構(gòu)造函數(shù)
2.javaBean內(nèi)的所有屬性必須通過get()和set()方法來訪問
3.javaBean必須都是可序列化的
實例:使用javaBean
index.jsp
<form action="user.jsp" method="post">
<input type="text" name="userName"/>
<input type="password" name="password"/>
><input type="submit">
</form>
UserEntity.class
package com.userBean.lhd;
import java.io.Serializable;
public class UserEntity implements Serializable {
private String userName;
private String password;
//ctrl+shift+f快速格式化代碼
public UserEntity() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
user.jsp
<jsp:useBean id="user" class="com.userBean.lhd.UserEntity"></jsp:useBean>
<jsp:setProperty property="userName" name="user"/>
<jsp:setProperty property="password" name="user"/>
<jsp:getProperty property="userName" name="user"/>
<jsp:getProperty property="password" name="user"/>
內(nèi)置對象
out
Request
Response
Session
HTTP是無狀態(tài)協(xié)議,每次客戶端檢索網(wǎng)頁時端礼,都要單獨打開一個服務(wù)器連接禽笑,因此服務(wù)器不會記錄下先前客戶端請求的任何信息。
有4種方法來維持客戶端與服務(wù)器的會話:
1.Cookies
網(wǎng)絡(luò)服務(wù)器可以指定一個唯一的session ID作為cookie來代表每個客戶端蛤奥,用來識別這個客戶端接下來的請求佳镜。這不是一種有效的方式,因為很多時候瀏覽器并不一定支持cookie凡桥。
2.隱藏表單域
一個網(wǎng)絡(luò)服務(wù)器可以發(fā)送一個隱藏的HTML表單域和一個唯一的session ID
<input type="hidden" name="sessionid" value="123">
表單被提交時蟀伸,指定的名稱和值將會自動包含在GET或POST數(shù)據(jù)中。每當(dāng)瀏覽器發(fā)送一個請求唬血,sessionid的值就可以用來保存不同瀏覽器的軌跡
3.重寫URL
在每個URL后面添加一些額外的數(shù)據(jù)來區(qū)分會話望蜡,服務(wù)器能夠根據(jù)這些數(shù)據(jù)來關(guān)聯(lián)session標(biāo)識符唤崭。比如http://sessionStudy.html;sessionid=12345拷恨。
相比而言,重寫URL是更好的方式谢肾,瀏覽器不支持cookies也能工作腕侄,但缺點是必須為每個URL動態(tài)指定session ID。
4.JSP內(nèi)置session對象
JSP利用servlet提供的HttpSession接口來識別一個用戶,存儲這個用戶的所有訪問信息冕杠。
默認(rèn)情況下微姊,JSP允許會話跟蹤,一個新的HttpSession對象將會自動地為新的客戶端實例化分预。禁止會話跟蹤需要顯式地關(guān)掉它兢交,通過將page指令中session屬性值設(shè)為false來實現(xiàn)。
實例:使用session對象統(tǒng)計用戶訪問情況
<%
// 獲取session創(chuàng)建時間
Date createTime = new Date(session.getCreationTime());
// 獲取最后訪問頁面的時間
Date lastAccessTime = new Date(session.getLastAccessedTime());
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");
// 檢測網(wǎng)頁是否由新的訪問用戶
if (session.isNew()){
session.setAttribute(userIDKey, userID);
session.setAttribute(visitCountKey, visitCount);
} else {
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount += 1;
userID = (String)session.getAttribute(userIDKey);
session.setAttribute(visitCountKey, visitCount);
}
%>
<html>
<head>
<title>Session 跟蹤</title>
</head>
<body>
<h1>Session 跟蹤</h1>
<table border="1" align="center">
<tr bgcolor="#ccc">
<th>Session 信息</th>
<th>值</th>
</tr>
<tr>
<td>id</td>
<td><% out.print( session.getId()); %></td>
</tr>
<tr>
<td>創(chuàng)建時間</td>
<td><% out.print(createTime); %></td>
</tr>
<tr>
<td>最后訪問時間</td>
<td><% out.print(lastAccessTime); %></td>
</tr>
<tr>
<td>用戶 ID</td>
<td><% out.print(userID); %></td>
</tr>
<tr>
<td>訪問次數(shù)</td>
<td><% out.print(visitCount); %></td>
</tr>
</table>
</body>
</html>
Application
Config
Page
Exception
PageContext