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

歡迎訪問 生活随笔!

生活随笔

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

javascript

JSP标签和JSTL标签注意点

發(fā)布時(shí)間:2025/3/21 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSP标签和JSTL标签注意点 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、轉(zhuǎn)發(fā)和重定向問題

  當(dāng)前項(xiàng)目:/Test

  轉(zhuǎn)發(fā)路徑:"/"根目錄表示當(dāng)前項(xiàng)目"/Test","/login.jsp"就是"/Test/login.jsp"。所以只能在web程序內(nèi)部跳轉(zhuǎn)

  重定向:"/"根目錄代表當(dāng)前web容器,"/login.jsp"就是"/login.jsp",不符合要求,需要使用"/Test/login.jsp"。所以可以實(shí)現(xiàn)外部程序跳轉(zhuǎn)

轉(zhuǎn)發(fā):request.getRequestDispacther("/login.jsp").forward(req, res); 重定向:response.sendRedirect(request.getContextPath() + "/login.jsp");

2、servlet的response.sentRedirect(String url)和jstl標(biāo)簽的<c:redirect url="">[<jsp:param />......]</c:redirect>比較

  response的重定向方法"/"是相對(duì)于web容器來說的,

  但是jstl的c標(biāo)簽的重定向是相當(dāng)于c標(biāo)簽的context屬性來說的,而c標(biāo)簽的context屬性默認(rèn)值就是${pageContext.request.contextPath }。

<%-- <%if (request.getAttribute("username") == null) {response.sendRedirect(request.getContextPath() + "/login.jsp");}%> --%><c:if test="${empty username }"><c:redirect url="/login.jsp"></c:redirect></c:if> 等價(jià)于: <c:redirect context="${pageContext.request.contextPath }" url="/login.jsp"></c:redirect>

3、jsp中獲取param請(qǐng)求參數(shù)

  servlet中:request.getParameter(String pname) String

  jsp中:使用jstl隱式對(duì)象(看我前面的博客中jstl11個(gè)隱式對(duì)象)

    

<%request.setCharacterEncoding("UTF-8"); //設(shè)置編碼,防止中文亂碼%>請(qǐng)求參數(shù):${param.username } --%>

?

附錄:jsp和jstl標(biāo)簽詳解.

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL語法</title> </head><body><%String name = "rose"; //放入域中//pageContext.setAttribute("name",name);pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE); %><%=name %><br/><%--1)從四個(gè)域自動(dòng)搜索--%>EL表達(dá)式: ${name }<%--${name } 等價(jià)于<%=pageContext.findAttribute("name")%>--%><%--2) 從指定的域中獲取數(shù)據(jù)--%>EL表達(dá)式: ${pageScope.name }<%--${pageScope.name } 等價(jià)于<%= pageContext.getAttribute("name",PageContext.PAGE_SCOPE)%>--%></body> </html> <%@ page language="java" import="java.util.*,gz.itcast.b_entity.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL輸出不同類型的數(shù)據(jù)</title> </head><body><%--1)EL輸出對(duì)象的屬性 --%><%//保存數(shù)據(jù)Student student = new Student("eric",20);//放入域中pageContext.setAttribute("student",student);//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);%><%--使用EL獲取對(duì)象 --%>${student.name} - ${student.age }<%--${student.name} 等價(jià)于 (點(diǎn)相對(duì)于調(diào)用getXX()方法)<%=((Student)pageContext.findAttribute("student")).getName()%>--%><hr/><%--使用EL獲取List對(duì)象 --%>${list[0].name } - ${list[0].age }<br/>${list[1].name } - ${list[1].age }<br/>${list[2].name } - ${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'].name } - ${map['100'].age }<br/>${map['101'].name } - ${map['101'].age }<br/>${map['102'].name } - ${map['102'].age }<br/></body> </html> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>EL表達(dá)式計(jì)算</title> </head><body><%--1)算術(shù)表達(dá)式+ - * /--%>${10+5 }<br/>${10*5 }<hr/><%--2)比較運(yùn)算> < >= <= == !=--%>${10>5 }<br/>${10<5 }<br/>${10!=10 }<hr/><%--3)邏輯運(yùn)算&& || !--%>${true && false }<br/>${true || false }<br/>${!false }<br/><hr/><%--4)判空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 }</body> </html> <%@ page language="java" import="java.util.*,gz.itcast.b_entity.*" pageEncoding="utf-8"%> <%--導(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"> <html><head> <title>核心標(biāo)簽庫</title> </head><body><%--使用標(biāo)簽 --%><%--set標(biāo)簽 :保存數(shù)據(jù)(保存到域中)默認(rèn)保存到page域 --%><c:set var="name" value="rose" scope="request"></c:set><%String msg = null;pageContext.setAttribute("msg",msg);%>${msg }<br/><%--out標(biāo)簽: 獲取數(shù)據(jù)(從域中) default: 當(dāng)value值為null時(shí),使用默認(rèn)值escapeXml: 是否對(duì)value值進(jìn)行轉(zhuǎn)義,false,不轉(zhuǎn)義,true,轉(zhuǎn)義(默認(rèn))--%><c:out value="${msg}" default="<h3>標(biāo)題3</h3>" escapeXml="true"></c:out><hr/><%--if標(biāo)簽 :單條件判斷--%><c:if test="${!empty msg}">條件成立</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="" : 步長 (每次加幾) ,默認(rèn)1items="": 需要遍歷的數(shù)據(jù)(集合) var="": 每個(gè)元素的名稱 varStatus="": 當(dāng)前正在遍歷元素的狀態(tài)對(duì)象。(count屬性:當(dāng)前位置,從1開始)--%><c:forEach items="${list}" var="student" varStatus="varSta">序號(hào):${varSta.count} - 姓名:${student.name } - 年齡:${student.age}<br/></c:forEach><hr/><c:forEach items="${map}" var="entry">${entry.key } - 姓名: ${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">${s }<br/></c:forTokens><%--redrict:重定向 --%><c:redirect url="http://www.baidu.com"></c:redirect></body> </html>

jsp:include

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>頭部頁面</title> </head><body>通用的頭部頁面的內(nèi)容<br/>參數(shù): <%=request.getParameter("name") %></body> </html><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>動(dòng)作標(biāo)簽</title> </head><body><%--轉(zhuǎn)發(fā) --%><%//request.getRequestDispatcher("/09.action2.jsp?name=eric").forward(request,response);%><%-- 參數(shù) --%><%--<jsp:forward page="/09.action2.jsp"><jsp:param value="jacky" name="name"/><jsp:param value="123456" name="password"/></jsp:forward>--%><%--包含 --%><%--<jsp:include page="/common/header.jsp"><jsp:param value="lucy" name="name"/></jsp:include>--%><%@include file="common/header.jsp" %>主頁的內(nèi)容</body> </html>

?

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

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的JSP标签和JSTL标签注意点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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