一、jsp标签(sun公司提供的)

二、EL表达式

三、jstl (javaserver pages standard tag library)

1.为什么要有jstl

  jsp标签太弱,el表达式功能不完整(比如不能遍历集合),所有就有了jstl。目的就是配合jsp+el取代在jsp中嵌入Java代码的方式,提高程序的可读性、维护性、方便性

2. jstl实质、原理

  jstl的实现过程就相当于el表达式中调用Java方法的实现过程。首先定义类包含静态方法,定义一个tdl文件对函数进行描述,然后在页面上用tablib指令引用文件,然后就可以通过前缀加冒号调用函数了

3.jstl五个类型的库(核心标签库、国际化标签库、数据库标签库、xml标签库、jstl函数,其中SQL标签库跟xml标签库不常用所以不学)

  3.1 core标签库

    3.1.1 c:out

      3.1.1.1 输出常量         

      3.1.1.2 输出变量

      3.1.1.3 输出默认值(有值就输出值没有值就输出默认值)

      3.1.1.4 html转义输出  ps:escapeXml属性能控制是否按转义输出默认值为true

jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'cout.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>输出常量</h1><hr>
<c:out value="你好我这是输出常量"></c:out>
<h1>输出变量</h1><hr>
<%String name = "无双";
pageContext.setAttribute("name",name);
%>
<c:out value="${name }"></c:out>
<h1>输出默认值</h1><hr>
<c:out value="${name}" default="奥巴马(这个是默认值)"></c:out>
<c:out value="${name1 }" default="奥巴马(这个是默认值)"></c:out>
<h1>输出html转义</h1><hr>
<c:out value="<a href='#'>html转义内容</>" escapeXml="true"></c:out>
</body>
</html>

页面效果:

    3.1.2 c:set  ps:var属性有变量则找到变量没有则声明一个变量;value属性设置变量值;scope:指定域;

      3.1.2.1 设置或修改域中属性的值

      3.1.2.2 设置或修改域中map的属性值

      3.1.2.3 设置或修改域中javabean的属性值

jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'cset.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>设置域中属性的值</h1>
<c:set var="name" value="力宏" scope="page"></c:set> <br>
${name }
<c:set var="name" value="王力宏" scope="page"></c:set> <br>
${name }
<h1>设置域中map的属性值</h1><hr>
<%Map map = new HashMap();
pageContext.setAttribute("map", map);
%>
<c:set target="${map }" property="cellphone" value="10086"></c:set>
${map.cellphone}(设置map值)<br>
<c:set target="${map }" property="cellphone" value="10087"></c:set>
${map.cellphone}(修改map值)
<h1>设置域中javabean的属性值(用法同map)</h1><hr>
</body>
</html>

效果图:

    3.1.3 c:remove 移除域中属性的值   <c:remove var="name" scope="page"/>指定变量名称然后指定域

    3.1.4 c:catch 捕获异常(把异常抓起来,捕获了页面上就不会有异常了,用户体验就友好了)    

<c:catch var="e">
<%int i= 1/0; %>
</c:catch>
${e.message}

效果:

    3.1.5 c:if 执行判断条件,有一个很重要的属性test接收一个bool值

    3.1.6 c:choose、c:when、c:otherwise

        c:chooose要c:when和c:otherwise配合使用

    3.1.7 c:foreach  ps:items属性指定要遍历的集合

      3.1.7.1 遍历数组 string[]

      3.1.7.2 遍历集合 list

      3.1.7.3 遍历map HashMap、linkedHashMap(按顺序输出)

      3.1.7.4 循环执行指定的内容若干次

    3.1.8 c:forTokens  切割字符串

    3.1.9 c:import  包含文件

    3.1.10 c:redirect、c:param  重定向

    3.1.11 c:url  替代代码进行url重写

代码:cforeach、cfortokens

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'cforeach.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<%String[] cities = new String[]{"北京","上海","广州"};
pageContext.setAttribute("city",cities);
%>
<body>
<h1>遍历数组</h1><br>
<c:forEach items="${city }" var="c"> ${c}<br></c:forEach><br>
<h1>遍历集合</h1><br>
<%
List list = new ArrayList();
list.add("中国");
list.add("美国");
list.add("俄罗斯");
pageContext.setAttribute("list", list);
%>
<c:forEach items="${list}" var="c">${c}<br></c:forEach>
<h1>遍历map</h1><br>
<%
Map map = new LinkedHashMap();
map.put("name","力宏");
map.put("age", 19);
map.put("wife", "呵呵");
pageContext.setAttribute("map", map);
%>
<c:forEach items="${map }" var="entry">
${entry.key }:${entry.value }<br>
</c:forEach>
<h1>循环执行指定的内容若干次</h1><br>
<c:forEach begin="1" end="20" step="1" var="i" varStatus="stat">
<c:if test="${stat.index%2 == 0 }">
<font color="red"> ${i }</font> </c:if>
<c:if test="${stat.index%2!= 0 }">
<font color="blue">${i}</font>
</c:if>
</c:forEach>
<h1>c:forTokens切割字符串</h1><br>
<c:forTokens items="www.aqioo.com" delims="." var="c">
${c }<br>
</c:forTokens>
</body>
</html>

效果图:

代码:cif

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'cif.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<%int day = 4;
pageContext.setAttribute("day",day);
%>
<body>
<c:if test="${ 2>1}">就是这样的</c:if> <br>
<c:choose>
<c:when test="${day==3 }">星期三</c:when>
<c:otherwise>
不是星期三
</c:otherwise>
</c:choose>
</body>
</html>

代码:credirect

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'credirect.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>credirect练习</h1> <br>
<c:redirect url="/jstl/cforeach.jsp" context="${pageContext.request.contextPath }">
<c:param name="aa" value="nihao"></c:param>
</c:redirect>
</body>
</html>

效果:直接跳转到cforeach.jsp中去了

代码:curl 当浏览器端禁用cookie后,右键查看源代码就能看到url后跟一个JessesionId

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'curl.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <%
request.getSession();
String url = response.encodeUrl(request.getContextPath()+"/jstl/cforeach.jsp");
pageContext.setAttribute("url", url);
%>
<body>
<h1>url重写练习</h1> <br>
<a href="${url }">这个是用Java代码实现的url重写</a>
<c:url context="${pageContext.request.contextPath }" value="/jstl/cforeach.jsp" scope="page" var="url1"></c:url>
<a href="${url1 }">这个是用c:url实现的url重写
</a>
</body>
</html>

传智播客JavaWeb day06-jstl的更多相关文章

  1. 传智播客JavaWeb day01 快捷键、XML

    2015-01-14 一直计划着学习java,今天晚上终于下定决心看了下传智播客朴乾老师的javaweb开发视频day01之第一讲,主要内容是开发工具简单介绍.怎么创建工程.Junit的介绍,我是C# ...

  2. 传智播客JavaWeb听课总结

    一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本兼容低版本的 ...

  3. 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销

    第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...

  4. 传智播客JavaWeb day02笔记

    2015年1月21日 今天的主要内容:介绍了几款常用Javaweb服务器,重点介绍了tomcat以及tomcat的安装和怎么样检测安装成功 1.JavaWeb常见服务器 Tomcat(免费但是只支持部 ...

  5. 传智播客JavaWeb day09-mysql入门、数据库操作、数据库表操作、数据行操作

    不知不觉已到了第九天了,今天主要讲了关系数据库的基本概述.安装.数据库.表和数据行的操作 1. 基本概述 1.1 数据库就是用来存储数据的.早期是存在文件里面的操作起来效率低而且不是很安全. 1.2 ...

  6. 传智播客JavaWeb day05-session、url重写

    1.session是什么 1.1 session是一种会话技术  ps:还有一种是cookie 2.session的作用 2.1 服务器端会话范围内的数据共享 3.session的生命周期 3.1何时 ...

  7. 传智播客JavaWeb day11--事务的概念、事务的ACID、数据库锁机制、

    1. 什么叫做事务? 2.默认情况下每一条sql语句都是一个事务,然后自动提交事务  ps:如果想多条语句占一个事务,则可以手动设置SetAutoCommit为false 3.关键字 start tr ...

  8. 传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤

    第十天主要讲了jdbc操作mysql数据库,包括连接数据库六大步骤(注册数据库驱动.获得连接对象connetion.生成传输器stament.执行查询获得ResultSet.遍历结果集.关闭资源).介 ...

  9. 传智播客JavaWeb day03

    ServletContext 这堂课主要讲ServletContext这个web域(可以看得见范围的)对象,web在启动的时候会创建唯一的ServletContext域对象. 作用:1.用来web域共 ...

随机推荐

  1. python学习之字典

    1.字典 列表存储的数据比较单一也不够灵活,这时我们可以使用字典来存储某些多内容的数据,字典是无顺序的 1.简单的字典 book={ 'huqiang':13457412571, 'Jasper':1 ...

  2. 深入对比数据科学工具箱:Python和R之争

    建议:如果只是处理(小)数据的,用R.结果更可靠,速度可以接受,上手方便,多有现成的命令.程序可以用.要自己搞个算法.处理大数据.计算量大的,用python.开发效率高,一切尽在掌握. 概述 在真实的 ...

  3. nRF52系列来袭,Nordic的低功耗蓝牙方案大有可为

      坐落在北欧的挪威不像他的邻居芬兰那样,可以先后依靠NOKIA和愤怒的小鸟在世界科技界享有盛名.在一般人看来,挪威除了一个逐渐式微的Opera浏览器以外,并没有更多拿得出手的科技企业.而事实证明这只 ...

  4. 修改PHP 上传文件大小限制

    Windows 环境下的修改方法 ================================================================第一步:修改在php5下POST文件大 ...

  5. centos minimal 开启无线网卡 & 查看IP

    minimal版本默认不启动网络,所以要自己配置. 配置过程很简单,编辑配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 需要更改两项 NM_CONTR ...

  6. Codeforces Round #384 (Div. 2)A,B,C,D

    A. Vladik and flights time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. C++用PostMessage模拟按钮点击

    有时我们可能会在某个程序中用到模拟按钮点击事件. 本文中的例子在MFC程序中调试通过,duilib的没试过,还需探索 不多说,上代码: #include "stdafx.h" #i ...

  8. python collections defaultdict

    class_counts  = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...

  9. Linux 系统目录结构

    登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin: bin是Binary的缩写, 这个目录存放着最经常使用的命令. /boot ...

  10. HTML5基本元素初探

    最近看了一些HTML5的基础知识,写了一些小案例,记录一下,方便查找. 1.新建的HTML5页面中显著的变化是:DOCTYPE声明变简洁(<!DOCTYPE html>) / <me ...