日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSP入门实战下

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

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

全部代碼下載:鏈接

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

1.1簡要介紹:

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

1.2JSTL庫安裝:

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

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

    核心標(biāo)簽是最常用的JSTL標(biāo)簽。現(xiàn)在基本上我們也之使用功能核心標(biāo)簽庫,此去只介紹核心標(biāo)簽,對于其他的標(biāo)簽用法類似。
  • 引用核心標(biāo)簽庫的語法如下:
  • <%--導(dǎo)入核心標(biāo)簽庫 --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"
  • 核心標(biāo)簽庫的介紹:
  • 演示如下:詳細(xì)見注釋
  • <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"import="java.util.*,com.rlovep.entity.Student"%> <%--導(dǎo)入核心標(biāo)簽庫 --%> <%@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)簽庫 --%> <%--set標(biāo)簽:保存數(shù)據(jù)到域中,默認(rèn)保存到pag域中var:屬性名value:屬性價值,可以是對象scope:范圍--%> <c:set var="name" value="rose" scope="page"></c:set> <%--out標(biāo)簽: 類似輸出表達(dá)式:<%= %>value:顯示的內(nèi)容;default:value為空時顯示的內(nèi)容escapexml:是否對<等實體符號轉(zhuǎn)義; --%> <%--el表達(dá)式輸出,調(diào)用屬性 --%> <c:out value="${name }" default="<h3>標(biāo)題3</h3>" escapeXml="true"></c:out> <%--默認(rèn)值測試,以及輸出特殊字符 --%> <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)簽:可以用來取得發(fā)生錯誤時的信息,同時可以進(jìn)行適當(dāng)處理.相當(dāng)于try catchvar:保存錯誤信息的exception--%><c:catch var="e"><%int a=0,b=10;b=b/a;%></c:catch><%--輸出錯誤信息 --%><c:out value="${e }"/> <%-- <%int a=0,b=10;b=b/a;%> --%><hr/><%--<c:url>標(biāo)簽將URL格式化為一個字符串,然后存儲在一個變量中var:變量名。value:urlcontext:本地的另一個工程庫--%><%--c:param 在重定向時當(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:包含的頁面--%><c:import url="/common/header1.jsp" ><c:param name="name" value="sisi"/></c:import><hr/><%--c:redirect 標(biāo)簽 可以是絕對地址 url:地址context:另外一個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="" : 從哪個元素開始遍歷,從0開始.默認(rèn)從0開始end="": 到哪個元素結(jié)束。默認(rèn)到最后一個元素step="" : 步長 (每次加幾) ,默認(rèn)1items="": 需要遍歷的數(shù)據(jù)(集合) var="": 每個元素的名稱 varStatus="": 當(dāng)前正在遍歷元素的狀態(tài)對象。(count屬性:當(dāng)前位置,從1開始,last屬性:最后一個)--%><c:forEach items="${list}" var="student" varStatus="varSta">序號: {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}"/>個元素</c:if></c:forTokens> </body> </html>

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

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

    2.1EL語法:${exper}

  • 輸出基本數(shù)據(jù)類型變量:
    不注明域的范圍時,從四個域中獲取:順序為pageScoep / requestScope / sessionScope / applicationScope
    ${name}<%--從指定域中獲取name變量的值--%>
    指定域獲取:
    ${pageScope.name} <%--等價于getAttribute()方法;--%>
  • 輸出對象的屬性值
    ${student.name} 等價于 (點相對于調(diào)用getXX()方法)
  • 使用EL獲取集合對象
  • {list[0].age } <%-- list[0]等價于 (中括號相對于調(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高級用法自定義函數(shù):

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

  • 自定義函數(shù)使用語法:
    ${rlovep:reverse(student.name)}<%--調(diào)用reverse方法使傳入的student.name反轉(zhuǎn)--%>
  • 開發(fā)步驟:
    1.在src建立開發(fā)處理類,這個類包含若干個靜態(tài)方法。當(dāng)然這個步驟可以省掉使用jdk庫的類也是可以的
    2.使用標(biāo)簽庫定義函數(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)簽庫文件,增加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)簽庫的版本 --><tlib-version>1.0</tlib-version><!-- 標(biāo)簽庫前綴 --><short-name>rlovep</short-name><!-- tld文件的唯一標(biāo)記 --><uri>http://rlovep.com</uri><!-- 定義第一個方法 --><function><!-- 定義方法名 --><name>reverse</name><!-- 定義方法的處理類 --><function-class>com.rlovep.elmethod.MyFuns</function-class><!-- 定義函數(shù)的實現(xiàn)方法:包括返回值和函數(shù)名以及參數(shù) --><function-signature>java.lang.String reverse(java.lang.String)</function-signature></function><!-- 定義第二個方法 --><function><!-- 定義方法名 --><name>count</name><!-- 定義方法的處理類 --><function-class>com.rlovep.elmethod.MyFuns</function-class><!-- 定義函數(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)置對象 --%><%pageContext.setAttribute("name", "peace");pageContext.setAttribute("age", "22", pageContext.APPLICATION_SCOPE);%><%--直接從域中搜索獲得屬性 --%>El表達(dá)式:${name }<hr/><%--等價于 --%>表達(dá)式:<%=pageContext.findAttribute("name") %><hr/><%--從指定域中獲取屬性 --%>EL表達(dá)式:${applicationScope.age}<hr/><%--等價于 --%><%=pageContext.getAttribute("age", pageContext.APPLICATION_SCOPE) %><hr/><%--獲取請求參數(shù) --%>請求參數(shù)${param.pass}<hr/> <%--請求頭獲取 --%> 請求頭${header.Host} <%--還可以獲得初始參數(shù):initparam 以及cookie --%> <hr/> <%--el輸出對象的屬性 ,必須將對象放入域中--%> <%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獲取對象值 --%> {student.age }<%--${student.name} 等價于 (點相對于調(diào)用getXX()方法)<%=((Student)pageContext.findAttribute("student")).getName()%>--%><hr/><%--使用EL獲取List對象 --%> {list[0].age }<br/>{list[1].age }<br/> {list[2].age }<%--list[0]等價于 (中括號相對于調(diào)用get(參數(shù))方法)((List)pageContext.findAttribute("list")).get(0)--%><hr/><%--使用EL獲取Map對象 --%> {map['100'].age }<br/> {map['101'].age }<br/> {map['102'].age }<br/><%--el還可以執(zhí)行算法表達(dá)式 --%> <%--el表達(dá)語言的自定義函數(shù) 本質(zhì)是為了調(diào)用提供一種方法允許el中調(diào)用某類的靜態(tài)方法: 1.在src建立開發(fā)處理類,這個類包含若干個靜態(tài)方法。當(dāng)然這個步驟可以省掉使用jdk庫的類也是可以的 2.使用標(biāo)簽庫定義函數(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字符個數(shù):${rlovep:count(student.name)} </body> </html>

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

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

    總結(jié)

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

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