• 7.JSP基础语法,指令和标签以及Java Bean


    一.JSP的基础语法

     1 <%--1.JSP表达式
     2     作用:用于将程序的结果,输出到客户端
     3     语法:
     4     <%=变量或者表达式 %>
     5 --%>
     6 
     7 <%= new java.util.Date()%>
     8 
     9 <br>
    10 
    11 <%--2.SP脚本片段--%>
    12 <%
    13     int sum = 0;
    14     for (int i = 0; i < 100; i++) {
    15         sum += i;
    16     }
    17     out.print("<h1>Sum=" + sum + "</h1>");
    18 %>
    19 
    20 <%--3.在代码中嵌套HTML元素--%>
    21 <%
    22     for (int i = 0; i < 5; i++) {
    23 %>
    24 <h3>hello world <%=i%></h3>
    25 
    26 <%
    27     }
    28 %>
    29 
    30 <%--4.JSP声明:提升作用域到全局--%>
    31 <%!
    32     static {
    33         System.out.println("Loading Servlet");
    34     }
    35 
    36     private int globalVar =0;
    37 
    38     public void func(){
    39         System.out.println("方法func");
    40     }
    41 %>
    42 
    43 <%--5.JSP注释:不会在浏览器的查看源代码中显示,HTML的注释会在浏览器的查看源代码中显示--%>

    二.JSP指令

    1.手动指定错误页面和使用配置文件web.xml指定错误页面

    1 <%-- 可以手动指定错误页面,也可以在web.xml中配置指定错误页面<%@ page errorPage="error/500.jsp" %>--%>
    1 <error-page>    
    2   <error-code>404</error-code>
    3   <location>/error/404.jsp</location>
    4 </error-page>
    5 
    6 <error-page>
    7   <error-code>500</error-code>
    8   <location>/error/500.jsp</location>
    9 </error-page>

    404.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>404</title>
     5 </head>
     6 <body>
     7 
     8 <img src="../img/404.jpg" alt="404">
     9 </body>
    10 </html>

    500.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 
     3 <html>
     4 <head>
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 
     9 <img src="../img/500.png" alt="500">
    10 </body>
    11 </html>

    2.指定嵌入页面

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>嵌入头部和尾部页面</title>
     5 </head>
     6 <body>
     7 
     8 <%--嵌入页面方法一:使用include--%>
     9 <%--不会将页面拼接,直接将文件中的内容放在一起,会导致各自页面中的变量可能同名而冲突--%>
    10 <%@include file="common/header.jsp"%>
    11 <h1>页面主体</h1>
    12 <%@include file="common/footer.jsp"%>
    13 
    14 <%--嵌入页面方法二:使用jsp:include标签 (推荐使用)--%>
    15 <%--会将页面拼接,各自页面中的变量互不干扰--%>
    16 <jsp:include page="common/header.jsp"></jsp:include>
    17 <h1>页面主体</h1>
    18 <jsp:include page="common/footer.jsp"></jsp:include>
    19 
    20 </body>
    21 </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大内置对象

    1. PageContext  存东西
    2. Request
    3. Response
    4. Session   存东西
    5. Application [ServletContext] 存东西
    6. config  [ServletConfig]
    7. out
    8. page
    9. exception
     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>Title</title>
     5 </head>
     6 <body>
     7 
     8 <%--内置对象--%>
     9 <%
    10 
    11     //作用域:page-->request-->session-->application 从小到大
    12     pageContext.setAttribute("name1","王1"); //保存的数据只在一个页面中有效
    13     request.setAttribute("name2","王2");//保存的数据只在请求中有效,但是如果使用请求转发则可以携带数据
    14     session.setAttribute("name3","王3");//保存的数据只在一次会话中有效,关闭浏览器后数据丢失
    15     application.setAttribute("name4","王4");//保存的数据只在服务器有效,关闭服务器数据丢失
    16 %>
    17 
    18 <%
    19     String name1 = pageContext.findAttribute("name1").toString();
    20     String name2 = pageContext.findAttribute("name2").toString();
    21     String name3 = pageContext.findAttribute("name3").toString();
    22     String name4 = pageContext.findAttribute("name4").toString();
    23     String name5 = pageContext.findAttribute("name5").toString(); //不存在的值
    24 
    25     //前台使用pageContext请求转发,后台使用则使用request
    26     pageContext.forward("/index.jsp");
    27     //等价于
    28     //request.getRequestDispatcher("/index.jsp").forward(request,response);
    29     
    30     //pageContext中可以设置scope作用域参数
    31 //    public static final int PAGE_SCOPE = 1;
    32 //    public static final int REQUEST_SCOPE = 2;
    33 //    public static final int SESSION_SCOPE = 3;
    34 //    public static final int APPLICATION_SCOPE = 4;
    35     pageContext.setAttribute("hello1","你好1",PageContext.SESSION_SCOPE);
    36     //等价于
    37 //    session.setAttribute("hello1","你好1");
    38     
    39 %>
    40 
    41 <%--使用EL表达式将值输出: ${}--%>
    42 
    43 <h3>${name1}</h3>
    44 <h3>${name2}</h3>
    45 <h3>${name3}</h3>
    46 <h3>${name4}</h3>
    47 <h3>${name5}</h3>
    48 
    49 </body>
    50 </html>

    request:用于用户看完就没有用的数据,例如:新闻

    session:购物车案例中使用(一个人使用)

    application:聊天数据使用(多人使用)

    四.JSP标签、JSTL标签、EL表达式

    1.导入包:

     1 <!--JSTL表达式依赖-->
     2 <dependency>
     3   <groupId>javax.servlet.jsp.jstl</groupId>
     4   <artifactId>jstl-api</artifactId>
     5   <version>1.2</version>
     6 </dependency>
     7 
     8 <!--standard标签库-->
     9 <dependency>
    10   <groupId>taglibs</groupId>
    11   <artifactId>standard</artifactId>
    12   <version>1.1.2</version>
    13 </dependency>

    2.EL表达式:${}

    • 获取数据
    • 执行运算
    • 获取web开发常用对象
    • 调用Java方法

    3.JSP标签:

    jsptag1.jsp:

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>jsptag1</title>
     5 </head>
     6 <body>
     7 
     8 <h1>jsptag1</h1>
     9 <%--jsp:include 用于嵌入页面--%>
    10 
    11 <%--jsp:forward 用于请求转发--%>
    12 <jsp:forward page="jsptag2.jsp">
    13     <jsp:param name="name" value="wang"></jsp:param>
    14     <jsp:param name="age" value="24"></jsp:param>
    15 </jsp:forward>
    16 
    17 
    18 </body>
    19 </html>

    jsptag2.jsp:

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>jsptag2</title>
     5 </head>
     6 <body>
     7 
     8 <h1>jsptag2</h1>
     9 <%--取出参数--%>
    10 名字:<%=request.getParameter("name")%>
    11 年龄:<%=request.getParameter("age")%>
    12 </body>
    13 </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

  • 相关阅读:
    汇编 gdb调试
    汇编 运行错误解决办法
    binutils安装
    汇编错误
    汇编
    pthread_create()
    error2019-01-17 宏STDOUT_FILENO
    2019-01-17 跳至首行
    .NET MVC 两种视图引擎(Razor、Aspx)
    ECharts属性设置
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12315344.html
Copyright © 2020-2023  润新知