JSP概述
jsp本质上还是个Servlet,会被编译成Servlet执行,能够更好的编写页面代码
脚本的三种方式
<%%>普通脚本 里面的变量是局部的 在service方法里
<%! %>声明脚本 全局变量 在service方法外,与service同级
<%=%>输出脚本 向页面输出内容
<%!
//全局里面可以放方法 局部里面不能放
//局部的代码编译后在servlet的service里面
//全局的代码编译后和service同级
public void a(){
}
String src="hello";
%>
<!--直接在在页面输出用这个标签-->
<%=src%>
JSP注释
第一种是引用html注释,显示注释<!-- 注释内容–>
第二种隐式注释JSP本身的注释,<%—注释内容—%>
JSP指令page、include、taglib
page指令用于配置:
里面有import导入包、
session用于设置是否创建session、
content-type设置编码、
errorPage用于当发生异常跳转到异常界面、
isErrorPage用于创建exception作为异常处理界面、
language用于设置语言、
pageEncoding指定jsp页面的解码格式
<%@ page contentType="text/html;charset=UTF-8" language="java" session="true" errorPage="errow.jsp" isErrorPage="true" pageEncoding="UTF-8" import="com.alibaba.druid.Constants" %>
include指令用于包含另一个界面,此include指令属于静态包含
属于代码叠加,一起编译,会出现变量重名冲突(看编译生成的源码)
<%@include file="login.jsp" %>
taglib用于导入第三方包
需要提前加入jar包才能导入
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
通过prefix的c可以调用里面的方法
对test里面的东西判断为true就执行内容1
<c:if test="${empty src}">内容1</c:if>
JSP动作include、userBean(对象)、setProperty、getProperty、forward、param
案例userBean(对象)、setProperty、getProperty
//相当于 User user1=new User();
//user1.ename=admin;
<jsp:useBean id="user1" class="com.lee.entity.User" >
<jsp:setProperty name="user1" property="ename" value="admin"></jsp:setProperty>
</jsp:useBean>
<jsp:getProperty name="user1" property="ename"></jsp:getProperty>
案例include
//动态包含,把包含的页面编译之后返回的结果进行接收,会产生两个文件
<jsp:include page="login.jsp"></jsp:include>
案例forward、param附带参数
//forward包含里面连空格都不能出现,否则报错
<jsp:forward page="login.jsp">
<jsp:param name="admin" value="admin"></jsp:param>
<jsp:param name="pwd" value="admin"></jsp:param>
</jsp:forward>
四大域对象
pageContext、request、session、ServletContext(application)
pageContext当前页面有效
request一起请求有效
session一次会话有效
ServletContext服务器运行过程中有效
内置对象
JSP自动创建直接使用
1、pageContext
2、request
3、response
4、session ---------- 由session=“true”开关
5、application
6、config
7、out ------------- javax.servlet.jsp.JspWriter
8、exception -----------由isErrorPage=“false”开关
9、page -----------当前servlet实例
EL表达式和JSTL
EL表达式的作用
用于获取数据在页面中显示
${}会依次从pageContext、request、session、application(ServletContext)中查找数据,查找到后就不会往后查找
//users集合保存在了session中,通过${users}取出后遍历users集合,通过user取出
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
<td>${user.age}</td>
<td>${user.sex}</td>
<td>${user.birthday}</td>
<td>${user.email}</td>
<td><a href="SelectUser?id=${user.id}">编辑</a><a href="DelectUserServlet?id=${user.id}">删除</a></td>
</tr>
</c:forEach>
寻找指定作用域的对象
${pageContext.users}
${requestScope.users}
${sessionScope.users}
${applicationScope.users}
JSTL的使用
需要导入包,标签库用taglib引用
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt”%>
//forEach循环遍历
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td><a href="SelectUser?id=${user.id}">编辑</a><a href="DelectUserServlet?id=${user.id}">删除</a></td>
</tr>
</c:forEach>
//判断test里面的条件为true就执行
<c:if test="">执行块</c:if>
//当test的条件为true就执行否则执行otherwise
<c:choose>
<c:when test="">执行块</c:when>
<c:otherwise></c:otherwise>
</c:choose>
练习
<pre>
1、显示注释 继承于html
<!--sss-->
2、隐式注释
2.1、jsp自身的注释
<%--我是注释--%>
2.2、继承java的注释
<%
//我是注释
/* 我是注释 */
%>
3、脚本段
局部变量
//局部变量不能再全局使用
<% int a=10;%>
声明全局变量、方法、类
//全局可以在局部使用
<%! int num=20;%>
输出表达式、字面量、变量
<%=num%>
</pre>
<pre>
<h1>include</h1>
页面的静态包含include
<%@include file="index.jsp"%>
//动态包含include jsp双标签之间不能有任何东西,包括空格 注释
//只能包含参数jsp:param name(不能用表达式),value(可以)
// 用res.getParameter接收
<jsp:include page="index.jsp">
<jsp:param name="uname" value="admin"></jsp:param>
<jsp:param name="upwd" value="123"></jsp:param>
</jsp:include>
</pre>
<pre>
<h1>EL表达式</h1>
pageContext范围 只在一个一面中保存属性 跳转后无效
EL表达式 取作用域的值
默认从小到大的范围寻找,都没有返回空字符串
pageContext-request-session-application
默认从pageContext开始寻找
寻找特定的域的值
pageContext:${pageScope.name}
request:${requestScope.name}
空:${a}
取list、map、user
<%
List list=new ArrayList();
list.add("aaa");
list.add("bbb");
request.setAttribute("list",list);
Map map=new HashMap();
map.put("key1","aaa");
map.put("key2","bbb");
request.setAttribute("map",map);
%>
集合长度:${list.size()}
${list[1]}
map:
${map.key1}
${map["key2"]}
<%
Map map2=new HashMap();
Map map3=null;
request.setAttribute("map2",map2);
request.setAttribute("map3",map3);
request.setAttribute("data",new Date());
%>
判断是否为空 javaBean 对象 new了之后不为空 其他的new了之后如果长度为0或者本身为null就判断为空
map2:${empty map2}
map3:${empty map3}
运算符比较${map==map2}
</pre>
<pre>
<h1>JSTL标签</h1>
导包
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="${empty map2}">判断的test为true 进行处理</c:if>
//if没有else
// choose和otherwise没有标签
// choose必须包含至少一个when
//otherwise必须在最后一个when之后
//when和otherwise可以包含其他标签
<br>
<c:choose>
<c:when test="${empty map2}">判断成立when进来,可以包含其他标签
<c:if test="${empty map2}">二次判断</c:if>
</c:when>
<c:otherwise>when不成立就otherwise</c:otherwise>
</c:choose>
// foreach循环
//var为i,i存放循环得到的数,输出i
<c:forEach var="i" begin="1" end="10">
${i}
</c:forEach>
// var为str,str存放循环得到的数,输出str
<c:forEach var="str" items="${map}">
${str}
</c:forEach>
//解决date
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f"%>
<f:formatDate value="${data}" type="date"></f:formatDate><br>
<f:formatDate value="${data}" type="time"></f:formatDate><br>
<f:formatDate value="${data}" pattern="yyyy-MM-dd"></f:formatDate>
</pre>