posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          jsp基礎(chǔ)速成精華(摘)

          Posted on 2009-05-20 23:10 Gavin.lee 閱讀(318) 評論(0)  編輯  收藏 所屬分類: jsp 【勿忘】

          摘自 csdn:http://blog.csdn.net/anysky130/archive/2008/04/04/2252055.aspx? anysky130的專欄

          Servlet三個要素:

          1.必須繼承自HttpServlet

          2.必須實現(xiàn)doGet()或者doPost()

          3.必須在web.xml中配置Servlet
          <servlet>
          <servlet-name> </servlet-name>
          <servlet-class> </servlet-class>
          </servlet>
          <servlet-mapping>
          <servlet-name> </servlet-name>
          <url-pattern> </url-pattern>
          </servelt-mapping>

          HttpServeltRrequest:請求對象
          getParameter():獲得表單元素的值
          getAttribute():獲得request范圍中的屬性值
          setAttribute():設(shè)置reqeust范圍中的屬性值
          setCharacterEncoding():設(shè)置字符編碼
          HttpSerletResponse:相應(yīng)對象
          sendRedirect():外部跳轉(zhuǎn)
          getWriter():獲得輸出流對象
          setContentType("text/html; charset=utf-8"):設(shè)置相應(yīng)內(nèi)容格式和編碼

          四種會話跟蹤方式:
          1.Session
          HttpSession session = request.getSession();
          session.setAttribute("name", "zhangsan");
          session.setAttribute("pwd", "aaa");
          String name = (String) session.getAttribute("name");
          2.cookie:
          //創(chuàng)建Cookie
          Cookie cookie = new Cookie("name", "zhangsan");
          //設(shè)置Cookie的超時時間
          cookie.setMaxAge(24 * 60 * 60 *60);
          //把Cookie發(fā)送到客戶端
          response.addCookie(cookie);
          //得到客戶端發(fā)送的Cookie
          Cookie [] cookies = request.getCookies();
          for(int i=0; i <cookies.length; i++) {
          ?? Cookie temp = cookies;
          ?? String key = temp.getName();
          ?? String value = temp.getValue();
          }
          3.隱藏表單域
          <input type="hidden" name="name" value="zhangsan" />
          request.getParameter("name");
          4.Url重寫
          問號傳參
          LoginServlet?username=zhangsan&pwd=123
          String name = request.getParameter("username");
          String pwd =request.getPareameter("pwd");
          內(nèi)部跳轉(zhuǎn):
          LoginServlet
          request.getRequestDispatcher("index.jsp").forward(request, resposne);
          外部跳轉(zhuǎn):
          response.sendRedirect("index.jsp");
          內(nèi)部跳轉(zhuǎn)是一次請求和一次響應(yīng)
          外部跳轉(zhuǎn)是兩次請求和兩次響應(yīng)
          ServletContext:Servlet上下文對象
          它是一個公共區(qū)域,可以被所有的客戶端共享
          setAttribute():向公共區(qū)域里放入數(shù)據(jù)
          getAttribute():從公共區(qū)域里取數(shù)據(jù)
          二:
          三:三個標(biāo)準(zhǔn)范圍:request, session, ServletContext
          ?? 共同點:都有setAttribute(), getAttribute()
          ?? 區(qū)別:范圍不同,request? < session? < servletContext
          四:四種會話跟蹤方式
          五:服務(wù)器上的五大對象
          ?? request, response, servlet, session, servletContext
          ??
          Jsp:Java Server Page
          頁面構(gòu)成:7種元素
          1.靜態(tài)內(nèi)容:html
          2.指令:page, include, taglib:
          <%@ 指令名 屬性1="屬性值1" 屬性2="屬性值2" %>
          3.表達(dá)式: <%=表達(dá)式 %>
          4.Scriptlet <% Java代碼 %>
          5.聲明: <%! %>:變量和方法
          6.動作: <jsp:動作名 屬性="屬性值"> </jsp:動作名>
          7.注釋:
          客戶端看不到的: <%-- --%>
          客戶端可以看到的: <!-- -->

          Jsp的執(zhí)行過程:
          1.轉(zhuǎn)譯:Jsp--->Servlet
          2.編譯:Servlet---->.class
          3.執(zhí)行:.class
          第一次訪問jsp的時候響應(yīng)速度較慢,后面請求時響應(yīng)速度快
          腳本:
          表達(dá)式: <%= %>
          Scriptlet: <% %>
          聲明: <%! %>
          指令:
          page:language, import, errorPage, isErrorpage
          include:file
          taglib:uri:指定標(biāo)簽庫描述符的路徑 prefix:指定標(biāo)簽的前綴
          隱式對象:
          分類:
          1.輸入和輸出對象:request(HttpServletRequest),
          ???????????????? response(HttpServletResponse),
          ???????????????? out(JspWriter), servlet中的out是PrintWriter
          2.作用域通信對象:pageContext, request,
          ???????????????? session(HttpSession),
          ???????????????? application(ServletContext)
          3.Servlet對象:page(this), config
          4.錯誤對象:exception
          ???
          JavaBean:
          一個標(biāo)準(zhǔn)的JavaBean有三個條件
          1.共有的類
          2.具有不帶參數(shù)的公共的構(gòu)造方法
          3.具有set()和get()方法
          4.私有屬性
          Jsp中的標(biāo)準(zhǔn)動作:
          1.useBean:創(chuàng)建JavaBean的一個實例
          <jsp:useBean id="stu" class="com.westaccp.test.Student" scope="page/session/application/request" />
          2.setProperty:給JavaBean的屬性賦值
          <jsp:setProperty name="stu" property="stuName" value="zhangsan" />
          <jsp:setProperty name="stu" property="stuName" param="txtName" />
          value和param不能同時使用
          偷懶的方法: <jsp:setProperty name="stu" property="*" />
          這個時候需要注意的是,表單元素的名字必須和JavaBean的屬性值
          一模一樣
          3.getProperty:獲得JvaBean的屬性值
          <jsp:getProperty name="stu" property="stuName" />
          4.forward:內(nèi)部跳轉(zhuǎn),相當(dāng)于request.getRequestDispatcher().forward(request, response);
          <jsp:forward page="index.jsp" />
          5.include:包含
          <jsp:include page="header.jsp" flush="true" />
          表達(dá)式語言:
          EL: Expression Language
          語法格式: ${表達(dá)式 }
          表示式 = 運算符 + 操作數(shù)
          運算符:跟Java比較,多了一個empty, 少了一個賦值運算符
          ${empty ""} : true
          ${empty null} :true
          操作數(shù):
          -->常量:布爾型(true/false), 整型, 浮點型, 字符串(可以用'', 還可以用""), Null
          -->變量:
          ??? 1.指的是放在四個標(biāo)準(zhǔn)范圍里的屬性(page, request, session, application)
          ??? 2.在編準(zhǔn)范圍內(nèi)的搜索順序:page-->request--->session--->application
          ??? 3.怎么取得變量值:點運算符., 還以用[]
          ???? <%
          ????? request.setAttribute("name", "lisi");
          ??? %>
          ??? ${requestScope.name}
          ??? 或者
          ??? ${requestScope["name"]}
          -->隱式對象
          ??? 1.pageContext:通過它可以訪問request, session, servletContext
          ??? 2.跟范圍由關(guān)的:pageScope, requestScope, sessionScope, applicationScope
          ??? 3.跟輸入有關(guān)的:param, paramValues
          ??? 4.其他的:header, cookie, headervalues,
          EL表達(dá)式適用的場合:
          1.可以在靜態(tài)文本中使用
          2.與自定義標(biāo)簽結(jié)合使用
          3.和JavaBean結(jié)合使用
          <jsp:userBean id="stu" class="com.westaccp.test.Student" scope="session" />
          <jsp:setProperty name="stu" property="stuName" value="hello" />
          ${stu.stuName}
          自定義標(biāo)簽:
          1.標(biāo)簽處理程序?qū)崿F(xiàn)
          --->實現(xiàn):繼承自BodyTagSupport或者TagSupport
          ????????? 一般會重寫doStartTag(), doEndTag(), doAfterBody()
          --->描述:在標(biāo)簽庫描述符文件中描述(.tld)
          ???? <taglib>
          ??????? <tlib-version>1.0 </tlib-version>
          ??????? <jsp-version>2.0 </jsp-version>
          ??????? <short-name>simpletag </short-name>
          ???
          ??????? <tag>
          ???????? <name>showbody </name>
          ???????? <tag-class>com.westaccp.test.ShowBodyTag </tag-class>
          ???????? <body-content>empty/jsp </body-content>
          ???????? <attribute>
          ????????? <name>color </name>
          ???????? </attribute>
          ??????? </tag>
          ???? </taglib>
          --->使用: <%@ taglib uri="WEB-INF/mytag.tld" prefix="my" %>
          ?????????? <my:showbody />
          2.標(biāo)簽文件
          --->實現(xiàn)和描述
          ???? 在.tag文件中實現(xiàn)
          ???? 設(shè)置主體內(nèi)容: <%@ body-content="empty/scriptless" %>
          ???? 設(shè)置屬性: <%@ attribute name="name" required="true" rtexprvalue="true" %>
          ???? 有主體內(nèi)容: <jsp:doBody scope="session" var="theBody" />
          ????? <%
          ??????? String body = (String) session.getAttribute("theBody");
          ???? %>
          --->使用
          ???? WEB-INF/tags/sayhello.tag
          ????? <%@ taglib tagdir="/WEB-INF/tags/" prefix="you" %>
          ????? <you:sayhello />
          ????
          標(biāo)準(zhǔn)標(biāo)簽庫:
          1.核心標(biāo)簽庫
          -->通用:
          ???? set: <c:set var="" value="" scope="" />
          ???? out: <c:out value="" />
          ???? remove:? <c:remove var="" scope="" />
          -->條件:
          ???? if: <c:if test="">..... </c:if>
          ???? choose:? <c:choose>
          ???????????? <c:when test="">... </c:when>
          ???????????? <c:when test="">... </c:when>
          ???????????? <c:when test="">... </c:when>
          ??????????????? .....
          ???????????????? <c:otherwise>... </otherwise>??????????
          ????????????? </c:choose>
          -->迭代:
          ??? forEach: <forEach var="" items="" varStatus="" begin="" end="">
          ??? foTokens: <foTodens var="" items="" delim=",; |"> </foTodens>
          ??? Java,C#;SQL &brvbarC
          2.I18N與格式化標(biāo)簽庫
          -->setLocale:設(shè)置本地區(qū)域
          -->bundle:設(shè)置資源包
          -->setBundle:設(shè)置資源包
          -->message:輸出消息
          3.SQL標(biāo)簽庫
          -->setDataSource:設(shè)置數(shù)據(jù)源,用于獲得與數(shù)據(jù)庫的連接
          -->query:執(zhí)行查詢
          -->update:執(zhí)行增,刪,改
          -->transaction:事務(wù)
          -->param:參數(shù)
          4.XML標(biāo)簽庫
          過濾器:
          生命周期:
          1.實例華:
          2.初始化:init()
          3.過濾:doFilter()
          4.銷毀:destroy()
          5.不可用
          配置:
          <filter>
          <filter-name> </filter-name>
          <filter-class> </filter-class>
          </filter>
          <filter-mapping>
          <filter-name> </filter-name>
          <url-pattern> </url-pattern>
          </filter-mapping>
          幾個重要的接口:
          1.Filter:init(), doFilter(), destroy()
          2.FilterChain: doFilter(request, response)
          3.FilterConfig:getFilterName(), getInitParameter(),
          過濾器鏈:--->1--->2--->3--->Servlet 請求
          ???????? <----1 <---2 <---3 <---??????? 響應(yīng)
          ???????
          MvC設(shè)計模式
          1.ModelI:jsp+JavaBean
          2.ModelII:jsp+Servlet+JavaBean
          ????????? jsp---view
          ????????? servlet---control
          ????????? javabean---model
          MVC:
          M--Model:模型:訪問后臺數(shù)據(jù)庫
          V--view:視圖:展示
          C--control:控制器:控制程序流程
          ModelII和MVC的關(guān)系:
          MVC是一種設(shè)計模式,ModelII它是MVC的一種具體的實現(xiàn)?

          主站蜘蛛池模板: 开平市| 鄯善县| 呼伦贝尔市| 淮南市| 锦州市| 剑川县| 东阿县| 碌曲县| 凤台县| 五河县| 隆安县| 广德县| 平陆县| 于都县| 凤冈县| 日土县| 台东县| 博客| 张掖市| 普兰店市| 四子王旗| 平谷区| 新和县| 临高县| 滕州市| 乐清市| 台中市| 蒙阴县| 教育| 萍乡市| 磐石市| 新兴县| 乾安县| 新余市| 花垣县| 北京市| 光山县| 华蓥市| 固安县| 奈曼旗| 榆树市|