JavaWeb Express JSP

Table of contents

1. Quick Start with JSP

        1.Basic introduction: 

        2. Operating principle: 

2. JSP syntax

        1.page command: 

        2. Statement script: 

        3. Expression script: 

        4.Java code script: 

        5.JSP comments: 

3. JSP object

        1.Nine built-in objects: 

        2. Four major domain objects: 

            1° Basic introduction

            2° Application examples

        3. Regarding request forwarding labels: 


1. Quick Start with JSP

        1.Basic introduction: 

        (1) JSP stands for Java Server Pages , which is Java server pages; JSP is a server rendering technology based on Servlet. It can be understood that JSP is the packaging of Servlet .

        (2) HTML can only provide users with static data, while JSP allows Java code to be embedded in the page to provide users with dynamic data . Note: JSP pages cannot be run directly through the browser like HTML pages. Tomcat must be started first and then accessed through the URL in the browser address bar .
        (3) It is difficult for Servlet to format data, while JSP can easily format data while using Java code to generate dynamic data.

        2. Operating principle: 

        (1) The essence of a JSP page is a Servlet program (that is, it is essentially a Java program) , and its performance is related to Java.

        (2) When accessing JSP for the first time, the Tomcat server will first parse the JSP page into a .java source file, and further compile it into a .class bytecode file. What is actually executed is the .class bytecode program . eg: index.jsp ---> index_jsp.java and index_jsp.class.

                As shown below: 


2. JSP syntax

        1.page command: 

        language indicates the language file after JSP parsing. Currently, only java language is supported.
        contentType represents the data type returned by JSP, corresponding to the parameter value in response.setContentType() in the source code.
        3°  pageEncoding attribute, indicating the character set of the current JSP page file itself.
        The use of the import attribute is similar to that in Java source code, and can be used to import packages and classes.

        2. Statement script: 

        The format of the declaration script is: <%! java declaration%> ; you can use Java code to define the properties, methods, static code blocks and internal classes required by JSP in the declaration script, that is, define members for the java class corresponding to the JSP file .

        The demo.jsp code is as follows: 

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP demo</title>
</head>
<body>
    <h1>demo.jsp</h1>
    <%!
        private String name;
        private int age;
        private String gender;
        private static String color;

        public String getName() {
            return this.name;
        }
        public int getAge() {
            return this.age;
        }
        public String getGender() {
            return this.gender;
        }
        static {
            color = "Cyan";
        }
    %>
</body>
</html>

        The corresponding generated .java file is shown below: 

       3. Expression script: 

        (1) The format of the expression scriptis: <%=expression%>
        (2) The function of the expression script is: output data on the JSP page
        (3) Notes on the expression script: the expression cannot end with a semicolon .

        expression.jsp code is as follows: 

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Expression</title>
</head>
<body>
    <%
        String name = "Cyan_RA9";
    %>
    public : <%=141 + 130%> <br/>
    name : <%=name%> <br/>
    job : <%="Java Full Stack Developer"%>
</body>
</html>

       running result: 

        4.Java code script: 

        The syntax formatof code script : <%java code%> .
        The role of code scripts : Use Java code in JSP pagesto complete business requirements.
        Multiple code script blocks can be combined into a complete Java statement, that is, multiple Java code scripts are allowed .
        Code scripts can also be used in combination with expression scripts to output data on JSP pages.

        First define a JavaBean class. The Student class code is as follows: 

package script;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class Student {
    private String name;
    private int age;
    private String gender;
    
    public Student() {
    }
    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

                In the javacode.jsp page, use the List collection to store information on several Student objects.
                The javacode.jsp code is as follows: 

\<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="script.Student" %> <%--自动导包--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Java Code script</title>
</head>
<body>
    <%
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("Cyan", 21, "male"));
        students.add(new Student("Five", 22, "female"));
        students.add(new Student("Ice", 20, "male"));
    %>
    <table bgcolor="#e0ffff" border="2px" bordercolor="pink" cellspacing="0" width="300px">
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
        </tr>
    <%
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
    %>
        <tr>
            <td><%=student.getName()%></td>
            <td><%=student.getAge()%></td>
            <td><%=student.getGender()%></td>
        </tr>
    <%
        }
    %>
    </table>
</body>
</html>

        operation result: 

        5.JSP comments: 

        JSP not only supports HTML format comments, but also supports its own unique comment format: <%-- --%> .

        In addition, Java line comments and paragraph comments can also be used in JSP Java code scripts.

        note.jsp code is as follows: 

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Note demo</title>
</head>
<body>
<!-- 这是HTML格式的注释√ -->
<%
    //这是Java代码的行注释;
    /*
        这是Java代码的段注释。
     */
%>
<%-- 这是JSP特有的注释格式√ --%>
</body>
</html>

3. JSP object

        1.Nine built-in objects: 

        1> JSP built-in objects (referring to already created objects, you can use [inbuild] directly), that is, the nine objects provided internally by Tomcat after parsing the JSP page into a Servlet, are called built-in objects.
        2> Built-in objects can be used directly without manual definition.

        3> The nine built-in objects are as follows——

                ① out outputs data to the client, out.println("");
                ② request handles HTTP requests from the client
                ③ response is related to the HTTP response sent back
                ④ session session object
                ⑤ application corresponds to ServletContext
                ⑥ pageContext The context of the JSP page is a domain object. You can use the setAttribue() method, but the scope is only this page.
                ⑦ exception exception object, getMessage()
                ⑧ page represents the JSP instance itself
                ⑨ config corresponds to ServletConfig
        4> The relationship diagram between the nine built-in objects and Servlet——

        2. Four major domain objects: 

            1° Basic introduction

        1>  pageContext domain object, the data stored can only be used on the current page.

        2>  Request domain object, the data stored is valid in a Request request (eg: request forwarding).

        3>  Session domain object, the data stored is valid in one session ( sessionId ).

        4>  Application domain object, the data stored is valid during the entire running of the web application, and the scope is larger.

        Area of ​​influence: pageContext < request < session < application.

            2° Application examples

                scope.jsp code is as follows: 

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Scope</title>
</head>
<body>
<%-- 由于四个域对象是不同的对象,因此允许出现相同的name。 --%>
<%
    pageContext.setAttribute("color", "cyan");
    request.setAttribute("color", "pink");
    session.setAttribute("color", "cornflower");
    application.setAttribute("color", "lightblue");

    //请求转发
//    RequestDispatcher requestDispatcher = request.getRequestDispatcher("scope2.jsp");
//    requestDispatcher.forward(request, response);

    //请求重定向
    ServletContext servletContext = config.getServletContext();
    String contextPath = servletContext.getContextPath();
    response.sendRedirect(contextPath + "/scope2.jsp");
%>
<h1>四个域对象,在本页面获取数据的情况</h1>
pageContext-color : <%=pageContext.getAttribute("color")%>  <br/>
request-color : <%=request.getAttribute("color")%> <br/>
session-color : <%=session.getAttribute("color")%> <br/>
application-color : <%=application.getAttribute("color")%> <hr/>
</body>
</html>

               Running effect : (This page accesses domain objects)

               The scope2.jsp code is as follows: (External page accesses domain objects)

<%--
    User : Cyan_RA9
    Version : 21.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>scope2</title>
</head>
<body>
<h1>在scope2页面获取数据的情况:</h1>
pageContext-color : <%=pageContext.getAttribute("color")%>  <br/>
request-color : <%=request.getAttribute("color")%> <br/>
session-color : <%=session.getAttribute("color")%> <br/>
application-color : <%=application.getAttribute("color")%> <hr/>
</body>
</html>

                Operation effect: ( scope request forwarding )

                Running effect: ( scope request redirection

        3. Regarding request forwarding labels: 

        <jsp:forward page="URL"></jsp:forward>

        Note:  1> The URL here is parsed on the server side.

                   2> The request forwarding tag is often used on an entry page to forward to other functional resources.

Guess you like

Origin blog.csdn.net/TYRA9/article/details/132207077