7.JSP基础语法,指令和标签以及Java Bean
一.JSP的基础语法
<%--1.JSP表达式
    作用:用于将程序的结果,输出到客户端
    语法:
    <%=变量或者表达式 %>
--%>
<%= new java.util.Date()%>
<br>
<%--2.SP脚本片段--%>
<%
    int sum = 0;
    for (int i = 0; i < 100; i++) {
        sum += i;
    }
    out.print("<h1>Sum=" + sum + "</h1>");
%>
<%--3.在代码中嵌套HTML元素--%>
<%
    for (int i = 0; i < 5; i++) {
%>
<h3>hello world <%=i%></h3>
<%
    }
%>
<%--4.JSP声明:提升作用域到全局--%>
<%!
    static {
        System.out.println("Loading Servlet");
    }
    private int globalVar =0;
    public void func(){
        System.out.println("方法func");
    }
%>
<%--5.JSP注释:不会在浏览器的查看源代码中显示,HTML的注释会在浏览器的查看源代码中显示--%>二.JSP指令
1.手动指定错误页面和使用配置文件web.xml指定错误页面
1 <%-- 可以手动指定错误页面,也可以在web.xml中配置指定错误页面<%@ page errorPage="error/500.jsp" %>--%>
<error-page> <error-code>404</error-code> <location>/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500.jsp</location> </error-page>
404.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>404</title>
</head>
<body>
<img src="../img/404.jpg" alt="404">
</body>
</html>500.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<img src="../img/500.png" alt="500">
</body>
</html>2.指定嵌入页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>嵌入头部和尾部页面</title>
</head>
<body>
<%--嵌入页面方法一:使用include--%>
<%--不会将页面拼接,直接将文件中的内容放在一起,会导致各自页面中的变量可能同名而冲突--%>
<%@include file="common/header.jsp"%>
<h1>页面主体</h1>
<%@include file="common/footer.jsp"%>
<%--嵌入页面方法二:使用jsp:include标签 (推荐使用)--%>
<%--会将页面拼接,各自页面中的变量互不干扰--%>
<jsp:include page="common/header.jsp"></jsp:include>
<h1>页面主体</h1>
<jsp:include page="common/footer.jsp"></jsp:include>
</body>
</html>header.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <h1>我是头部</h1>
footer.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <h1>我是尾部</h1>
三.JSP的9大内置对象
- PageContext 存东西
- Request
- Response
- Session 存东西
- Application [ServletContext] 存东西
- config [ServletConfig]
- out
- page
- exception
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--内置对象--%>
<%
    //作用域:page-->request-->session-->application 从小到大
    pageContext.setAttribute("name1","王1"); //保存的数据只在一个页面中有效
    request.setAttribute("name2","王2");//保存的数据只在请求中有效,但是如果使用请求转发则可以携带数据
    session.setAttribute("name3","王3");//保存的数据只在一次会话中有效,关闭浏览器后数据丢失
    application.setAttribute("name4","王4");//保存的数据只在服务器有效,关闭服务器数据丢失
%>
<%
    String name1 = pageContext.findAttribute("name1").toString();
    String name2 = pageContext.findAttribute("name2").toString();
    String name3 = pageContext.findAttribute("name3").toString();
    String name4 = pageContext.findAttribute("name4").toString();
    String name5 = pageContext.findAttribute("name5").toString(); //不存在的值
    //前台使用pageContext请求转发,后台使用则使用request
    pageContext.forward("/index.jsp");
    //等价于
    //request.getRequestDispatcher("/index.jsp").forward(request,response);
    
    //pageContext中可以设置scope作用域参数
//    public static final int PAGE_SCOPE = 1;
//    public static final int REQUEST_SCOPE = 2;
//    public static final int SESSION_SCOPE = 3;
//    public static final int APPLICATION_SCOPE = 4;
    pageContext.setAttribute("hello1","你好1",PageContext.SESSION_SCOPE);
    //等价于
//    session.setAttribute("hello1","你好1");
    
%>
<%--使用EL表达式将值输出: ${}--%>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>
</body>
</html>request:用于用户看完就没有用的数据,例如:新闻
session:购物车案例中使用(一个人使用)
application:聊天数据使用(多人使用)
四.JSP标签、JSTL标签、EL表达式
1.导入包:
<!--JSTL表达式依赖--> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <!--standard标签库--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
2.EL表达式:${}
- 获取数据
- 执行运算
- 获取web开发常用对象
- 调用Java方法
3.JSP标签:
jsptag1.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsptag1</title>
</head>
<body>
<h1>jsptag1</h1>
<%--jsp:include 用于嵌入页面--%>
<%--jsp:forward 用于请求转发--%>
<jsp:forward page="jsptag2.jsp">
    <jsp:param name="name" value="wang"></jsp:param>
    <jsp:param name="age" value="24"></jsp:param>
</jsp:forward>
</body>
</html>jsptag2.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsptag2</title>
</head>
<body>
<h1>jsptag2</h1>
<%--取出参数--%>
名字:<%=request.getParameter("name")%>
年龄:<%=request.getParameter("age")%>
</body>
</html>4.JSTL标签
- JSTL标签库的使用是为了弥补HTML标签的不足,标签的功能和Java代码一样
(1)根据JSTL标签所提供的功能,可以将其分为5个类别。
- 核心标签(掌握部分)
- 格式化标签
- SQL 标签
- XML 标签
- JSTL 函数

(2)JSTL标签库使用步骤
- 引入对应的taglib的jar包
- 注意:Tomcat中也需要引入jstl的jar 包,否则会报错:JSTL解析错误
c:if

c:choose , c:when

c:forEach

五.Java Bean

