1. 程式人生 > 實用技巧 >Servlet_03 進階隨筆 兩種域的運用呼叫方式

Servlet_03 進階隨筆 兩種域的運用呼叫方式

12 Servlet_04 Servlet增刪改查 靜態頁面與動態頁面 EL表示式 table表格的一些樣式

今天學習了servlet的增刪改查:

儲存資料
setAttribute(String name,Object obj );
獲取資料
getAttribute(String name);
刪除資料
removeAttribute(String name);

servlet小知識點:

req 請求

resp 反饋

Servlet request域(只能使用一次)

Servlet Context域(能夠長時間使用,隨伺服器關閉而關閉)

靜態頁面與動態頁面:

html 頁面被稱為靜態頁面 頁面內容基本上是不變的

jsp;php 動態頁面 (根據不同的情況顯示不同的內容,經常會隨著後端伺服器需求的變化而變化)
在jsp頁面上,需要動態接收後端伺服器傳輸給前段jsp介面的資料
通過EL表示式來實現動態的接收伺服器傳輸的資料 Expression Language
request域和ServletContext域中的值是可以傳輸到頁面中的

EL表示式:

EL表示式的語法: 遵從key-value鍵值對這種資料結構
通過key獲取value值
${key} key指的就是放進域中的name名稱值
EL表示式只能在jsp動態頁面中使用(歡迎介面可以),對於html頁面不支援

table 表格:

單元格與單元格之間的間距 外邊距 cellSpacing
單元格邊框與單元格內容之間的間距 內間距 cellpadding
合併table表格內部邊框線 border-collapse:collapse(合併)
合併一列中的多行 rowspan
合併一行中的多列 colspan

水平對齊方式 align let center rigdt
垂直對齊方式 valid top middle bottom
表格的背景顏色 blackground-color
表格邊框寬度 border
表格的標題 caption
文字內容居中 text-aline : center

Servlet例題格式:

正常流程:  

@WebServlet("/getData01")(對映)
//從tomcat伺服器中取出全域性域物件
        ServletContext context = req.getServletContext();
        //從全域性域物件中取出使用者名稱和密碼值
        if (context.getAttribute("username") != null && context.getAttribute("password") != null) {

            String username = context.getAttribute("username").toString();//null被引用了  觸發了空指標
            String password = context.getAttribute("password").toString();

            System.out.println("getData01" + username + "---");
            System.out.println("getData01" + password + "---");

        } else {
            //  表明該ServletContext域中沒有此使用者資訊  該返回首頁
            resp.sendRedirect("/index.jsp");
           // System.exit(0);  破壞性行為  直接停止虛擬機器(所有程式停止)
            return;//返回  讓當前正在執行的方法結束掉

        }
        //資源跳轉   getData02
        resp.sendRedirect("/getData02");



@WebServlet("/getData02")(對映)
 //從伺服器中獲取ServletContext物件
ServletContext context = req.getServletContext();
//從ServletContext域物件中取出使用者名稱和密碼值
String username = context.getAttribute("username").toString();
String password = context.getAttribute("password").toString();
System.out.println(username+"+++++");
System.out.println(password+"+++++");
// 資源跳轉 removeData01
resp.sendRedirect("/removeData01");