日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

JSP入门实战下

發(fā)布時(shí)間:2025/3/20 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSP入门实战下 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一部分簡(jiǎn)單講解:jsp語(yǔ)法的規(guī)范,以及三大編譯指令,七個(gè)動(dòng)作指令和九大內(nèi)置對(duì)象,生命周期講解等。這章主要講解el表達(dá)式,核心標(biāo)簽庫(kù)。

全部代碼下載:鏈接

1.核心標(biāo)簽庫(kù)(JSTL:c)講解:

1.1簡(jiǎn)要介紹:

JSTL全名JspServer Pages Standdard Tag Library(Jsp標(biāo)準(zhǔn)標(biāo)簽庫(kù)),它是sun公司發(fā)布的一個(gè)針對(duì)JSP開發(fā)的新組件,它允許使用標(biāo)簽開發(fā)Jsp頁(yè)面.JSTL支持通用的、結(jié)構(gòu)化的任務(wù),比如迭代,條件判斷,XML文檔操作,國(guó)際化標(biāo)簽,SQL標(biāo)簽。 除了這些,它還提供了一個(gè)框架來(lái)使用集成JSTL的自定義標(biāo)簽。
JSTL所提供的標(biāo)簽庫(kù)主要分為五大類:

1.2JSTL庫(kù)安裝:

  • 從Apache的標(biāo)準(zhǔn)標(biāo)簽庫(kù)中下載的二進(jìn)包(jakarta-taglibs-standard-current.zip)。下載地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
  • 將下載的壓縮包解壓,將lib下的兩個(gè)jar文件:standard.jar和jstl.jar文件拷貝到Tomcat下lib/目錄下。
  • 現(xiàn)在就可以通過(guò)在頭部包含標(biāo)簽使用JSTL了

    1.3核心標(biāo)簽庫(kù)的使用:

    核心標(biāo)簽是最常用的JSTL標(biāo)簽。現(xiàn)在基本上我們也之使用功能核心標(biāo)簽庫(kù),此去只介紹核心標(biāo)簽,對(duì)于其他的標(biāo)簽用法類似。
  • 引用核心標(biāo)簽庫(kù)的語(yǔ)法如下:
  • <%--導(dǎo)入核心標(biāo)簽庫(kù) --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"
  • 核心標(biāo)簽庫(kù)的介紹:
  • 演示如下:詳細(xì)見注釋
  • <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"import="java.util.*,com.rlovep.entity.Student"%> <%--導(dǎo)入核心標(biāo)簽庫(kù) --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>核心標(biāo)簽jiangjie</title> </head> <body> <%--使用標(biāo)簽庫(kù) --%> <%--set標(biāo)簽:保存數(shù)據(jù)到域中,默認(rèn)保存到pag域中var:屬性名value:屬性價(jià)值,可以是對(duì)象scope:范圍--%> <c:set var="name" value="rose" scope="page"></c:set> <%--out標(biāo)簽: 類似輸出表達(dá)式:<%= %>value:顯示的內(nèi)容;default:value為空時(shí)顯示的內(nèi)容escapexml:是否對(duì)<等實(shí)體符號(hào)轉(zhuǎn)義; --%> <%--el表達(dá)式輸出,調(diào)用屬性 --%> <c:out value="${name }" default="<h3>標(biāo)題3</h3>" escapeXml="true"></c:out> <%--默認(rèn)值測(cè)試,以及輸出特殊字符 --%> <c:out value="${peace }" default="<h3>標(biāo)題</h3>" escapeXml="true"></c:out> <hr/> <%--remove標(biāo)簽:刪除數(shù)據(jù),默認(rèn)刪除到pag域中var:屬性名scope:范圍--%> <c:remove var="name" scope="page"/> <c:out value="${name }" default="刪除name之后" escapeXml="true"></c:out> <hr/> <%--catch標(biāo)簽:可以用來(lái)取得發(fā)生錯(cuò)誤時(shí)的信息,同時(shí)可以進(jìn)行適當(dāng)處理.相當(dāng)于try catchvar:保存錯(cuò)誤信息的exception--%><c:catch var="e"><%int a=0,b=10;b=b/a;%></c:catch><%--輸出錯(cuò)誤信息 --%><c:out value="${e }"/> <%-- <%int a=0,b=10;b=b/a;%> --%><hr/><%--<c:url>標(biāo)簽將URL格式化為一個(gè)字符串,然后存儲(chǔ)在一個(gè)變量中var:變量名。value:urlcontext:本地的另一個(gè)工程庫(kù)--%><%--c:param 在重定向時(shí)當(dāng)參數(shù)用 --%><c:url var="url" value="el.jsp"><c:param name="pass" value="peace"/></c:url><a href="${url }">url重定向</a><c:url var="baidu" value="http://wwww.baidu.com"/><a href="${baidu }">百度</a> <hr/> <%--<c:import>標(biāo)簽:功能類似于<jsp:import>,但是功能更加強(qiáng)大。可以導(dǎo)入外部jsp文件,和保存到輸入流中var:輸出保存到stringvarReader:輸出保存到輸入字符流url:包含的頁(yè)面--%><c:import url="/common/header1.jsp" ><c:param name="name" value="sisi"/></c:import><hr/><%--c:redirect 標(biāo)簽 可以是絕對(duì)地址 url:地址context:另外一個(gè)jsp容器--%> <%-- <c:redirect url="el.jsp"><c:param name="pass" value="wang"></c:param></c:redirect> --%><%Integer score=new Integer(60);pageContext.setAttribute("score", score);%><%--if標(biāo)簽 :單條件判斷test:判斷是否為true執(zhí)行--%><c:if test="${!empty score}">條件成立</c:if><hr/><%--choose標(biāo)簽+when標(biāo)簽+otherwirse標(biāo)簽: 多條件判斷 --%><c:set var="score" value="56"></c:set><c:choose><c:when test="${score>=90 && score<=100}">優(yōu)秀</c:when><c:when test="${score>=80 && score<90}">良好</c:when><c:when test="${score>=70 && score<80}">一般</c:when><c:when test="${score>=60 && score<70}">及格</c:when><c:otherwise>不及格</c:otherwise></c:choose><%-- forEach標(biāo)簽:循環(huán) --%><%//ListList<Student> list = new ArrayList<Student>();list.add(new Student("rose",18));list.add(new Student("jack",28));list.add(new Student("lucy",38));//放入域中pageContext.setAttribute("list",list);//MapMap<String,Student> map = new HashMap<String,Student>();map.put("100",new Student("mark",20));map.put("101",new Student("maxwell",30));map.put("102",new Student("narci",40));//放入域中pageContext.setAttribute("map",map);%><hr/><%--begin="" : 從哪個(gè)元素開始遍歷,從0開始.默認(rèn)從0開始end="": 到哪個(gè)元素結(jié)束。默認(rèn)到最后一個(gè)元素step="" : 步長(zhǎng) (每次加幾) ,默認(rèn)1items="": 需要遍歷的數(shù)據(jù)(集合) var="": 每個(gè)元素的名稱 varStatus="": 當(dāng)前正在遍歷元素的狀態(tài)對(duì)象。(count屬性:當(dāng)前位置,從1開始,last屬性:最后一個(gè))--%><c:forEach items="${list}" var="student" varStatus="varSta">序號(hào): {student.name } - 年齡:${student.age}<br/></c:forEach><hr/><c:forEach items="${map}" var="entry"> {entry.value.name } - 年齡:${entry.value.age }<br/></c:forEach><hr/><%-- forToken標(biāo)簽: 循環(huán)特殊字符串 --%><%String str = "java-php-net-平面";pageContext.setAttribute("str",str);%><c:forTokens items="${str}" delims="-" var="s" varStatus="status">${s }<br/><c:if test="${status.last }"><c:out value="輸出:${status.count}"/>個(gè)元素</c:if></c:forTokens> </body> </html>

    2.EL表達(dá)式語(yǔ)言:

    E L(Expression Language) 目的:為了使JSP寫起來(lái)更加簡(jiǎn)單。 EL 提供了在 JSP 腳本編制元素范圍外使用運(yùn)行時(shí)表達(dá)式的功能。
    EL既可以用來(lái)創(chuàng)建算術(shù)表達(dá)式也可以用來(lái)創(chuàng)建邏輯表達(dá)式。在JSP EL表達(dá)式內(nèi)可以使用整型數(shù),浮點(diǎn)數(shù),字符串,常量true、false,還有null。
    EL使得訪問(wèn)存儲(chǔ)在JavaBean中的數(shù)據(jù)變得非常簡(jiǎn)單,EL可以訪問(wèn)內(nèi)置對(duì)象,以及放置在對(duì)象中的屬性;
    EL表達(dá)式作用: 向?yàn)g覽器輸出域?qū)ο笾械淖兞恐祷虮磉_(dá)式計(jì)算的結(jié)果!!!

    2.1EL語(yǔ)法:${exper}

  • 輸出基本數(shù)據(jù)類型變量:
    不注明域的范圍時(shí),從四個(gè)域中獲取:順序?yàn)閜ageScoep / requestScope / sessionScope / applicationScope
    ${name}<%--從指定域中獲取name變量的值--%>
    指定域獲取:
    ${pageScope.name} <%--等價(jià)于getAttribute()方法;--%>
  • 輸出對(duì)象的屬性值
    ${student.name} 等價(jià)于 (點(diǎn)相對(duì)于調(diào)用getXX()方法)
  • 使用EL獲取集合對(duì)象
  • {list[0].age } <%-- list[0]等價(jià)于 (中括號(hào)相對(duì)于調(diào)用get(參數(shù))方法) ((List)pageContext.findAttribute("list")).get(0)--%>

  • el還可以執(zhí)行算法表達(dá)式
    EL表達(dá)式支持大部分Java所提供的算術(shù)和邏輯操作符:

    演示如下:
  • 比較運(yùn)算${10>5 }<br/>${10<5 }<br/>${10!=10 }<hr/> 邏輯運(yùn)算${true && false }<br/>${true || false }<br/>${!false }<br/> 判空null 或 空字符串: empty<%//String name = "eric";//String name = null;String name = "";pageContext.setAttribute("name",name);%>判斷null: ${name==null }<br/>判斷空字符: ${name=="" }<br/>判空: ${name==null || name=="" } 另一種判空寫法: ${empty name }

    2.2EL高級(jí)用法自定義函數(shù):

    el表達(dá)語(yǔ)言的自定義函數(shù) 本質(zhì)是為了調(diào)用提供一種方法允許el中調(diào)用某類的靜態(tài)方法:

  • 自定義函數(shù)使用語(yǔ)法:
    ${rlovep:reverse(student.name)}<%--調(diào)用reverse方法使傳入的student.name反轉(zhuǎn)--%>
  • 開發(fā)步驟:
    1.在src建立開發(fā)處理類,這個(gè)類包含若干個(gè)靜態(tài)方法。當(dāng)然這個(gè)步驟可以省掉使用jdk庫(kù)的類也是可以的
    2.使用標(biāo)簽庫(kù)定義函數(shù):定義函數(shù)的方式與定義標(biāo)簽的方式相似。增加function標(biāo)簽就行;
    3.使用:增加taglib指令
  • 演示如下
    建立開發(fā)處理類: MyFuns.java
  • public static String reverse(String str){return new StringBuffer(str).reverse().toString();}public static int count(String str){return str.length();}

    在webcontent目錄下建立:mytaglib.tld標(biāo)簽庫(kù)文件,增加function標(biāo)簽

    <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"><description>A tag library exercising SimpleTag handlers.</description><!-- 標(biāo)簽庫(kù)的版本 --><tlib-version>1.0</tlib-version><!-- 標(biāo)簽庫(kù)前綴 --><short-name>rlovep</short-name><!-- tld文件的唯一標(biāo)記 --><uri>http://rlovep.com</uri><!-- 定義第一個(gè)方法 --><function><!-- 定義方法名 --><name>reverse</name><!-- 定義方法的處理類 --><function-class>com.rlovep.elmethod.MyFuns</function-class><!-- 定義函數(shù)的實(shí)現(xiàn)方法:包括返回值和函數(shù)名以及參數(shù) --><function-signature>java.lang.String reverse(java.lang.String)</function-signature></function><!-- 定義第二個(gè)方法 --><function><!-- 定義方法名 --><name>count</name><!-- 定義方法的處理類 --><function-class>com.rlovep.elmethod.MyFuns</function-class><!-- 定義函數(shù)的實(shí)現(xiàn)方法:包括返回值和函數(shù)名以及參數(shù) --><function-signature>int count(java.lang.String)</function-signature></function></taglib>

    增加taglib指令
    <%@taglib prefix="rlovep" uri="http://rlovep.com" %>

    2.3整體演示如下:

    <%@page import="java.util.HashMap,java.util.Map"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="com.rlovep.entity.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!-- 定義標(biāo)簽 --> <%@taglib prefix="rlovep" uri="http://rlovep.com" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>el表達(dá)式學(xué)習(xí):</title> </head> <body> <%--el的內(nèi)置對(duì)象 --%><%pageContext.setAttribute("name", "peace");pageContext.setAttribute("age", "22", pageContext.APPLICATION_SCOPE);%><%--直接從域中搜索獲得屬性 --%>El表達(dá)式:${name }<hr/><%--等價(jià)于 --%>表達(dá)式:<%=pageContext.findAttribute("name") %><hr/><%--從指定域中獲取屬性 --%>EL表達(dá)式:${applicationScope.age}<hr/><%--等價(jià)于 --%><%=pageContext.getAttribute("age", pageContext.APPLICATION_SCOPE) %><hr/><%--獲取請(qǐng)求參數(shù) --%>請(qǐng)求參數(shù)${param.pass}<hr/> <%--請(qǐng)求頭獲取 --%> 請(qǐng)求頭${header.Host} <%--還可以獲得初始參數(shù):initparam 以及cookie --%> <hr/> <%--el輸出對(duì)象的屬性 ,必須將對(duì)象放入域中--%> <%Student student=new Student("peace",22);String a="123";//放入域中pageContext.setAttribute("student", student);//放入list中List<Student> list=new ArrayList<Student>();list.add(new Student("sisi",22));list.add(new Student("nick",20));list.add(new Student("lucy",38));pageContext.setAttribute("list", list);//放入map中Map<String,Student> map=new HashMap<String,Student>();map.put("100",new Student("mark",20));map.put("101",new Student("maxwell",30));map.put("102",new Student("narci",40));//放入域中pageContext.setAttribute("map",map); %> <%--使用el獲取對(duì)象值 --%> {student.age }<%--${student.name} 等價(jià)于 (點(diǎn)相對(duì)于調(diào)用getXX()方法)<%=((Student)pageContext.findAttribute("student")).getName()%>--%><hr/><%--使用EL獲取List對(duì)象 --%> {list[0].age }<br/>{list[1].age }<br/> {list[2].age }<%--list[0]等價(jià)于 (中括號(hào)相對(duì)于調(diào)用get(參數(shù))方法)((List)pageContext.findAttribute("list")).get(0)--%><hr/><%--使用EL獲取Map對(duì)象 --%> {map['100'].age }<br/> {map['101'].age }<br/> {map['102'].age }<br/><%--el還可以執(zhí)行算法表達(dá)式 --%> <%--el表達(dá)語(yǔ)言的自定義函數(shù) 本質(zhì)是為了調(diào)用提供一種方法允許el中調(diào)用某類的靜態(tài)方法: 1.在src建立開發(fā)處理類,這個(gè)類包含若干個(gè)靜態(tài)方法。當(dāng)然這個(gè)步驟可以省掉使用jdk庫(kù)的類也是可以的 2.使用標(biāo)簽庫(kù)定義函數(shù):定義函數(shù)的方式與定義標(biāo)簽的方式相似。增加function標(biāo)簽就行; 3.增加taglib指令 --%> 此去表達(dá)式調(diào)用函數(shù):<br/> peace倒轉(zhuǎn):${rlovep:reverse(student.name)}<%--調(diào)用reverse方法使傳入的student.name反轉(zhuǎn)--%> <br/> peace字符個(gè)數(shù):${rlovep:count(student.name)} </body> </html>

    來(lái)自一條小鯊魚wpeace(rlovep.com)

    轉(zhuǎn)載于:https://www.cnblogs.com/onepeace/p/5043477.html

    總結(jié)

    以上是生活随笔為你收集整理的JSP入门实战下的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。