What can JSP technology do?

1. JSP

Write your first JSP

Goal: Master the writing of JSP files

1. Create a Web project

Create Java Enterprise, add Web Application, Please add a picture description
set the project name and save location, Please add a picture description
and click the [Finish] buttonPlease add a picture description

2. Modify the Artifact name and redeploy the project

Modify the Artifact name in the project structure window In Please add a picture description
the service period configuration window, redeploy the project Please add a picture description
Switch to the [Server] tab and set the default browserPlease add a picture description

3. Create a welcome JSP page

Create welcome.jsp in the web directory Note
Please add a picture description
Please add a picture description
Please add a picture description
: According to the welcome.jsp page, the newly created JSP file is almost the same as the traditional HTML file. The only difference is that when it is created by default, there is an extra page instruction at the top of the page code, and the file extension is jsp instead of html.
Modify welcome.jspPlease add a picture description

4. Start the server and view the results

Start the server and visit http://localhost:8080/JSPDemo/welcome.jsp Please add a picture description
Note: The content added in the tag of welcome.jsp has been displayed, which means that HTML elements can be parsed by the JSP container. In fact, JSP just adds some Java-specific codes to the original HTML file, and these are called JSP syntax elements.
Classroom exercise: modify the home page, as shown in the figure belowPlease add a picture description

Two, JSP basic syntax

Basic composition of JSP pages

Goal: Familiar with the basic composition of JSP pages

1. JSP page structure

Although the JSP file has been created, the page composition of the JSP file has not been introduced in detail. A JSP page may include content such as instruction mark, HTML code, JavaScript code, embedded Java code, annotation and JSP action mark.

2. Case demonstration - display the current time of the system

Create time_info.jsp page
Please add a picture description

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP页面 - 显示系统当前时间</title>
    </head>
    <body>
        <!--JSP脚本元素-->
        <%
            Date date = new Date();
            // year, month, day, hour, minute, second
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");  
            String currentTime = sdf.format(date);
        %>
        <!--JSP表达式元素-->
        <h3 style="text-align: center">系统当前时间:<%= currentTime%></h3>
    </body>
</html>

Page composition diagram
Please add a picture description
Start the server and visit http://localhost:8080/JSPDemo/time_info.jspPlease add a picture description

JSP script element

1、JSP Scriptlet

Case presentation

Create demo01.jsp page
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP Scriptlets</title>
    </head>
    <body>
        <%
            int a = 100, b = 150; // 定义两个整型变量
            int sum = a + b; // 计算两个整数之后
            // 利用JSP隐式对象`out`输出结果
            out.print(a + " + " + b + " = " + sum);
        %>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo01.jspPlease add a picture description

Please add a picture description

Case demonstration
Create demo02.jsp page

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP声明标识</title>
    </head>
    <body>
        <%!
            // 定义阶乘函数
            public long factorial(int n) {
                long jc = 1;
                for (int i = 1; i <= n; i++) {
                    jc = jc * i;
                }
                return jc;
            }
        %>
        <%
            // 输出10的阶乘值
            out.println("10! = " + factorial(10));
        %>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo02.jspPlease add a picture description

Case presentation

Create demo03.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP表达式</title>
    </head>
    <body>
        <%!
            int a = 100, b = 150; // 声明两个整型变量
        %>
        sum = <%= a + b%> <!--JSP表达式-->
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo03.jspPlease add a picture description

Case presentation

Create demo04.jsp page
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>带有JSP表达式的注释</title>
    </head>
    <body>
        <%!
            /**
             * @param a
             * @param b
             * @return a + b
             */
            public int sum(int a, int b) {
                /*
                 * a, b都是形式参数
                 */
                return a + b; // 返回两个整数之和
            }
        %>
        <%
            int a = 100, b = 150; // 定义两个整型变量
            int sum = sum(a, b); // 调用求和函数计算两个整数之和,此处a,b是实际参数
            out.print("sum = " + sum); // 输出求和结果
        %>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo04.jspPlease add a picture description

) Case Demo

Create demo05.jsp page
Please add a picture description

<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP注释</title>
    </head>
    <body>
        <h1>学习JSP元素</h1> <!--HTML注释:一级标题显示-->
        <%= new Date()%> <%--JSP注释:用JSP表达式元素显示当前日期--%>
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo05.jsp, Please add a picture description
click the right mouse button on the opened page, and select the "View Webpage Source Code" option in the pop-up menuPlease add a picture description

Case presentation

Create demo06.jsp page

<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>动态注释</title>
    </head>
    <body>
        随机数:<%= Math.random() %> <!-- 求随机数,当前日期:<%=new Date()%> -->
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo06.jsp, Please add a picture description
click the right mouse button on the opened page, and select the "View Webpage Source Code" option in the pop-up menuPlease add a picture description

Case presentation

Except for the import attribute among the common attributes of the page directive, other attributes can only appear once, otherwise the compilation will fail.
The page error.jsp that jumps after creating an errorPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
    <head>
        <title>错误页面</title>
    </head>
    <body>
        <h3 style="text-align: center">错误信息:<%= exception.getMessage() %></h3>
    </body>
</html>

Create demo07.jsp pagePlease add a picture description

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>JSP页面 - 显示系统时间</title>
    </head>
    <body>
        <!--JSP脚本元素-->
        <%
            Date date = new Date();
            // year, month, day, hour, minute, second
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String currentTime = sdf.format(date);
        %>
        <!--JSP表达式元素-->
        <h3 style="text-align: center">系统当前时间:<%= currentTime%></h3>
    </body>
</html>

View the page directive Please add a picture description
The code in the red box above uses the language, contentType, pageEncoding, and import attributes of the page directive. It should be noted that the page directive is valid for the entire page, regardless of where it is written, but it is customary to write the page directive at the top of the JSP page.

To start the service period, visit http://localhost:8080/JSPDemo/demo07.jsp
Please add a picture description
to modify the demo07.jsp page, and intentionally let the Java code make mistakes. Please add a picture description
To start the service period, visit http://localhost:8080/JSPDemo/demo07.jsp to display the system default error page. Please add a picture description
If demo07.jsp specifies the error page error.jsp Please add a picture description
If demo07.jsp specifies the error page error.jsp Please add a picture description
If demo07.jsp specifies the error pageerror.jspPlease add a picture description

Case presentation

Create demo08.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>欢迎页面</title>
    </head>
    <body>
        <h1 style="color: blue; text-align: center">欢迎访问JSP世界~</h1>
        <%@ include file="time_info.jsp"%>
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo08.jsp Please add a picture description
Please add a picture description
start the server, visit http://localhost:8080/JSPDemo/demo08.jsp, the content of the included page can also be displayedPlease add a picture description

Case presentation

Create a lib directory in WEB-INF and add two jar packages Please add a picture description
Add the library to the project Please add a picture description
Click【Add as Library…】Please add a picture description
Click【OK】button Please add a picture description
Create demo09.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>JSTL核心库演示</title>
    </head>
    <body>
        <c:set var="message" value="欢迎访问JSP世界~"/>
        <h1 style="text-align: center"><c:out value="${message}"/></h1>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo09.jsp Please add a picture description
If you do not use the JSTL core tag library, use JSP code to achieve the same function Please add a picture description
Start the server and visit http://localhost:8080/JSPDemo/demo09.jspPlease add a picture description

, case demonstration

Create demo10.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>被包含的页面</title>
    </head>
    <body style="text-align: center">
        <%
            Thread.sleep(5000); // 线程休眠5秒
        %>
        红豆生南国<br>
        春来发几枝<br>
        愿君多采撷<br>
        此物最相思<br>
    </body>
</html>

Create the demo11.jsp page and import the demo10.jsp page. demo10.jsp is the imported file, let it pause for 5 seconds and then output the content, so that it is convenient to test the flush attribute of the jsp:include element.Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>引入被包含的页面</title>
    </head>
    <body>
        <h3 style="text-align: center">相思</h3>
        <jsp:include page="demo10.jsp" flush="true"/>
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo11.jsp, and find that the browser first displays the output content of the demo11.jsp page, and waits for 5 seconds before displaying the output content of the demo10.jsp page, indicating that the referenced resource demo10.jsp is called after the output content of the current JSP page.
Please add a picture description
demo11.jsp page final display effectPlease add a picture description

, case demonstration

Create demo12.jsp page
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>演示请求转发元素</title>
    </head>
    <body>
        <h1 style="text-align: center">演示请求转发元素</h1>
        <%
            Thread.sleep(5000); // 线程休眠5秒
        %>
        <jsp:forward page="welcome.jsp"/> <!--转发到欢迎页面-->
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo12.jsp, and find that the browser will not display the output content of the demo12.jsp page, and will display the content of the welcome.jsp page after waiting for 5 seconds. Please add a picture description
The demo12.jsp page finally displays the effect. Although the request is forwarded to the welcome.jsp page, the address bar is still demo12.jspPlease add a picture description

Case presentation

Create demo13.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>演示out对象的用法</title>
    </head>
    <body>
        <%
            out.println("第1行:Web开发很有意思~<br />");
            response.getWriter().println("第2行:我们来学Web开发~<br />");
        %>
    </body>
</html>

Start the server, visit http://localhost:8080/JSPDemo/demo13.jsp Please add a picture description
Please add a picture description
start the server, visit http://localhost:8080/JSPDemo/demo13.jspPlease add a picture description

Case presentation

Create demo14.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>演示pageContext对象</title>
    </head>
    <body>
        <%
            // 获取out对象
            JspWriter myOut = pageContext.getOut();
            // 设置page范围内属性
            pageContext.setAttribute("message", "学习Java基础编程", pageContext.PAGE_SCOPE);
            // 设置request范围内属性
            request.setAttribute("message", "学习Java Web开发");
            // 设置session范围内属性
            session.setAttribute("message", "学习Spring Boot框架");
            // 设置application范围内属性
            application.setAttribute("message", "学习大数据实时处理");
            // 获取page范围属性
            String pageMessage = (String)pageContext.getAttribute("message", pageContext.PAGE_SCOPE);
            // 获取request范围属性
            String requestMessage = (String)pageContext.getAttribute("message", pageContext.REQUEST_SCOPE);
            // 获取session范围属性
            String sessionMessage = (String)pageContext.getAttribute("message", pageContext.SESSION_SCOPE);
            // 获取application范围属性
            String applicationMessage = (String)pageContext.getAttribute("message", pageContext.APPLICATION_SCOPE);
        %>
        <%
            myOut.println("page范围消息:" + pageMessage + "<br />");
            myOut.println("request范围消息:" + requestMessage + "<br />");
            myOut.println("session范围消息:" + sessionMessage + "<br />");
            myOut.println("application范围消息:" + applicationMessage + "<br />");
        %>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo14.jspPlease add a picture description

Case presentation

View the error.jsp page created earlier
Please add a picture description
Create a demo15.jsp pagePlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java"
    pageEncoding="UTF-8" errorPage="error.jsp" %>
<html>
    <head>
        <title>演示页面异常</title>÷
    </head>
    <body>
        <%
            int a = 10;
            int b = 0;
        %>
        <%
            out.print(a + " ÷ " + b + " = " + (a / b));  // 产生异常
        %>
    </body>
</html>

Start the server and visit http://localhost:8080/JSPDemo/demo15.jspPlease add a picture description

Guess you like

Origin blog.csdn.net/qq_64505257/article/details/130982859