In-depth understanding of JSP
JSP (Java server page) is the most basic Member of the Java EE specification. It is a key knowledge of Java Web development. Although we have been using it, we know little about its principles. Today, we will focus on the core content of JSP and its working principles.
JSP and Servlet are essentially the same, because JSP must be compiled into a Servlet to run. In other words, JSP is used to generate a draft file of Servler.
JSP is relatively simple, that is, embedding Java code in HTML or using JSP tags, including using user-defined tags to dynamically provide content. JSP is widely used in early stage. A web application can be composed of all JSP pages, which only requires a small number of JavaBean. However, this leads to the complexity of JSP duties, this is the emergence of the Java EE standard. Therefore, JSP has gradually developed into a single performance technology and is no longer responsible for business logic components and persistent layer components.
Basic Principles of JSP
JSP is essentially a servlet. When you specify a servlet to send a request, the servlet uses the output stream to dynamically generate an HTML page. It contains a large number of HTML tags. Static text and other formats lead to extremely low servlet development efficiency. All presentation logic, including layout, color, and image, must be coupled in Java code, the static part requires no Java program control. Only the page content that needs to be read from the database or dynamically generated can be controlled using Java scripts.
Therefore, the JSP page content consists of the following two parts:
Static part: HTML Tag
Dynamic part: Java Script
JSP comments
JSP comments are used to mark Development prompts during program development and are not output to the client. <% -- Comment content -- %>
JSP Declaration
JSP declarations are used to declare variables and methods. In JSP declarations, methods seem very special. You can directly define methods without defining classes. methods seem to be independent from classes. In fact, JSP declarations are converted to servlet member variables or member methods. Therefore, JSP declarations still comply with Java syntax. <%! Declaration Part %>
Output JSP expression
<% = Expression %>
JSP script
In the past, JSP was widely used. In general, any Java code that can be executed can be embedded into HTML pages through JSP scripts. <% Java Script %>
JSP three pre-compiled commands page command
This command is a command for the current page, usually located at the top of the JSP page, a JSP page can use multiple page commands.
Include command
This command is used to specify commands that contain another page. You can use the include command to embed an external file into the current JSP file and parse the JSP file on this page. Include can contain static text and dynamic JSP pages.
Taglib command
Used to define and access custom tags.
Seven JSP action commands forword commands
It is used to forward the page response to another page. It can be either forwarded to static HTML, dynamic JSP pages, or servlet in the container.
Param command
Used to execute parameters. It must be used with tags that support parameters. It cannot be used independently, because the separate param command has no practical significance.
Include command
It is used to dynamically introduce a JSP page. It is a dynamic include command. It is also used to include a page and will not import the compilation instructions of the include page, only Insert the body content of the imported page into this page.
Plugin command
It is used to download JavaBean or applet to the client for execution.
UseBean command
Create a an instance
SetProperty command
Set the attribute value of the JavaBean instance
GetProperty command
Attribute Value of the output JavaBean instance
Nine built-in object application objects in JSP scripts
An instance of javax. servlet. ServletContext. This instance represents the web application to which the JSP belongs and can be used for JSP pages or information exchange between servlets.
Config object
Javax. servlet. ServletConfig instance, which represents the configuration information of JSP
Exception object
Java. lang. Throwable instance, which represents exceptions and errors on other pages.
Out object
Javax. servlet. jsp. JspWriter instance, which represents the output stream of JSP and is used to output content to form an HTML page.
Page Object
It indicates the page itself, which is usually of little use.
PageContext object
Javax. servlet. jsp. PageContext instance. This object represents the context of the JSP page. You can use this object to access the shared data on the page.
Request object
Javax. servlet. http. HttpServletRequest instance. This object encapsulates a request, and the parameters of the client request are encapsulated in this object.
Response object
The javax. servlet. http. HttpServletResponse instance represents the server's response to the client. Generally, this object is rarely used for direct response, but the out object is used unless a non-character response is generated.
Session Object
Javax. servlet. http. HttpSession instance. This object represents a session. When the client browser is connected to the site, the session starts. when the client is closed, the session ends.