表达式语言(Expression Language,EL)
提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。
1、pageContext 表示javax.servlet.jsp.PageContext 对象
2、pageScope 表示从page 属性范围查找输出属性
3、requestScope 表示从request 属性范围查找输出属性
4、sessionScope 表示从session 属性范围查找输出属性
5、applicationScope 表示从application 属性范围查找输出属性
6、param 接收传递到本页面的参数
7、paramValues 接收传递到本页面的一组参数
8、header 取得一个头信息数据
9、headerValues 取出一组头信息数据
10、cookie 取出cookie 中的数据
11、initParam 取得配置的初始化参数
寻找值的顺序:page->request->session->application
<body>
<%
pageContext.setAttribute("info1","page范围的值");
request.setAttribute("info1","request范围的值");
session.setAttribute("info1","session范围的值");
application.setAttribute("info1","application范围的值");
%>
<h1>${info1 }h1>
body>
执行结果是:page范围的值
param:单个参数
paramValues:一组参数
<body>
<form action="el2.jsp" method="post">
<input type="text" name="name"/>
<input type="submit" value="提交el2.jsp"/>
form>
<a href="el2.jsp?age=12">提交el2.jspa>
<form action="el2.jsp" method="post">
<input type="checkbox" name="hobby" value="java语言"/>java语言
<input type="checkbox" name="hobby" value="C#语言"/>C#语言
<input type="checkbox" name="hobby" value="php语言"/>php语言
<input type="submit" value="提交el2.jsp"/>
form>
body>
//el2.jsp
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<h1>姓名:${param.name }h1>
<h1>年龄:${param.age }h1>
<h1>爱好一:${paramValues.hobby[0] }h1>
<h1>爱好二:${paramValues.hobby[1] }h1>
<h1>爱好三:${paramValues.hobby[2] }h1>
body>
<%@page import="com.java1234.model.People" %>
<body>
<%
People zhangsan=new People();
zhangsan.setId(1);
zhangsan.setName("张三");
zhangsan.setAge(20);
pageContext.setAttribute("zhangsan",zhangsan);
%>
<h1>编号:${zhangsan.id }h1>
<h1>姓名:${zhangsan.name }h1>
<h1>年龄:${zhangsan.age }h1>
body>
<%@ page import="java.util.*" %>
<body>
<%
List all=new LinkedList();
all.add(0,"元素一");
all.add(1,"元素二");
all.add(2,"元素三");
request.setAttribute("all",all);
%>
<h1>${all[0] }h1>
<h1>${all[1] }h1>
<h1>${all[2] }h1>
body>
算数运算符,关系运算符,逻辑运算符;
三目运算符;
empty 关键字:判断是否空
<body>
<%
request.setAttribute("num1",10);
request.setAttribute("num2",3);
request.setAttribute("flag1",true);
request.setAttribute("flag2",false);
%>
<h5>算数运算符h5>
<p>num1=${num1 },num2=${num2 }p>
<p>num1+num2=${num1+num2 }p>
<p>num1-num2=${num1-num2 }p>
<p>num1*num2=${num1*num2 }p>
<p>num1/num2=${num1/num2 }p>
<p>num1%num2=${num1%num2 }p>
<p>num1*(num1-num2)=${num1*(num1-num2) }p>
<h5>关系运算符h5>
<p>flag1=${flag1 },flag2=${flag2 }p>
<p>与操作flag1 && flage2:${flag1 && flage2 }p>
<p>或操作flag1 || flage2:${flag1 || flage2 }p>
<p>非操作!flag1:${!flag1}p>
<h5>三目运算符h5>
<p>三目操作:num1>num2:${num1>num2?"yes":"no" }p>
<h5>empty关键字h5>
<p>判断空操作:${empty num1 }p>
<p>判断空操作:${empty num3 }p>
body>
Jsp自定义标签在功能上逻辑上与javaBean 类似,都封装Java代码。自定义标签是可重用的组件代码,并且允许开发人员为复杂的操作提供逻辑名称。
JSP开发人员使用标签库创建标签.标签库是按照功能或实现进行分组的自定义标签的集合。
第二节:问候自定义标签他大爷
//HelloWorldTag.java
package com.java1234.tag;
import java.io.IOException;
public class HelloWorldTag extends TagSupport {
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
JspWriter out=this.pageContext.getOut();
try {
out.print("自定义标签大爷你好!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY; //直接结束标签
}
}
//WEB-INF目录下新建一个java1234.tld文件,配置
<tag>
<name>helloWorldname>
<tag-class>
com.java1234.tag.HelloWorldTag
tag-class>
<body-content>emptybody-content>
tag>
//helloWorldTag.jsp
<%@ taglib prefix="java1234" uri="/WEB-INF/java1234.tld" %>
<body>
<java1234:helloWorld/>
body>
运行结果如图:
在上面的HelloWorldTag.java添加name属性,文件命名为HelloWorldTag2.java
public class HelloWorldTag2 extends TagSupport {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
JspWriter out=this.pageContext.getOut();
try {
out.print(name+"自定义标签大爷你好!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY; //直接结束标签
}
}
配置Java234.tld文件
<tag>
<name>helloWorld2name>
<tag-class>
com.java1234.tag.HelloWorldTag2
tag-class>
<body-content>emptybody-content>
<attribute>
<name>namename>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
helloWorldTag.jsp
<body>
<java1234:helloWorld2 name="JspServlet屌炸天"/>
body>
运行结果:
//IterateTag.java,tld文件配置略
public class IterateTag extends TagSupport{
private static final long serialVersionUID = 1L;
private String var;
private String items;
private Iterator iter;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
public Iterator getIter() {
return iter;
}
public void setIter(Iterator iter) {
this.iter = iter;
}
@Override
public int doStartTag() throws JspException {
Object value=this.pageContext.getAttribute(items); //items="people"
if(value!=null && value instanceof List){
this.iter=((List)value).iterator();
if(iter.hasNext()){
this.pageContext.setAttribute(var, iter.next()); //var="p"
return TagSupport.EVAL_BODY_INCLUDE; // 执行标签体
}else{
return TagSupport.SKIP_BODY; // 退出标签执行
}
}else{
return TagSupport.SKIP_BODY; // 退出标签执行
}
}
@Override
public int doAfterBody() throws JspException {
if(iter.hasNext()){
this.pageContext.setAttribute(var, iter.next());
return TagSupport.EVAL_BODY_AGAIN; // 再执行一次标签体
}else{
return TagSupport.SKIP_BODY; // 退出标签执行
}
}
}
//iterateTag.jsp
<body>
<%
List people=new ArrayList();
people.add(0,"王二小");
people.add(1,"丝丝光");
people.add(2,"草泥马");
pageContext.setAttribute("people", people);
%>
<java1234:iterate items="people" var="p">
<h2>${p }h2>
java1234:iterate>
body>
//IterateSimpleTag.java,tld文件配置略
public class IterateSimpleTag extends SimpleTagSupport{
private static final long serialVersionUID = 1L;
private String var;
private String items;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
@Override
public void doTag() throws JspException, IOException {
Object value=this.getJspContext().getAttribute(items);
if(value!=null && value instanceof List){
Iterator iter=((List)value).iterator();
while(iter.hasNext()){
this.getJspContext().setAttribute(var, iter.next());
this.getJspBody().invoke(null); // 响应页面
}
}
}
}
//iterateSimpleTag.jsp
<body>
<%
List people=new ArrayList();
people.add("王二小2");
people.add("丝丝光2");
people.add("草泥马2");
pageContext.setAttribute("people", people);
%>
<java1234:iterate2 items="people" var="p">
<h2>${p }h2>
java1234:iterate2>
body>
JSTL(JSP Standard Tag Library ,JSP 标准标签库)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<c:out value="jstl大爷你好">c:out>
body>
c:out 内容输出标签;
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<%
pageContext.setAttribute("people","张三");
%>
<h2><c:out value="${people}">c:out>h2>
<h2><c:out value="${people2}" default="某人">c:out>h2>
c:set 用来设置4种属性范围值的标签;
<body>
<c:set var="people" value="张三" scope="request">c:set>
<h2><c:out value="${people}">c:out>h2>
<jsp:useBean id="people2" class="com.java1234.model.People" scope="page">jsp:useBean>
<c:set property="id" target="${people2 }" value="007">c:set>
<c:set property="name" target="${people2 }" value="王二小">c:set>
<c:set property="age" target="${people2 }" value="16">c:set>
<h2>编号:${people2.id }h2>
<h2>姓名:${people2.name }h2>
<h2>年龄:${people2.age }h2>
body>
c:remove 用来删除指定范围中的属性;
<body>
<c:set var="people" value="张三" scope="request">c:set>
<h2><c:out value="${people}" default="没人啊">c:out>h2>
<c:remove var="people" scope="request"/>
<h2><c:out value="${people}" default="没人啊">c:out>h2>
body>
c:catch 用来处理程序中产生的异常;
<body>
<c:catch var="errMsg">
<%
int a=1/0;
%>
c:catch>
<h2>异常信息:${errMsg }h2>
body>
c:if 用来条件判断;
<body>
<jsp:useBean id="people" class="com.java1234.model.People" scope="page">jsp:useBean>
<c:set property="id" target="${people }" value="007">c:set>
<c:set property="name" target="${people }" value="王二小">c:set>
<c:set property="age" target="${people }" value="16">c:set>
<c:if test="${people.name=='王二小' }" var="r" scope="page">
<h2>是王二小h2>
c:if>
<c:if test="${people.age<18 }">
<h2>是未成年h2>
c:if>
body>
c:choose、c:when、c:otherwise 用来多条件判断;
<body>
<jsp:useBean id="people" class="com.java1234.model.People" scope="page">jsp:useBean>
<c:set property="id" target="${people }" value="007">c:set>
<c:set property="name" target="${people }" value="王二小">c:set>
<c:set property="age" target="${people }" value="19">c:set>
<c:choose>
<c:when test="${people.age<18 }">
<h2>小于18h2>
c:when>
<c:when test="${people.age==18 }">
<h2>等于18h2>
c:when>
<c:otherwise>
<h2>大于18h2>
c:otherwise>
c:choose>
body>
c:forEach 用来遍历数组或者集合;
<%@ page import="com.java1234.model.*"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<%
String dogs[]={"小黑","小黄","小白","小小"};
pageContext.setAttribute("dogs",dogs);
%>
<c:forEach var="dog" items="${dogs }">
${dog }
c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" step="2">
${dog }
c:forEach>
<hr/>
<c:forEach var="dog" items="${dogs }" begin="1" end="2">
${dog }
c:forEach>
<hr/>
<%
List
pList.add(new People(1,"张三",10));
pList.add(new People(2,"李四",20));
pList.add(new People(3,"王五",30));
pageContext.setAttribute("pList",pList);
%>
<table>
<tr>
<th>编号th>
<th>姓名th>
<th>年龄th>
tr>
<c:forEach var="p" items="${pList }">
<tr>
<td>${p.id }td>
<td>${p.name }td>
<td>${p.age }td>
tr>
c:forEach>
table>
body>
c:fortoke ns 分隔输出;
<body>
<%
String str1="www.java1234.com";
String str2="张三,李四,王五";
pageContext.setAttribute("str1",str1);
pageContext.setAttribute("str2",str2);
%>
<c:forTokens items="${str1 }" delims="." var="s1">
${s1 }
c:forTokens>
<hr/>
<c:forTokens items="${str2 }" delims="," var="s2">
${s2 }
c:forTokens>
body>
c:import 导入页面;
<body>
<c:import url="c_if.jsp">c:import>
<c:import url="c_choose.jsp">c:import>
body>
c:url 生成一个url 地址;
<body>
<c:url value="http://www.java1234.com" var="url">
<c:param name="name" value="xiaofeng">c:param>
<c:param name="age" value="26">c:param>
c:url>
<a href="${url }">Java知识分享网a>
body>
c:redirect 客户端跳转
<body>
<c:redirect url="target.jsp">
<c:param name="name" value="xiaofeng">c:param>
<c:param name="age" value="26">c:param>
c:redirect>
body>
//target.jsp
<body>
<h2>姓名:${param.name }h2>
<h2>年龄:${param.age }h2>
body>
fmt:setLocale 设定用户所在的区域;
fmt:formatDate 对日期进行格式化;
<%@ page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<body>
<%
pageContext.setAttribute("date",new Date());
%>
中文日期:
<fmt:setLocale value="zh_CN"/>
<fmt:formatDate value="${date }"/>
<hr/>
英文日期:
<fmt:setLocale value="en_US"/>
<fmt:formatDate value="${date }"/>
body>
fmt:requestEncoding 设置所有的请求编码;
<body>
<fmt:requestEncoding value="UTF-8"/>
body>
fmt:bundle fmt:message fmt:param 读取国际化资源;
//文件info_zh_CN.properties
name=\u5c0f\u950b
info=\u5f53\u524d\u7528\u6237{0}:\u6b22\u8fce\u4f7f\u7528\u672c\u7cfb\u7edf
//文件info_en_US.properties
name=xiaofeng
info=Current user{0}:Welcome to use our system
//fmt_bundle.jsp
<body>
<fmt:setLocale value="zh_CN"/>
<fmt:bundle basename="info">
<fmt:message key="name" var="userName"/>
fmt:bundle>
<h2>姓名:${userName }h2>
<fmt:bundle basename="info">
<fmt:message key="info" var="infomation">
<fmt:param value="<font color='red'>小锋font>"/>
fmt:message>
fmt:bundle>
<h2>信息:${infomation }h2>
<hr/>
<fmt:setLocale value="en_US"/>
<fmt:bundle basename="info">
<fmt:message key="name" var="userName"/>
fmt:bundle>
<h2>姓名:${userName }h2>
<fmt:bundle basename="info">
<fmt:message key="info" var="infomation">
<fmt:param value="<font color='red'>小锋font>"/>
fmt:message>
fmt:bundle>
<h2>信息:${infomation }h2>
body>
fmt:formatNumber 格式化数字;
<body>
<fmt:formatNumber value="12" type="currency" pattern="$.00"/>
<fmt:formatNumber value="12" type="currency" pattern="$.0#"/>
<fmt:formatNumber value="1234567890" type="currency"/>
<fmt:formatNumber value="123456.7891" pattern="#,#00.0#"/>
body>
fmt:formatDate 格式化日期;
<body>
<%
Date date=new Date();
pageContext.setAttribute("date",date);
%>
<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
<hr/>
<fmt:formatDate value="${date }" pattern="yyyy-MM-dd"/>
body>
fmt:timeZone 设置临时时区;
<body>
<%
Date date=new Date();
pageContext.setAttribute("date",date);
%>
当前时间:<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
<hr/>
格林尼治时间:
<fmt:timeZone value="GMT">
<fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/>
fmt:timeZone>
body>
Sql:setDataDource 设置JDBC 连接;
<body>
<h1>设置JDBC连接h1>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl?serverTimezone=UTC" user="root" password="password" />
body>
sql:query 数据库查询操作;
<body>
<h1>设置JDBC连接h1>
<sql:setDataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/db_jstl?serverTimezone=UTC" user="root" password="password" />
<sql:query var="result">
select * from t_student;
sql:query>
<h2>总记录数:${result.rowCount }h2>
<table>
<tr>
<th>编号th>
<th>学号th>
<th>姓名th>
<th>出生日期th>
<th>性别th>
tr>
<c:forEach var="student" items="${result.rows }">
<tr>
<td>${student.id }td>
<td>${student.stuNo }td>
<td>${student.stuName }td>
<td>${student.birthday }td>
<td>${student.sex }td>
tr>
c:forEach>
table>
body>
Sql:update 数据库添加,修改,删除操作;
<h1>添加数据h1>
<sql:update var="result" >
insert into t_student values(null,"008","草泥马","1991-1-1","男");
sql:update>
<h1>修改数据h1>
<sql:update var="result" >
update t_student set stuNo="010",sex="未知" where id=6
sql:update>
<h1>删除数据h1>
<sql:update var="result" >
delete from t_student where id=6
sql:update>
sql:transaction 数据库事务;
<h1>事务h1>
<sql:transaction>
<sql:update var="result" >
insert into t_student values(null,"008","草泥马","1991-1-1","男");
sql:update>
sql:transaction>
x:parse 解析xml;
x:out 输出xml 文件的内容;
//usersInfo.xml
xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name id="n1">张三name>
<birthday>2011-1-1birthday>
user>
users>
//xml_out.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
head>
<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<h2>姓名:<x:out select="$usersInfoXml/users/user/name"/>
(ID:<x:out select="$usersInfoXml/users/user/name/@id"/>)h2>
<h2>出生日期:<x:out select="$usersInfoXml/users/user/birthday"/>h2>
body>
x:set 把xml 读取的内容保存到指定的属性范围;
<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:set var="userInfoXml" select="$usersInfoXml/users/user"/>
<h2>姓名:<x:out select="$userInfoXml/name"/>h2>
body>
x:if 判断指定路径的内容是否符合判断的条件;
<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:if select="$usersInfoXml/users/user/name/@id='n1'">
<h2>有编号是n1的user信息h2>
x:if>
body>
x:choose x:when x:otherwise 多条件判断;
<body>
<c:import var="usersInfo" url="usersInfo.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:choose>
<x:when select="$usersInfoXml/users/user/name/@id='n2'">
<h2>有编号是n2的user信息h2>
x:when>
<x:otherwise>
<h2>没有编号是n2的user信息h2>
x:otherwise>
x:choose>
body>
x:forEach 遍历
//usersInfo2.xml
xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name id="n1">张三name>
<birthday>2011-1-1birthday>
user>
<user>
<name id="n2">王五name>
<birthday>2011-1-2birthday>
user>
<user>
<name id="n3">赵六name>
<birthday>2011-1-3birthday>
user>
users>
//xml_forEach.jsp
<body>
<c:import var="usersInfo" url="usersInfo2.xml" charEncoding="UTF-8"/>
<x:parse var="usersInfoXml" doc="${usersInfo }"/>
<x:forEach select="$usersInfoXml/users/user" var="userInfoXml">
姓名:<x:out select="$userInfoXml/name"/><br>
出生日期:<x:out select="$userInfoXml/birthday"/>
<hr/>
x:forEach>
body>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<body>
<%
pageContext.setAttribute("info","www.java1234.com");
%>
<h2>查找java1234位置:${fn:indexOf(info,"java1234")}h2>
<h2>判断java1234是否存在:${fn:contains(info,"java1234")}h2>
<h2>截取:${fn:substring(info,0,5)}h2>
<h2>拆分:${fn:split(info,".")[1]}h2>
body>
End完!