JSP 內置對象(一)

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

4種屬性範圍

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

page屬性

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

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午1:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 設置page屬性
    pageContext.setAttribute("name", "ming");
    pageContext.setAttribute("birtday", new Date());
%>
<%
    // 重page中取出屬性
    String username = (String)pageContext.getAttribute("name");
    Date userbirthday = (Date)pageContext.getAttribute("birtday");
%>
姓名 <%=username%>
生日 <%=userbirthday%>
</body>
</html>

request 屬性

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

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午1:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setAttribute("name", "ming");
    request.setAttribute("birthday", new Date());
%>
<jsp:forward page="request_scope_02.jsp"/>
</body>
</html>
<%@ page import="java.util.*" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午1:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%=(String)request.getAttribute("name")%>
<%=(Date)request.getAttribute("birthday")%>
</body>
</html>

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.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 設置session屬性範圍
    session.setAttribute("name", "ming");
    session.setAttribute("birthday", new Date());
%>
<!-- 超鏈接跳轉 -->
<a href="session_scope_02.jsp">超級鏈接跳轉</a>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午3:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 取出session屬性
    String username = (String)session.getAttribute("name");
    Date userbirthday = (Date)session.getAttribute("birthday");
%>
<%=username%>
<%=userbirthday%>
</body>
</html>

application

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

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午10:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    application.setAttribute("name", "ming");
    application.setAttribute("birthday", new Date());
%>
<!-- 超級鏈接跳轉 -->
<a href="./application_scope_02.jsp">超級鏈接獲得屬性</a>
</body>
</html>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午10:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // application中獲得屬性
    String username = (String)application.getAttribute("name");
    Date userbirthday = (Date)application.getAttribute("birthday");
%>
<%=username%>
<%=userbirthday%>
</body>
</html>

request對象

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="request_demo01.jsp" method="post">
        <input type="text" name="info">
        <input type="submit" value="submit">
    </form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-10
  Time: 下午11:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 接收參數
    String content = request.getParameter("info");
%>
<%=content%>
</body>
</html>

接收全部請求參數

用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.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="login.jsp" method="post">
        用戶名
        <input type="text" name="uname"/>
        密碼
        <input type="password" name="upass"/>
        <input type="submit" value="登錄">
    </form>
<%
    // 用戶名 密碼
    // 獲得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);
        %>
            <h3>用戶登錄成功,兩秒後將會跳轉到歡迎頁!</h3>
            <h3>如果沒有跳轉,點擊<a href="./welcome.jsp">這裏</a></h3>
        <%
        }else{
            %>
                <h3>用戶名密碼錯誤</h3>
            <%
        }
    }
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-11
  Time: 下午10:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 設置兩秒跳轉
    response.setHeader("refresh", "2;URL=login.jsp");
    // 清除session
    session.invalidate();
%>
<h3>成功退出本系統,兩秒跳轉回首頁</h3>
<h3>如果沒有跳轉,訪問<a href="login.jsp">點我</a> </h3>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-11
  Time: 下午9:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 判斷此用戶的session是否設置過
    if(session.getAttribute("userid")!=null){
        %>
            <h3>歡迎<%=session.getAttribute("userid")%></h3>
            <h3>註銷登錄<a href="./logout.jsp">點我</a></h3>
        <%
    }else{
        %>
            <h3>非法訪問</h3>
        <%
    }
%>
</body>
</html>

判斷新用戶

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

用戶操作時間

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

application對象

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="./input_content.jsp" method="post">
        輸入文件名稱 <input type="text" name="filename"/><br/>
        輸入文件內容 <textarea name="filecontent" cols="30" rows="3">
    </textarea>
        <input type="submit" value="保存"/>
        <input type="reset" value="重置"/>
    </form>
</body>
</html>
<%@ page import="java.io.File" %>
<%@ page import="java.io.PrintStream" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.util.Scanner" %>
<%@ page import="java.io.FileInputStream" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-11
  Time: 下午10:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 接收保存的文件名稱
    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("<br>");
    }
    // 關閉
    scanner.close();
%>
<%=stringBuffer%>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章