本质:servlet
目的:简化页面书写
* page指令:
language :指定jsp页面使用的语言。只有一个值 java
*import :导入软件包
*pageEncoding :设置页面编码
1.指定jsp文件的编码
2.告诉浏览器页面使用的编码格式
autoFlush:自动刷新
buffer:缓冲区大小
*errorPage:指定错误页面,当页面发生异常时,指定跳转的页面
* 一般在开发完成后,会在web.xml中通过标签来配置整个项目的错误页面
500
/500.jsp
isErrorPage:是否是错误页面
true:是错误页面,可以使用内置对象exception
false:不是
<%@ page contentType="text/html;charset=utf-8" language="java" pageEncoding="utf-8" import="java.io.FileOutputStream" autoFlush="true" buffer="8kb" errorPage="500.jsp" %>
<%@include file=""%>
导入额外的内容,如:导入jstl标签
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%--导入常用的第三方标签库,比如常用的c标签--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--一个页面会把另一个页面包含进来
静态包含:通过静态代码块把页面包含进来,不会为包含进来的页面单独生成一个java类文件
--%>
<%@include file="index.jsp"%>
概念:替换java代码,简化书写,提高可读性
签:替换Java代码。简化书写,提高可读性
使用一个ID和一个给定作用范围和同一ID的JavaBean相关联
设置JavaBean的属性值
取得JavaBean的属性值
请求时文件包含
* 多个.class字节码文件
接受用户输入并将请求分派给另一页面
<%--动态包含--%>
<%--内部转发标签--%>
<%--原来--%>
<%
request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request,response);
%>
<%--现在,记住前面的/不要自己加--%>
application:ServletContext
session:HttpSession
request:HttpServletRequest
pageContext:PageContext
out:JspWriter
response:HttpServletResponse
config:ServletConfig
page:this
exception: Throwable
<%ServletRequest request1 = pageContext.getRequest();
ServletConfig servletConfig = pageContext.getServletConfig();
HttpSession session1 = pageContext.getSession();
pageContext.setAttribute("num", 100);
%>
在使用对象向页面输出信息时,采用out
jsp内的一种语法,为了替代脚本,el可以取出来域中的数据
1.获取域中的数据
2.执行运算
3.获取常见的web对象
4.调用java方法
${el表达式}
${pageScope|requestScope|sessionScope|applicationScope.属性名}
以后经常使用:
${属性名}:依次从pageContext,request,session,application查找指定属性,若查找到返回值,结束该次查找
若查找不到,返回" "
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="org.westos.domain.Person" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/9/8
Time: 16:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--1获取域中的数据,展示在页面上--%>
<%
pageContext.setAttribute("p","ppppp");
request.setAttribute("r","rrrrr");
session.setAttribute("s","sssss");
application.setAttribute("a","aaaaa");
%>
<%--传统的取法--%>
<%=pageContext.getAttribute("p")%>
<%=request.getAttribute("r")%>
<%=session.getAttribute("s")%>
<%=application.getAttribute("a")%>
<%--用EL表达式进行取--%>
${pageScope.p}
${requestScope.r}
${sessionScope.s}
${applicationScope.a}
<%--用数组存键值的形式存到集合里面,el表达式取出来--%>
<%
String[] str={"第一个人","第二个人","第三个人"};
request.setAttribute("arr",str);
ArrayList list = new ArrayList<>();
list.add(100);
list.add(200);
request.setAttribute("jihe",list);
%>
<%--用传统的方法取出来--%>
<%
//先把大集合取出来
String[] arr = (String[]) request.getAttribute("arr");
//再把里面的小集合取出来
String[] jihe = (String[]) request.getAttribute("jihe");
//取出来索引一
Integer integer = list.get(0);
//打印在页面上
response.getWriter().write(integer);
//打印在控制台
out.write(integer);
%>
<%--用EL表达式取出来--%>
${arr[0]}
${arr[1]}
<%--EL表达式支持运算--%>
${1>2?"大于":"小于"}
${100+200+100*3}
<%--EL表达式支持调用java的方法,将集合中的键值取出来--%>
${"afasfsa".length()}
${jihe.get(0)}
<%
HashMap map = new HashMap<>();
map.put("你","是谁");
map.put("我","不认识你");
request.setAttribute("map",map);
%>
<%--传统方法取出来--%>
<%
HashMap map1 = (HashMap) request.getAttribute("map");
request.getAttribute("map");
String value = map1.get("你");
out.write(value);
%>
<%--EL取出map中的数据--%>
${map.你}
${map.get("我")}
<%
Person person = new Person();
person.setName("你叫啥");
person.setAge(50);
session.setAttribute("p",person);
%>
${p.name}
${p.age}
<%
Person person5 = new Person();
person5=null;
session.setAttribute("p5",person5);
%>
<%--
empty 可以判断一个对象是否null 也可以判断一个容器的长度是否为0
--%>
${p5==null}
${empty p5}
${not empty p5}
${pp.size()==0}
${empty pp};
<%--通过el表达式动态获取项目路径--%>
login
<%--${pageContext.request.contextPath}--%>
login