The basic syntax of JSP

  1. <% %> Can write Java code in the middle
  2. The < % -- -- % >JSP comments can be written in the middle: cannot be displayed on the client side

    <! -- -->In the middle can write comments HTML comments: can look at the client display JSP comments shortcut keys:ctrl+/
  3. < % = % >“=” is for the out. Println ()

instruction

  1. Page directive: A page directive that defines the global properties of a JSP page. This configuration applies to the entire JSP page. The page directive is used to specify the scripting language to use, import specified classes and packages, and so on.
  • This is a JSP without the page directive
< % =new java.util.Date()%>
Copy the code
  • This is a JSP with the page directive
<%@ page import="java.util.*"% > < % =new Date()%>
Copy the code
  1. Include directive: file loading directive, used to insert a file containing text or code into a JSP file. It inserts the file and merges it with the original JSP file into a new JSP page. Also note that if the inserted file changes, the JSP file that contains it needs to be recompiled. This is often used as a top bar or a bottom bar on a web page, not very often.
  2. The taglib directive: References the tag library and sets the prefix of the tag library. This directive allows JSP pages to use user-defined tags, and it can also name the tag library in which the tags are defined.
Form: <% @taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:out value="This is a taglib tag!"/> <%-- prefix specifies the prefix used by the tag library. --%> <%-- URI specifies the location of the tag file or tag library. - % >Copy the code

JSP statement

  1. The < %! Global variables, static code blocks, and methods can be declared in the middle
The < %!static{
        static {
        System.out.println("This is a static code block.");
      }
      private int i = 10;// Global variables
      public void study(a){
        System.out.println("That's one way.");
      }
    }
%>
Copy the code
  1. Note: In reality, JSP pages will eventually compile into Servlet classes, and only one instance of the Servlet class will exist in the container. A variable declared in a JSP is a member variable that is initialized only once when the Servlet instance is created, and is held thereafter until the Servlet instance is destroyed
  2. Let me give you a little example
<! --JSP declaration statement --> <%!public int a;// Declare the integer variable a

    public void printStr(a) { // Declare method printStr
        System.out.println("This is a method");
    }
%>
<%
    out.println("a=" + a); // Outputs a value
    a++;/ / a since
    out.println("a=" + a);
%>
<br>
<%
    printStr();// Call printStr()% >Copy the code

JSP action elements

  1. < JSP :include > provides a way to include pages in JSP pages, either static or dynamic
<%-- index.jsp --%>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <%="hello"%>

  </body>
</html>
Copy the code
<%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> JSP :include</title> </head> <body> <h1> title </h1> < JSP :include page="index.jsp"></jsp:include>>

</body>
</html>
Copy the code

2. < jsp:forward >Action element for page redirection, which stops the execution of the current page and forwards the client request to another JSP page. This operation is performed on the server and does not cause a second request from the client. Therefore, the browser address does not change.

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

  <%="hello"%>

  </body>
</html>
Copy the code
<%@ page contentType="text/html; charset=UTF-8" language="java"%> < HTML > <head> <title> JSP :include</title> </head> <body> <h1> title </h1> < JSP :forword page="index.jsp"></jsp:include>>
</body>
</html>
Copy the code

3. < jsp:param >An accessory action element that provides parameters, in the form of name-value pairs, to provide additional information to other action elements, commonly used in conjunction with < JSP :include >, < JSP :forward >, and < JSP :plugin >.

  1. The < JSP: Plugin > action element downloads server-side Javabeans and applets to be executed on the client side.
  2. The < JSP :useBean > action element is used to load a JavaBean to be used in a JSP page. This feature is very useful because it takes advantage of the reuse of Java components while improving the ease of using JSPS.
  3. After the < JSP :setProperty > gets the Bean instance, you can use this action element to set and modify the property values in the Bean.
  4. The < JSP :getProperty > action element is used to extract the value of the property in the Bean, convert it to a string, and output it.

Nine built-in objects

  1. PageContext stores things (saved data is only valid in one page)
  2. Request stores something (stored data is only valid for a single Request, and Request forwarding carries this data)
  3. Response
  4. Session stores things (stored data is only valid for one Session, from opening the browser to closing the browser)
  5. Application [ServletContext] store things (stored data is only valid in the server, from opening to closing the server)
  6. The config [ServletConfig]
  7. out
  8. Page, don’t understand
  9. exception

Request: The client sends a request to the server. The data generated by the request is useless after the user finishes reading it, such as news

Session: The client sends a request to the server, generating data that the user can use later, such as shopping cart;

Application: The client sends a request to the server. The data generated by the request is used up by one user and may be used by other users, such as chat data.

PageContext, Request, Session, and Application can store things, but their scope is different, from low to high: ageContext–>Request–>Session–>Application