JSP學習筆記:簡介,內置對象,JavaBean

一、jsp簡介

1、jsp三大指令

page指令:<%@page 屬性=”” %>,位於jsp頁面頂端,可以有多個

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.text.*"%>

taglib指令:標籤庫

include
include指令:<%@include file="date.jsp"%>
include動作:<jsp:include page="url" flush="true|false"/>

page:要包含的頁面,
flush:被包含的頁面是否從緩衝區讀取

include指令與include動作的區別:
diff_between_includeCommand_includeAction

forward動作<jsp: forward page="url"/>
等同於:request.getRequestDispatcher("/url").forward(request,response);

param動作<jsp:param name="參數名" value="參數值">
常與一起使用,作爲其的子標籤:

<jsp:forward page="user.jsp">
    <%--用<jsp:param "></jsp:param>添加參數--%>
    <jsp:param name="email" value="11111111@163.com"></jsp:param>
</jsp:forward>

2、jsp註釋

  • html的註釋
  • jsp的註釋
    <%– jsp註釋 –>(客戶端不可見)
  • jsp腳本註釋:
    //單行
    /* */ 多行

3、jsp腳本

4、jsp聲明變量或方法

5、jsp表達式

<%=表達式 %> ps:不可;分號結束

6、jsp頁面的生命週期

jsp_life_cycle.png

二、jsp內置對象

* 九大內置對象:*out,request,response,session,application,Page,pageContext,exception,config

out


    <%
        out.println("<h2>靜夜思</h2>");
        out.println("牀前明月光<br>");
        out.println("疑是地上霜<br>");
        out.flush();
        /*out.clear();會拋出異常*/
        out.clearBuffer();//這裏不會拋出異常
        out.println("舉頭望明月<br>");
        out.println("低頭思故鄉<br>");
    %>

    緩衝區大小:<%= out.getBufferSize()%>byte<br>
    緩衝區剩餘大小:<%= out.getRemaining()%>byte<br>
    是否自動清空緩衝區:<%= out.isAutoFlush()%><br>

request


    <%
        request.setCharacterEncoding("utf-8");//解決post中文亂碼問題,但無法解決getget解決需要直接Tomcat配置文件
        request.setAttribute("password","123456");//設置屬性密碼
    %>
    用戶名:<%= request.getParameter("username")%><br>
    愛好:
    <%
        if(request.getParameterValues("favorite") != null){  //這裏需要判斷爲不爲空,jsp這裏不能將String數組看爲Boolean
            String[] favorites = request.getParameterValues("favorite");
            for (int i = 0; i < favorites.length; i++) {
                out.println(favorites[i] + "&nbsp;&nbsp;&nbsp;");
            }
        }
        String realPath = request.getRealPath("requset.jsp");%><br>

    密碼:
    <%=request.getAttribute("password")%><br>

    請求體的MIME類型:
    <%=request.getContentType()%><br>

    協議類型和版本號:
    <%=request.getProtocol()%><br>

    服務器主機名:
    <%=request.getServerName()%><br>

    服務器端口號:
    <%=request.getServerPort()%><br>

    請求文件長度:
    <%=request.getContentLength()%><br>

    請求的客戶端地址:
    <%=request.getRemoteAddr()%><<br>

    請求的真實路徑:
    <%=request.getRealPath("requset.jsp")%><br>

    請求的上下文路徑:
    <%=request.getContextPath()%>

response


    <%
        response.setContentType("text/html;charset=utf-8");
        out.println("<h1>response內置對象</h1>");
        out.println("<hr>");
        //out.flush();
        /* 因爲getWrite獲得的輸出流對象會先於內置對象out輸出,
        所以要先清空緩衝區,使out強制輸出,否則結果會是先輸出outer
        再輸出out*/

        PrintWriter outer = response.getWriter();
        outer.println("大家好,我是response生成的輸出流outer");
    //  response.sendRedirect("login.jsp");//重定向
    //  response.sendRedirect("request.jsp");
        request.getRequestDispatcher("request.jsp").forward(request,response);//轉發
    %>

請求轉發和請求重定向的區別:

redirectAndTransmit.PNG

session
HttpSession的實例,週期:在第一個jsp頁面被加載時自動創建,即瀏覽器連接到服務器開始,關閉瀏覽器離開這個服務器結束,在服務器的幾個頁面之間切換,服務器應當知道這是一個客戶,就可以用session對象


    <%
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 HH:mm:ss");
        Date d = new Date(session.getCreationTime());
        session.setAttribute("username","pinnuli");
        session.setAttribute("password","123456");
        session.setAttribute("age",20);
    //  session.setMaxInactiveInterval(10);

    //設置session最大生成期限,單位秒,也可在web.xml中設置session-timeout

    %>
    Session創建時間:
    <%=sdf.format(d)%><br>

    Session的ID:
    <%=session.getId()%><br>

    Session中獲取屬性值:
    <%=session.getAttribute("username")%><br>

    Session保存的屬性數組:
    <%
        String[] names = session.getValueNames();
        for(int i=0; i<names.length; i++){
            out.println(names[i] + "&nbsp;&nbsp;");
        }
    //        session.invalidate();//銷燬當前會話,每次刷新一次頁面就會新建一個session
    %><br>
    <%--測試不同頁面是否同一個session--%>
    <a href="session_page2.jsp">跳轉到session_page2</a>

application
實現用戶間數據的共享,可存放全局邊變量,相當於java的靜態變量


    <%
        application.setAttribute("city","廣州");
        application.setAttribute("postcode","510000");
        application.setAttribute("email","[email protected]");
    %>
    所在城市:<%=application.getAttribute("city")%><br>
    所有屬性:
    <%
        Enumeration attributes = application.getAttributeNames();
        while (attributes.hasMoreElements()){
            out.println(attributes.nextElement() + "&nbsp;&nbsp;");
        }
    %><br>

    jsp(serviet)引擎名和版本號:<%=application.getServerInfo()%><br>

page、pageContext

    <h3>page:</h3>當前page頁面的字符串描述:<%=page.toString()%><br><br>

   <h3>pageContext:</h3>用戶名:從session中獲取屬性-<%=pageContext.getSession().getAttribute("username")%><br>

    <%--跳轉到其他頁面--%>
   <%--<%
       pageContext.forward("out.jsp");
   %>--%>
    include方法,包含其他頁面:
        <%
            pageContext.include("out.jsp");
        %>

exception


    異常消息:<%=exception.getMessage()%><br>
    異常的字符串描述:<%=exception.toString()%>

三、jsp使用Javabean

1. Javabean的設計原則

  • 必須是公有類
  • 必須包含無參構造方法
  • 屬性私有
  • 用getter()和setter()進行封裝

例如:


public class Students{
    private String name;
    private int age;

    public  Students(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

2. 存取Javabean有關的jsp動作元素

在jsp頁面中使用Javabeans:

方法一:像使用普通java類一樣,創建Javabean實例
方法二:在jsp頁面中通常使用jsp動作標籤使用javabean,常用的動作標籤:userBeans、setProperty、getProperty

  • <jsp:useBeans>

在jsp頁面中實例化或者在指定範圍內使用Javabean:

<jsp:useBeans id="標示符" class="java類名" scope="作用範圍"/>

scope屬性:指定Javabean的作用範圍
page:當前頁面,重定向和轉發都無效
request:可通過HttpRequest.getAttribute()取得Javabean對象,重定向無效,轉發有效
session:可通過HttpSession.getAttribute()取得Javabean對象,同個會話有效
application:可通過application.getAttribute()取得Javabean對象,不同會話都有效

例如:

    <jsp:useBean id="myUsers" class="com.po.Users" scope="application"></jsp:useBean>
    用戶名:<jsp:getProperty name="myUsers" property="username"></jsp:getProperty>
    密碼:<jsp:getProperty name="myUsers" property="password"></jsp:getProperty>

也可使用內置對象獲取:

    用戶名:<%=((Users)application.getAttribute("myUsers")).getUsername()%>
    密碼: <%=((Users) application.getAttribute("myUsers")).getPassword()%>

  • <jsp:setProperty>
    根據表單自動匹配所有屬性:
    <jsp:setProperty name="myUsers" property="username"></jsp:setProperty>

    根據表單匹配部分屬性:
    <jsp:setProperty name="myUsers" property="username"></jsp:setProperty>

    與表單無關,通過手工賦值給屬性:
    <jsp:setProperty name="myUsers" property="password" value="hahahaha"></jsp:setProperty>

    通過url傳參數給屬性賦值:
    <jsp:setProperty name="myUsers" property="password" param="testparam"></jsp:setProperty>
  • <jsp:getProperty>
    使用getProperty獲取屬性值:
    <jsp:getProperty name="myUsers" property="username"></jsp:getProperty>

參閱:
慕課網:JAVA遇見HTML——JSP篇

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章