JSP 內置對象(一)

jsp提供了9個內置對象,該對象會自動進行實例化操作

4種屬性範圍

page 只在一個保存屬性,跳轉無效 request 一次請求保存屬性,跳轉依舊有效 session 同一會話有效 application 整個服務器上保存,所有用戶都可使用

page屬性

一個屬性設置在本頁上,跳轉後無法獲得

 Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午1:02
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


// 設置page屬性
pageContext.setAttribute("name", "ming");
pageContext.setAttribute("birtday", new Date());
%>
// 重page中取出屬性
String username = (String)pageContext.getAttribute("name");
Date userbirthday = (Date)pageContext.getAttribute("birtday");
%>
姓名
生日


複製代碼

request 屬性

服務器跳轉後,屬性會被繼續保存 瀏覽器的URL地址會發生改變

 Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午1:02
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


request.setAttribute("name", "ming");
request.setAttribute("birthday", new Date());
%>
<forward>


複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午1:29
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>






複製代碼

session屬性

當一個屬性設置以後,任何一個與設置頁面相關的頁面都可以取得 即,session,session屬於服務器端保存.

cokice 屬於客戶端保存

 Created by IntelliJ IDEA. 

User: ming
Date: 19-3-10
Time: 下午3:07
To change this template use File | Settings | File Templates.
--%>




<title>Title/<title>


// 設置session屬性範圍
session.setAttribute("name", "ming");
session.setAttribute("birthday", new Date());
%>




複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午3:07
To change this template use File | Settings | File Templates.
--%>




<title>Title/<title>


// 取出session屬性
String username = (String)session.getAttribute("name");
Date userbirthday = (Date)session.getAttribute("birthday");
%>




複製代碼

application

此為公共參數 此屬性保存在服務器上

 Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午10:17
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


application.setAttribute("name", "ming");
application.setAttribute("birthday", new Date());
%>




複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午10:20
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


// application中獲得屬性
String username = (String)application.getAttribute("name");
Date userbirthday = (Date)application.getAttribute("birthday");
%>




複製代碼

request對象

接收客戶端發送的請求,請求的參數,頭部信息.





<title>Title/<title>





複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-10
Time: 下午11:54
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


// 接收參數
String content = request.getParameter("info");
%>



複製代碼

接收全部請求參數

用getParameterNames

顯示全部頭信息

使用getHeaderNames()

角色驗證

額...依舊沒啥東東 學過

response

定時跳轉

定時跳轉屬於客戶端跳轉

操作cookie

額..依舊沒啥 在response中調用addCookie 需要注意的是會返回一個jsessionid

session

當服務器端使用session的時候,可以保存在redis中 會有一個不重複的編號,即session id

cookie中保存的jsessionid為同樣道理

登錄 註銷

實現思路,設置session範圍的屬性,需要驗證的頁面進行判斷session 即,保存用戶的信息,使用session進行保存

 Created by IntelliJ IDEA.
User: ming
Date: 19-3-11
Time: 下午9:17
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>



// 用戶名 密碼
// 獲得name

String name = request.getParameter("uname");
// 獲得password
String password = request.getParameter("upass");
// 進行用戶名密碼比對
if(!(name==null||"".equals(name)
|| password == null || "".equals(password))
){
if("admin".equals(name) && "admin".equals(password)){
// 跳轉
response.setHeader("refresh", "2;URL=welcome.jsp");
// 設置session
session.setAttribute("userid", name);
%>

用戶登錄成功,兩秒後將會跳轉到歡迎頁!


如果沒有跳轉,點擊


}else{
%>

用戶名密碼錯誤


}
}
%>


複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-11
Time: 下午10:06
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


// 設置兩秒跳轉
response.setHeader("refresh", "2;URL=login.jsp");
// 清除session

session.invalidate();
%>

成功退出本系統,兩秒跳轉回首頁


如果沒有跳轉,訪問




複製代碼
Created by IntelliJ IDEA.
User: ming
Date: 19-3-11
Time: 下午9:59
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>


// 判斷此用戶的session是否設置過
if(session.getAttribute("userid")!=null){
%>

歡迎


註銷登錄


}else{
%>

非法訪問


}
%>


複製代碼

判斷新用戶

使用isnew的方式, 原理,在第一次訪問的時候,給客戶端設置cokkie,然後再次訪問的時候,會帶上cokkie中的jsessionid,用來判斷是否為新用戶

用戶操作時間

使用getCreationTime獲取第一個session創建的session時間,和最後一次操作的時間,用來判斷秒數

application對象

用來獲取serlet對象上下文 ServletContext表示整個容器的操作 使用表單輸入要保存的文件名稱和內容,直接在web項目的根目錄的note文件夾中保存文件





<title>Title/<title>





複製代碼




Created by IntelliJ IDEA.
User: ming
Date: 19-3-11
Time: 下午10:46
To change this template use File | Settings | File Templates.
--%>



<title>Title/<title>



// 接收保存的文件名稱
String name = request.getParameter("filename");
// 接收文件內容
String content = request.getParameter("filecontent");
// 獲得文件名稱
String fileName = this.getServletContext().getRealPath("/") + "note" + File.separator + name;
// 獲得文件對象
File file = new File(fileName);
// 用於判斷父文件夾是否存在
if(!file.getParentFile().exists()){
// 創建文件夾
file.getParentFile().mkdir();
}
// 定義打印流對象
PrintStream printStream = null;
// 創建一個到文件的輸入流
printStream = new PrintStream(new FileOutputStream(file));
// 往流中輸入內容
printStream.println(content);
// 關閉流
printStream.close();
%>
// 通過Scanner獲取流的輸入
Scanner scanner = new Scanner(new FileInputStream(file));
// 設置讀取分隔符
scanner.useDelimiter("\n");
// 新建緩衝區
StringBuffer stringBuffer = new StringBuffer();
// 讀取內容,保存進入緩衝區
while(scanner.hasNext()){
stringBuffer.append(scanner.next()).append("
");
}
// 關閉
scanner.close();
%>




分享到:


相關文章: