JSP learning notes

1. JSP overview

1.1. What is jsp? (what)

JSP page is essentially a Servlet program. When visiting JSP page for the first time (after running Tomcat server, enter the path in the browser address bar), Tomcat server will translate this JSP page into a Java source file and compile it into class bytecode file.
*So jsp is a java class in a sense. The view method of java class is shown in (detailed explanation 2).

1.2. Why use jsp? (why)

For our buddies who are beginning to learn the web, we have an idea that the page and content can be displayed in the browser. This page is not fixed (it changes with the change of background management content). Previously, we learned that the page written in html is fixed and written by the front-end programmer from the beginning. So we should receive the background data dynamically, We used the knowledge of jsp.

Here's the point: the main function of JSP is to return the data of HTML page instead of Servlet program

1.3. How to use jsp? (how)

  1. web directory (or other) right click -- > New -- > JSP / jspx -- > Enter file name -- > select JSP file creation (idea creation)
  2. web directory (or other) right click -- > shortcut (Ctrl+N) – > search JSP -- > create (eclipse)

1.4. jsp knowledge points

Having said so much, we have to summarize these knowledge points.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--1.Declare class properties--%>
    <%!
        private String name;
        private static Map<String, Object> map;
    %>
    <%--2.Declare class methods--%>
    <%!
        public int sum() {
            return 12;
        }
    %>
    <%--3.Declare static code blocks--%>
    <%!
        static {
            map = new HashMap<String, Object>();
            map.put("key1", "value1");
        }
    %>
</body>
</html>

We can see that there are html code and java code in the jsp page, so we can understand it as an enhanced version of the jsp page.

  1. The first line of the page is the header and so on. Write this instruction to indicate that the file is a jsp file. There are many attributes in the page, which are reflected in the following (detailed explanation I).
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  1. Script in jsp

Declaration script:
Format: <%! Declare Java code% >
Function: you can define attributes, methods, static code blocks, internal classes, etc. for Java classes translated from JSP
Features: it will not be displayed on the browser page, but only exists in the translated Java class

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--1.Declare class properties--%>
    <%!
        private String name;
        private static Map<String, Object> map;
    %>
    <%--2.Declare class methods--%>
    <%!
        public int sum() {
            return 12;
        }
    %>
    <%--3.Declare static code blocks--%>
    <%!
        static {
            map = new HashMap<String, Object>();
            map.put("key1", "value1");
        }
    %>
</body>
</html>

Expression script:
Format: <% = expression% >
Function: output data on the JSP page of the browser (only this script can output data on the page of the browser)
characteristic:
(1) All expression scripts will be translated to the corresponding Java classes_ jspService() method, so the expression script can be used directly_ Object in jspService() method parameter
(2) Expression scripts will be compiled in the Java class out The print () method outputs to the browser page
(3) An expression in an expression script cannot end with a semicolon

<%=22 %> <br/>
<%="You can output strings" %> <br/>
<%=map %> <br/>
<%--use_jspService Object in method--%>
<%=request.getParameter("username") %>

Code script:
Format: <% Java statement% >
Function: you can write the required Java code in the JSP page
characteristic:
(1) Code scripts are translated in_ jspService method, so the code script can directly use the object in this method parameter
(2) Multiple code script blocks can be combined to complete a complete Java statement
(3) Code scripts can also be combined with expression scripts to output data on JSP pages

<%--1.if sentence--%>
<%
    int i = 1;
    if (i == 1) {
        System.out.println("I love my motherland!");
    } else {
        System.out.println("I love my motherland very much!");
    }
%> <br/>
<%--2.for Circular statement--%>
<%
    for (int j = 0 ; j < 3; j++) {
        System.out.println("The first" + j + "Secondary cycle");
    }
%> <br/>
<%--3.use_jspService Objects in method parameters--%>
<%
    String username = request.getParameter("username");
    System.out.println("username The corresponding values are:" + username);
%>

  1. Nine built-in objects in jsp

The built-in objects of JSP refer to the nine internal objects provided by Tomcat server after translating JSP pages into Java classes:

  1. Request: request object
  2. Response: response object
  3. pageContext: context object of JSP
  4. Session: session object
  5. application: ServletContext object
  6. config: ServletConfig object
  7. out: JSP output stream object
  8. page: object pointing to the current JSP
  9. Exception: exception object
  1. jsp's four domain objects:

Domain object refers to the object that can access data like Map. The functions of the four domain objects are the same, but the access range of data is different.
Note: if the range of the four domain objects can meet the requirements, the priority of use is (from small to large):
pageContext --> request --> session --> application

  1. out output of jsp:

Since the bottom layer of the translated Java code in the official code is output using out, it is generally output using out. Out is divided into write method and print method:
(1) out.print(): after converting any content into a string, call the write method output.
(2) out. The problem of converting the output of character string from character string to character string is not that of character string from character string to character string, but that of character string from character string to character string
Conclusion: any content to be output in the browser in the code script of JSP page should use out Print() method

  1. Introduction of jsp tag

When we write the code, we will find that there are many duplicate codes: such as links and contact us. We will write them on many pages. What if we want to simplify it? In this way, we use the introduction of jsp.

<body>
    Header information <br>
    Subject information <br>
    <%@include file="/foot.jsp"%>
</body>

2. JSP code writing

How many jsp exercises will be written here?

Define a = 1 and B = 2, and output the results on the jsp page.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%! int a = 1;%>
    <%! int b = 2;%>
    The results are:<%= a+b%>
</body>
</html>

Use the for loop to output i like pig ten times.

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

    The results are:<% for (int i=0; i<10; i++){
                System.out.println("i like pig");
            }
    %>
</body>
</html>

3. Explain in detail

Detailed explanation 1:

  1. contentType attribute: indicates the data type returned by JSP, i.e. response Parameter value of setcontenttype()
  2. Language attribute: indicates the language file after JSP translation (currently only Java is supported)
  3. pageEncoding attribute: indicates the character set of the current JSP file itself (which can be seen in the lower right corner of the IDEA)
  4. Import attribute: indicates the import package (import class), which is consistent with Java
  5. autoFlush property: sets whether to automatically refresh the out output stream buffer when it is full. The default value is true
  6. Buffer attribute: sets the size of the out buffer, which is 8kb by default
    Note: if the out buffer cannot be refreshed automatically after it is full, an error will be reported
  7. errorPage attribute: set the path of the page (error information page) that will automatically jump to when the JSP page runs in error. This path usually starts with a slash, indicating that the requested address is http://ip:port/ Project path /, corresponding code web directory
  8. isErrorPage property: set whether the current JSP page is an error information page. The default is false. If it is true, you can get the error information
  9. session attribute: sets whether an HttpSession object will be created when accessing the current JSP page. The default value is true
  10. Extensions attribute: set the default inheritance of Java classes translated from JSP pages

Detailed explanation 2:
How to view the translated Java source file: start the Tomcat server to access the JSP page, and then find using Catalina in the front end of the information output from the console_ The path in base, open this directory in the hard disk, click work -- > Catalina -- > localhost, and find the corresponding project folder.

This document refers to some csdn articles. Finally, thank you!

Keywords: SSM

Added by visitor on Mon, 07 Mar 2022 20:58:33 +0200