JavaWeb之Tomcat與數(shù)據(jù)庫

XML與Tomcat

XML:可擴展的標記語言

  • xml有什么用

    1. 可以用來保存數(shù)據(jù)
    2. 可以用來做配置文件
    3. 數(shù)據(jù)傳輸載體
  • 定義xml

    • 其實就是一個后綴位.xml的文件
    • 文檔聲明<? version='1.0' encoding='UTF-8' ?>
    • 當(dāng)xml中標簽過多,內(nèi)容中包含類似標簽的文字不想被解析,可以使用CDATA符號
  • XML解析:其實就是獲取元素里邊的字符數(shù)據(jù)或者屬性數(shù)據(jù)

    • xml解析方式:

      • DOM:整個xml讀取到內(nèi)存當(dāng)中,形成樹狀結(jié)構(gòu)。整個文檔稱之為document對象,屬性對應(yīng)Attribute對象,所有的元素節(jié)點對應(yīng)Element對象,文本也可以稱之為Text對象,以上所有對象都可以稱之為Node節(jié)點,如果xml特別大,會造成內(nèi)存溢出??梢詫ξ臋n進行增刪
      • SAX:Simple API for Xml 基于事件驅(qū)動。讀取一行,解析一行。不會造成內(nèi)存溢出。不可以增刪,只能查詢
    • xml解析的解決方案:jaxp、jdom、dom4j,dom4j是現(xiàn)在常用的

  • xml通過DTD或scheme來約束

Tomcat

  • 常用的服務(wù)器軟件:Tomcat、Weblogic、Websphere

Http&Servlet

  • 抓包工具httpWatch

  • Http協(xié)議:針對網(wǎng)絡(luò)上的客戶端 與 服務(wù)器端在執(zhí)行Http請求的時候,遵守的一種規(guī)范。其實就是規(guī)定了客戶端在訪問服務(wù)器的時候要帶上什么東西,服務(wù)器返回數(shù)據(jù)的時候也要帶上什么東西。

  • HTTP請求數(shù)據(jù)

    • 請求的數(shù)據(jù)包含三個部分:

      • 請求行(請求方式、請求路徑、協(xié)議版本):post/examples/servlets/servlet/RequestParamExample HTTP/1.1

      • 請求頭

        • Accept:客戶端向服務(wù)器表示我能支持什么類型的數(shù)據(jù)
        • Referer:真正請求的地址路徑,全路徑
        • Accpt-Language:支持語言
        • User-Agent:用戶代理 向服務(wù)器表明,當(dāng)前來訪的客戶端信息
        • Content-type:提交的數(shù)據(jù)類型。經(jīng)過URLEncoding編碼的form表單數(shù)據(jù)
        • Accept-Encoding:gzip,deflate:壓縮算法
        • Host:主機地址
        • Content-Length:數(shù)據(jù)長度
        • Connection:Keep-Alive 保持連接
        • Cache-Control:對緩存的操作
      • 請求體:瀏覽器發(fā)送給服務(wù)器的數(shù)據(jù),就是請求參數(shù)

    • 服務(wù)器響應(yīng)的數(shù)據(jù)

      • 響應(yīng)行(協(xié)議版本、狀態(tài)碼):HTTP/1.1 200 ok

      • 響應(yīng)頭:

        • Server:服務(wù)器是哪一種類型。Apache tomcat
        • Content-type:服務(wù)器返回給客戶端的內(nèi)容類型
        • Content-length:返回的數(shù)據(jù)長度
        • Date:通訊的日期,響應(yīng)的時間
      • 響應(yīng)體

    • GET請求和POST請求的區(qū)別

      • 請求路徑不同,get在請求地址后面跟上參數(shù),post不跟,post使用流的形式寫數(shù)據(jù)
      • get請求有安全隱患
      • get能夠攜帶的數(shù)據(jù)有限,1kb左右,post是以流的形式寫數(shù)據(jù),所以數(shù)據(jù)大小沒有限制

Web資源

  • 在HTTP協(xié)議中,規(guī)定了請求和響應(yīng)雙方,客戶端和服務(wù)器端。與web相關(guān)的資源就是web資源
  • 靜態(tài)資源:html、js、css
  • 動態(tài)資源:sevlet/jsp

Servlet

  • 其實就是一個java程序,運行在web服務(wù)器上,用于接收和響應(yīng)客戶端的請求。更多的是配合動態(tài)資源

  • 當(dāng)然靜態(tài)資源也需要servlet,只不過tomcat中已經(jīng)定義好一個默認的DefaultServlet

  • Hello servlet

    • 新建web項目,新建類繼承Servlet(servlet子類httpServlet)
    • 在web.xml配置Servlet,servlet-name和servlet-mapping
  • Servlet的生命周期

    • init:初始化方法,初次訪問創(chuàng)建實例時調(diào)用,一個實例只會初始化一次

    • service:只要客戶端來一個請求就執(zhí)行

    • destroy:servlet銷毀的時候,執(zhí)行該方法,項目從服務(wù)器移除的時候,正常關(guān)閉服務(wù)器也執(zhí)行

    • doGet和doPost不是生命周期方法

    • 讓servlet實例創(chuàng)建的時機提前

                        ```  <pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" style="box-sizing: border-box; white-space: normal; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; overflow: visible; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
      
      <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
      
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"> <load-on-startup>2</load-on-startup><!--值越小初始化越早,一般從2開始不寫負數(shù),tomcat默認的servlet使用1--></pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"></servlet></pre>
      
      </pre> 
      
      
    • ServletConfig獲取servlet配置參數(shù)

    • servlet配置方式:

      • 路徑匹配
      • 擴展名匹配
  • ServletContext

    • servlet上下文,每個web應(yīng)用只有一個ServletContext

    • 作用

      • 獲取全局配置參數(shù)(context-param)
      • 可以獲取web中的應(yīng)用資源
      • 存儲數(shù)據(jù),在servlet中共享數(shù)據(jù)
      • 作用范圍在本項目整個項目中
    • ServletContext何時創(chuàng)建何時銷毀

      • 服務(wù)器啟動的時候,會為托管的每一個web應(yīng)用創(chuàng)建一個ServletContext對象
      • 從服務(wù)器移出托管或者關(guān)閉服務(wù)器銷毀
  • HttpServletRequest

    • 可以獲取請求頭信息

    • 獲取協(xié)議,獲取遠程訪問來的ip:

    • 獲取客戶端提交上來的數(shù)據(jù):request.getParameter("name")

    • 請求中的亂碼問題

      • 解決方案

        get請求過來的數(shù)據(jù),在url地址欄已經(jīng)編碼了,tomcat收到數(shù)據(jù)默認使用ISO-8859-1來解碼,指定為utf-8

        ? username=new String(username.getBytes("ISO-8859-1"),UTF-8);

        POST請求在取數(shù)居前指定編碼:request.setCharacterEncoding("utf-8");//設(shè)置請求體文字編碼

  • HttpServletResponse

    • 負責(zé)返回數(shù)據(jù)給客戶端

    • 以字節(jié)或字符流寫數(shù)據(jù)到客戶端

    • 響應(yīng)數(shù)據(jù)亂碼問題

      • 1.指定輸出的時候使用utf-8編碼:response.setcharacterEncoding("utf-8");
      • 2.直接規(guī)定瀏覽器查看數(shù)據(jù)使用的編碼:response.setHeader("text/html;charset=UTF-8");
      • String 類的getBytes()默認使用的是UTF-8,tomcat默認是ISO-8859-1
      • response.setContentType("text/html;charset=UTF-8");//設(shè)置使用html文本類型,并使用UTF-8編碼;不管是字節(jié)還是字符流使用這一行代碼可以解決編碼問題

Cookie和Session

  • 請求轉(zhuǎn)發(fā)和重定向

    • response.sendRedirect("login_success.html");//返回碼是302,是重定向,兩次,可以跳轉(zhuǎn)任意路徑不是自己的也行
    • request.getRequestDispatcher("login_success.html").forward(request,responese);//返回碼200,請求轉(zhuǎn)發(fā),一次,只能跳磚自己項目的資源;效率比重定向高一點
Cookie
  • 定義:是一份小數(shù)據(jù),是服務(wù)器給客戶端,并且存儲在客戶端上的一份小數(shù)據(jù)。

  • 應(yīng)用場景:自動登錄、瀏覽記錄、購物車

  • 為什么有Cookie

    • 客戶端與服務(wù)器在通訊的時候,是無狀態(tài)的,其實就是客戶端在第二次來訪的時候,服務(wù)器根本就不知道這個客戶端來訪過沒有。為了更好的用戶體驗,更好的交互(自動登錄),也為了更好的收集用戶習(xí)慣(大數(shù)據(jù))
  • Cookie簡單用法

    • Cookie cookie=new Cookie("aa","bb"); response.addCookie(cookie);//發(fā)送cookie給客戶端
    • Cookie[] cookies=request.getCookies();//獲取客戶端帶過來的Cookie
    • 可以給客戶端多個Cookie
  • Cookie在沒有設(shè)置有效期的情況下關(guān)閉瀏覽器它就沒有了

    • cookie.setMaxAge(60607*24);//有效時間,以秒為單位,正數(shù)
  • 刪除cookie:cookie.setMaxAge(0);

  • 由于cookie會保存在客戶端上,所以有安全隱患,cookie的大小與個數(shù)有限制,為了解決這個問題,所以有Session

Session
  • 定義:是基于Cookie的一種會話技術(shù),數(shù)據(jù)存放在服務(wù)器端
  • 調(diào)用request.getSession創(chuàng)建,服務(wù)器關(guān)閉后銷毀,Session會話過期30分鐘也會銷毀
  • 會在cookie里面添加一個字段JSESSIONID,是Tomcat服務(wù)器生成
  • setAttribute,存數(shù)據(jù),在瀏覽器關(guān)閉后還有
  • getAttribute:取數(shù)據(jù)
  • removeAttribute:移除數(shù)據(jù)
  • getSessionId:獲取會話id
  • invalidate:強制讓會話失效

JSP&EL&JSTL

<%@ page %>
  • language 表示頁面支持java語言
  • content-type:表示是html文本,用utf-8編碼
  • pageEncoding:jsp內(nèi)容編碼
  • extends:用于指定jsp翻譯成java后繼承的父類
  • import:導(dǎo)包使用
  • Session:值為true或false,用于指定是否可以在本頁面直接使用Session對象
  • errorPage:頁面錯誤跳轉(zhuǎn),錯誤頁面用isErrorPage
JSP包含和請求轉(zhuǎn)發(fā)
  • 包含:

        <pre spellcheck="false" class="md-fences md-end-block contain-          
       cm modeLoaded" lang="" style="box-sizing: border-box; white-  
       space: normal; font-family: Consolas, &quot;Liberation 
       Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; 
      break-inside: avoid; text-align: left; overflow: visible; background- 
     image: inherit; background-position: inherit; background-size: 
     inherit; background-repeat: inherit; background-attachment: inherit; 
     background-origin: inherit; background-clip: inherit; background- 
    color: rgb(248, 248, 248); position: relative !important; border: 1px 
    solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; 
    margin-bottom: 15px; margin-top: 15px; width: inherit;">
    
            <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
    
        <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"><jsp:include page="other.jsp"><jsp:include></pre> </pre> 
    
  • 請求轉(zhuǎn)發(fā)

           <pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" style="box-sizing: border-box; white-space: normal; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; overflow: visible; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">
    
      <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
    
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"><jsp:forward page="oter.jsp"></pre>
    
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"> <!--頁面轉(zhuǎn)發(fā)攜帶參數(shù)--></pre>
    
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"> <jsp:param value="北京" name="address" /></pre>
    
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"></jsp:forward></pre>
    
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">request.getRequestDispatcher("other.jsp").forward(request,response);</pre>
    
      </pre>
    
JSP內(nèi)置對象
  • 所謂內(nèi)置對象就是我們可以直接在jsp使用這些對象,不用創(chuàng)建,四個作用域?qū)ο?/p>

  • "pageContext":此對象可以拿到其它八個對象

  • "request"

  • "session"

  • "application"

    以上四個是作用域?qū)ο螅硎具@些對象可以存值,它們的取值范圍有限定。共同的方法:setAttribute和getAttribute

    pageContext在當(dāng)前頁面有效,request請求轉(zhuǎn)發(fā)有效,重定向無效;作用域僅限于一次請求,只要服務(wù)器對該請求做出了響應(yīng),這個域中存的值就沒有了;session僅限于一次會話當(dāng)中;application整個工程都可以訪問,服務(wù)器關(guān)閉就沒有了

  • response:out和response一同使用,先輸出response內(nèi)容

  • out:out是放在response緩存區(qū),先輸出response本身的內(nèi)容,然后輸出out內(nèi)容

  • config:ServletConfig

  • page:Object類型,是這個jsp翻譯成java類實例的對象

  • exception:異常,是錯誤jsp頁面有的對象

EL表達式
  • EL表達式是為了簡化jsp代碼,具體簡化jsp里面的java代碼

  • 使用EL表達式取出作用域中的值

    • ${pageScope.name}

    • ${requestScope.name}

    • ${sessionScope.name}

    • ${applicationScope.name}

    • 取出作用域中的數(shù)組(或集合):{array[0]},{array[1]},${array[2]}

    • 取出作用域中的map值:{map.name},{map.age}

    • 在session中存儲對象,使用EL表達式取出

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" style="box-sizing: border-box; white-space: normal; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; overflow: visible; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">
            <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
            <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">//存儲對象到session</pre>
            <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"><%</pre>
            <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"> User user=new User("zhangsan",14);</pre>
            <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;"> session.setAttribute("u",user);</pre>
            <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">%></pre>
        <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">//EL判斷空</pre>

        <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">¥{empty u}</pre>

        <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">//使用EL取出</pre>

        <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">${u.name} ,${u.age}</pre>

        </pre></code>
  • EL表達式的11個內(nèi)置對象

    • pageContext(JSP)
    • pageScope(作用域)
    • requestScope(作用域)
    • sessionScope(作用域)
    • applicationScope(作用域)
    • header(請求頭)
    • headerValues(請求頭)
    • param(請求參數(shù))
    • paramValues(請求參數(shù))
    • cookie
    • initParam(初始化參數(shù))
JSTL
  • JSTL(Java Standard Tag Library),是一個不斷完善的開放源代碼的jsp標簽庫,是由apache的jakarta小組來維護的.

  • 它是用來簡化jsp代碼的,替換<% %>寫法,一般與EL表達式配合

  • 需要用到j(luò)stl.jar和standard.jar兩個庫,使用時要在jsp引入taglib,需要引入1.1版本才支持EL表達式

  • 常用標簽

    • <c:set><\c:set>
    • <c:if\ test="${age>16}">年齡大于16歲<\c:if>
    • <c:forEach begin="1" end="10" var="i" step="2" >${i}<\c:forEach>
    • <c:forEach var="user" items=" {list} " >{user.name}<\c:forEach>

事物&數(shù)據(jù)庫連接池&DBUtils

事物(Transaction):事物就是指一組操作,里邊包含許多個單一的邏輯
  • 為了確保一個完整的邏輯執(zhí)行成功,所以有了事物。例如銀行轉(zhuǎn)賬需要兩條數(shù)據(jù)都執(zhí)行成功才算成功,否則就失敗

  • 命令行演示事物

    • 數(shù)據(jù)庫默認是自動提交的,先關(guān)閉自動提交

                                          <pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" style="box-sizing: border-box; white-space: normal; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; overflow: visible; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">
      
      <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">show variables like '%commit%';//查看自動提交的狀態(tài)</pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">set autocommit = off;//關(guān)閉自動提交</pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">start transaction;//開啟事物</pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">rollback;//事物回滾,事物結(jié)束,數(shù)據(jù)回到最初狀態(tài)</pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">commit;//事物提交,事物結(jié)束,數(shù)據(jù)寫到磁盤</pre>
      
      <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">?</pre>
      
      </pre></code>
      
  • 代碼演示:

                                                  <pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="" style="box-sizing: border-box; white-space: normal; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-size: 0.9em; display: block; break-inside: avoid; text-align: left; overflow: visible; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 0px; width: inherit;">
    
    <textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="box-sizing: border-box; color: inherit; font: inherit; position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea>
    
    <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">//默認的連接事物是自動提交的</pre>
    
    <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">conn.setAutoCommit(false);//關(guān)閉自動提交</pre>
    
    <pre class=" CodeMirror-line " role="presentation" style="box-sizing: border-box; white-space: pre-wrap; font-family: inherit; break-inside: avoid; padding: 0px 4px; border-radius: 0px; border-top-width: 0px; border-right: none; border-bottom-width: 0px; border-left-width: 0px; background: 0px 0px; font-size: inherit; margin: 0px; overflow-wrap: break-word; color: inherit; z-index: 2; position: relative; overflow: visible; word-break: normal; width: inherit;">//事物正常執(zhí)行提交,異常就回滾.</pre>
    
    </pre>
    
  • 事物的特性

    • 原子性:事物中包含的邏輯不可分割。
    • 一致性:事物執(zhí)行的前后,數(shù)據(jù)的完整性保持一致。
    • 隔離性:事物執(zhí)行期間不應(yīng)該受到其它事物的影響。
    • 持久性:事物執(zhí)行成功,數(shù)據(jù)應(yīng)該持久保持到磁盤上。
  • 事物的安全隱患

    • 不考慮隔離級別會出現(xiàn)以下問題

      • 讀:臟讀、不可重讀、幻讀

        • 臟讀:一個事物讀到另外一個事物還未提交的數(shù)據(jù)。
        • 不可重讀:一個事物讀到了另外一個事物提交的數(shù)據(jù),造成了前后兩次查詢的結(jié)果不一致。
        • 幻讀:一個事物讀到了另一個事物已提交的插入數(shù)據(jù),導(dǎo)致多次查詢結(jié)果不一致。
      • 寫:

        • 丟失更新

          • B事物如果提交,導(dǎo)致A事物的修改沒有了。
          • B事物回滾,也會造成A事物更新沒有了。
        • 解決方法:

          • 悲觀鎖:select * from account for update;
          • 樂觀鎖:修改數(shù)據(jù)的時候同時修改數(shù)據(jù)庫版本。其它事物修改的時候先比對數(shù)據(jù)庫版本。
    • 隔離級別:
      select @@tx_isolation;//查看數(shù)據(jù)庫隔離級別 set session transaction isolation level read committed;//修改隔離級別位讀已提交

      • Read Uncommitted(讀未提交):指一個事物讀取到另外一個事物還未提交的數(shù)據(jù)。這就會引發(fā)臟讀,讀取到的數(shù)據(jù)是數(shù)據(jù)庫內(nèi)存中的數(shù)據(jù),并非磁盤上的真正數(shù)據(jù)。引發(fā)臟讀。
      • Read Committed(讀已提交):只能讀取到其它事物已經(jīng)提交的數(shù)據(jù),這會造成不可重復(fù)讀;當(dāng)有另外的事物操作數(shù)據(jù)并提交后,多次讀取數(shù)據(jù)就會出現(xiàn)前后結(jié)果不一致。解決臟讀,引發(fā)不可重復(fù)讀。
      • Repeatable Read(重復(fù)讀):Mysql默認的隔離級別就是這個,該隔離級別可以讓事物在自己的會話中重復(fù)讀取數(shù)據(jù),并且不會出現(xiàn)結(jié)果不一樣的狀況,即使其它事物已經(jīng)提交了,也依然顯示以前的數(shù)據(jù)。解決臟讀、不可重復(fù)讀,引發(fā)幻讀。
      • Serializable(可串行化):該事物級別是最高的事物級別了。比前面幾種強大一點,可以解決以上問題,但是有缺點。誰先開啟事物,誰就有先執(zhí)行的權(quán)利,一個事物提交或回滾后另一個事物繼續(xù)。會造成性能問題,效率比較低,用的少。

      mysql默認的隔離級別是可重復(fù)讀,oracle默認的隔離級別是讀已提交。

數(shù)據(jù)庫連接池
  • 定義:數(shù)據(jù)庫連接對象的創(chuàng)建比較銷耗性能。一開始先在內(nèi)存中開辟一塊空間(集合),先往池子里放置多個連接對象,需要連接的話直接從池子里拿,不要自己去創(chuàng)建連接。使用完畢,要記得歸還連接,確保連接對象能夠循環(huán)利用。

  • 連接池使用步驟

    1. 創(chuàng)建10個連接
    2. 來的程序通過getConnection(list.remove(0))獲取連接
    3. 用完之后歸還連接(addBack()),使用裝飾者模式和動態(tài)代理完善這一步
    4. 擴容
  • 使用c3p0實現(xiàn)數(shù)據(jù)庫連接的配置

元數(shù)據(jù)
  • Meata data:描述數(shù)據(jù)的數(shù)據(jù),String sql,描述這份sql字符串的數(shù)據(jù)叫做元數(shù)據(jù)

    • 數(shù)據(jù)庫元數(shù)據(jù):DataBaseMetaData
    • 參數(shù)元數(shù)據(jù):ParameterMetaData
    • 結(jié)果集元數(shù)據(jù):ResultSetMetaData

MVC設(shè)計模式

  • javaBean+jsp ;在jsp中寫過多的java代碼,維護起來會比較困難

  • Servlet+javaBean+jsp(MVC):分層,邏輯比較清楚,便于維護,擴展方便。小型項目用起來麻煩,會有過多代碼。

    • M:model(模型層):封裝數(shù)據(jù)JavaBean,數(shù)據(jù)的處理
    • V:View(視圖層),JSP只專注于顯示
    • C:Controller(控制層),Servlet接收頁面的請求,找模型層處理,然后響應(yīng)數(shù)據(jù)出去。
  • 三層架構(gòu)

    • 客戶端:前臺應(yīng)用程序
    • web層:Servlet/JSP,包含控制層和視圖層
    • 業(yè)務(wù)邏輯層:Ejb會話層,JavaBean,歸屬于Model層
    • 數(shù)據(jù)訪問層:Dao,歸屬于Model層
  • 簡易型學(xué)生管理系統(tǒng)

    • 數(shù)據(jù)庫創(chuàng)建

                                         create database stus;
      

use stus;
CREATE TABLE stu (
sid int(11) NOT NULL AUTO_INCREMENT,
sname varchar(20) DEFAULT NULL,
gender varchar(5) DEFAULT NULL,
phone varchar(20) DEFAULT NULL,
birthday date DEFAULT NULL,
hobby varchar(50) DEFAULT NULL,
info varchar(200) DEFAULT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
* 創(chuàng)建web動態(tài)工程
* 導(dǎo)入需要的包
* c3p0-0.9.1.2.jar
* commons-dbutils-1.4.jar
* jstl.jar
* mysql-connector-java-5.1.7-bin.jar
* standard.jar
* 創(chuàng)建首頁
* 創(chuàng)建入口servlet
* 根據(jù)數(shù)據(jù)庫創(chuàng)建javaBean
* 創(chuàng)建c3p0.xml文件,建立數(shù)據(jù)庫連接
* 創(chuàng)建數(shù)據(jù)庫連接工具類
* 創(chuàng)建dao層從數(shù)據(jù)庫查詢數(shù)據(jù)
* 創(chuàng)建Service層
* Dao只針對單一的邏輯,數(shù)據(jù)操作層
* Service是業(yè)務(wù)層面
* 在Servlet層查詢出所有學(xué)生;把查到的數(shù)據(jù)存儲到作用域中,并跳轉(zhuǎn)頁面,在頁面展示作用域中的數(shù)據(jù)
* 使用EL和JSTL表達式配合在jsp頁面顯示數(shù)據(jù),在使用表達式時注意多空格會產(chǎn)生Property [sid] not found on type [java.lang.String]錯誤
* 頁面添加數(shù)據(jù)的時候,愛好有多個值,可以使用request.getParamterValues("")取得數(shù)組操作
* 分頁
* 物理分頁:來數(shù)據(jù)庫查詢的時候只查詢一頁就返回了
* 優(yōu)點:內(nèi)存中的數(shù)據(jù)量不會太大
* 缺點:對數(shù)據(jù)庫的訪問頻繁了一點
* 邏輯分頁:一口氣把所有的數(shù)據(jù)全部查出來,然后放置在內(nèi)存中
* 優(yōu)點:訪問速度快
* 缺點:數(shù)據(jù)庫量過大,內(nèi)存溢出

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,931評論 1 92
  • IOC 控制反轉(zhuǎn)容器控制程序?qū)ο笾g的關(guān)系,而不是傳統(tǒng)實現(xiàn)中,有程序代碼之間控制,又名依賴注入。All 類的創(chuàng)建,...
    irckwk1閱讀 1,092評論 0 0
  • 今日心情欠安。中午沒休息加班趕均衡的材料。高估了自己的工作效率和身體狀況,一日下來到傍晚五點左右人已經(jīng)廢了。所以心...
    羅西西閱讀 231評論 0 0
  • 1.上傳工具類的使用 2.實現(xiàn)單文件上傳 3. 實現(xiàn)多文件上傳 4.動態(tài)選擇多文件上傳 5.上傳文件要注意以下幾點...
    峰子1994閱讀 340評論 0 1
  • 第一次來這個城市,可能是20年前,第一個一個人來這個城市,可能是五年前。昨天晚上又是獨自一人,坐著熟悉的那趟車,來...
    崔小菲閱讀 1,562評論 0 0

友情鏈接更多精彩內(nèi)容