11、JSP注释、三大指令、动作标签、内置对象、EL表达式

注释

标记语言HTML的注释:

<!-- -->
<!-- 缺点:在客户端检查源代码时可以看到,并且不能注释Java代码 -->

JSP的注释

<%-- 优点:不会发送到客户端,可以注释Java代码 --%>

三大指令

格式:

<%@ 指令名称 属性="属性值" %>

分类:

  • page:配置JSP页面的属性,编码、import等;
  • include:静态包含
  • taglib:导入额外的内容

page

<%@ page import="java.io.File" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
  <%--
    JSP:
    3:3个指令;
      page:<%@ page contentType="text/html;charset=UTF-8" language="java" %>
      page指令:主要配置一些页面的信息,
        contentType,(response.setContentType()),language,支持的语言,pageEncoding,页面编码
        import:导入Java类所在的包;
        errorPage:当前页面错误时,跳转的页面;
        isErrorPage:标明此页面是否为错误页面;
      include:用来抽取公共代码,index.jsp需要用到其他页面(页面布局,类似于Vue中的组件)
    6:6个动作标签;
    9:9个JSP内置对象
  --%>

  <%
    File file = new File("a.txt");
    //演示import属性
    int num = 100 / 0;
    //演示服务器500错误
    //404错误可以在浏览器访问项目中不存在的页面
  %>

</body>
</html>

1、错误页面的配置

在web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>
</web-app>

当错误代码为500时,跳转到500.jsp页面;当错误代码为404时,跳转到404.jsp页面。

并且在500.jsp和404.jsp页面中声明,isErrorPage属性为true。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>服务器内部错误。500</h3>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>服务器未找到该资源。404</h3>
</body>
</html>

include

include指令属于静态包含。

include抽取公共页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Public</title>
</head>
<body>
    <h3>公共页面</h3>
</body>
</html>

在index.html中引入

<%@ page import="java.io.File" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
  <%@include file="public.jsp"%>
</body>
</html>

并且在访问index.html时,服务器并不会把public.jsp公共页面也解析为一份.class文件,而是把public.jsp页面放在index.jsp的静态代码块中。

在这里插入图片描述

在这里插入图片描述

taglib

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

动作标签

1、a.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <jsp:useBean id="user" class="org.westos.pojo.User">
        <jsp:setProperty name="user" property="username" value="张三"/>
        <jsp:setProperty name="user" property="age" value="23"/>
        <jsp:getProperty name="user" property="username"/>
        <jsp:getProperty name="user" property="age"/>
    </jsp:useBean>
    <jsp:include page="common.jsp"/>
    <%--动态包含,与静态包含include指令有所不同,这种动态包含会有多个class文件--%>
</body>
</html>

common.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h4>公共页面</h4>
</body>
</html>

在这里插入图片描述

在这里插入图片描述

2、b.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--请求转发--%>
    <jsp:forward page="c.jsp">
        <jsp:param name="key" value="age"/>
        <jsp:param name="value" value="24"/>
    </jsp:forward>
</body>
</html>

c.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String key = request.getParameter("key");
        String value = request.getParameter("value");
    %>
    <%=key + ":" + value%>
</body>
</html>

在这里插入图片描述

内置对象

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        /*四大域对象,范围从大到小*/
        application.setAttribute("uuu", "aaa");
        /*ServletContext*/
        session.setAttribute("sss", "bbb");
        /*HttpSession*/
        request.setAttribute("qqq", "ccc");
		/*HttpServletRequest*/
        pageContext.setAttribute("ttt", "ddd");
		/*PageContext*/
		
        response.getWriter().write("Hello");
        /*HttpServletResponse*/
		out.write("JSP");
		/*JspWriter*/
        /*一般向页面输出时,使用内置的out对象*/
        config.getServletContext();
		/*ServletConfig*/
        /*exception对象只能用于isErrorPage属性为true的页面*/
        /*page对象*/
    %>
</body>
</html>

pageContext内置对象可以获取其他8种内置对象

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        /*pageContext可以获取其他8个内置对象,可以在非jsp环境使用其他内置对象*/
        ServletContext servletContext = pageContext.getServletContext();
        HttpSession session1 = pageContext.getSession();
        ServletRequest request1 = pageContext.getRequest();

        ServletResponse response1 = pageContext.getResponse();
        /*由于这个jsp是Servlet,已经存在默认对象session、request、response这些对象,则需要在后面加上1以区分*/
        Object page1 = pageContext.getPage();
       /*Exception exception = pageContext.getException();*/
        ServletConfig servletConfig = pageContext.getServletConfig();
        JspWriter out1 = pageContext.getOut();
    %>
</body>
</html>

EL表达式

jsp的内置表达式语言,从jsp2.0开始。

1、简单使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>Q
</head>
<body>
    <%--EL表达式用来替代<%=%>这种JSP原始脚本--%>
    <%
        request.setAttribute("username", "john");
        application.setAttribute("age", 23);
        session.setAttribute("gender", "male");
        pageContext.setAttribute("hobby", "basketball");
    %>
    <%--取出域中的数据--%>
    用户名:${username} <br>
    年龄:${age} <br>
    性别:${gender} <br>
    爱好:${hobby} <br>

    <%--当四个域中的属性名相同时,可以用EL表达式的内置对象--%>
    用户名:${requestScope.username}
    年龄:${applicationScope.age}
    性别:${sessionScope.gender}
    爱好:${pageScope.hobby}
    <br>
    
    <%--EL表达式支持简单的运算以及调用简单的方法,但是我们一般不会在EL表达式里做运算--%>
    ${5 * 3 + 1}
    ${3 > 2}
    ${"abcdefg".length()}
</body>
</html>

在这里插入图片描述

2、取出域中复杂数据

<%@ page import="java.util.ArrayList" %>
<%@ page import="org.westos.pojo.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.LinkedList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String[] strs = new String[]{"Hello", "World"};
        request.setAttribute("key", strs);
        /*存储数组*/
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(13);
        session.setAttribute("key", list);
        /*存储集合*/
        User user = new User("jack", 22);
        pageContext.setAttribute("key", user);
        /*存储对象*/
        HashMap<String, String> map = new HashMap<>();
        map.put("id", "001");
        map.put("name", "jenny");
        application.setAttribute("key", map);
        /*存储Map的双列集合*/
        LinkedList<User> users = new LinkedList<>();
        users.add(new User("张三", 23));
        users.add(new User("李四", 24));
        application.setAttribute("key1", users);
        /*存储对象的集合*/
        HashMap<String, User> hashMap = new HashMap<>();
        hashMap.put("one", new User("王五", 25));
        hashMap.put("two", new User("赵六", 26));
        request.setAttribute("key1", hashMap);
    %>

    ${requestScope.key[0]}
    ${requestScope.key[1]}
    <br>
    ${sessionScope.key[0]}
    ${sessionScope.key[1]}
    <br>
    ${pageScope.key.username}
    ${pageScope.key.age}
    <br>
    ${applicationScope.key.id}
    ${applicationScope.key.name}
    <br>
    ${applicationScope.key1[0].username}
    ${applicationScope.key1[1].age}
    <br>
    ${requestScope.key1.one.username}
    ${requestScope.key1.one.age}
    ${requestScope.key1.two.username}
    ${requestScope.key1.two.age}
</body>
</html>

在这里插入图片描述

3、Cookie、pageContext

<%@ page import="org.westos.pojo.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        User user = new User();
        request.setAttribute("user", user);
        request.setAttribute("key", null);

        ArrayList<String> strings = new ArrayList<>();
        session.setAttribute("key", strings);

        Cookie cookie = new Cookie("username", "john");
        response.addCookie(cookie);
    %>

    ${empty requestScope.user}
    ${empty requestScope.key}
    <%--判断对象是否为null--%>

    ${empty sessionScope.key}
    <%--判断集合的长度是否为0--%>

    ${cookie.username.name}
    ${cookie.username.value}
    <%--EL表达式中的cookie内置对象--%>

    <%--在JSP页面或者HTML页面写action或者href属性,动态获取项目上下文路径--%>
    ${pageContext.request.contextPath}
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值