Servlet is troublesome to generate dynamic web pages, so in order to simplify development, JSP was introduced;
3. JSP operating principle
When JSP is executed, it will be translated into Servlet and executed by the server. JSP is a Servlet;
4. JSP script elements
<%! %>: JSP declaration, translated into the content of the Servlet member part; declare variables, methods, and internal classes;
<%= %>: Translate out.print(), inside the Servlet method; used to generate HTML page source code;
<% %>: Embedded Java code, translated into code blocks inside the service method; declare variables and internal classes;
Two, JSP template modification and annotation
1. Modification of JSP template
Modify the character set encoding of JSP
2. JSP comments
HTML comments <!-- HTML 的注释 --> are stored in the source code of JSP, and comments will also exist after being translated into Servlet, and after the generated HTML is responded to the page, such comments will also exist;
The comments of the Java code <% //单行注释 %> <% /* 多行注释 */ %> <% /** 文档注释 */ %> exist in the source code of the JSP, and the comments will also exist after being translated into Servlet, but after the HTML response is generated to the page, the comments disappear;
JSP annotations <%-- JSP 的注释 --%> exist in the source code of JSP, and the annotations disappear after being translated into Servlet;
Three, an overview of the instruction elements of JSP
1. The role of JSP instruction elements
Used to indicate certain steps performed by the JSP;
Used to instruct JSP to realize specific behavior;
2. JSP instruction element syntax
<%@ 指令名称 属性名称=属性的值 属性名称=属性的值 %>
3. Classification of JSP instruction elements
page instruction: instructs the page setting attributes and behavior of JSP;
include directive: Instruct the JSP to include which other pages;
taglib instruction: instruct which tag libraries the JSP page contains;
Four, JSP instruction-page instruction
<%@ page attribute="value" %>
The Page instruction provides instructions for the container to use the current page;
A JSP page can contain multiple page instructions;
In the JSP page, only the import attribute can appear multiple times, and all other attributes can only appear once;
Five, JSP instructions-include and taglib instructions
1. Include Directive
<%@ include file="relative url" %>
JSP can include other files through the include directive; the included files will be compiled and executed at the same time;
2. The taglib command
<%@ taglib uri="uri" prefix="prefixOfTag" %>
JSP API allows users to customize tags. A custom tag library is a collection of custom tags.
The Taglib directive introduces a definition of a custom tag set, including library paths and custom tags.
Six, JSP built-in objects
1. What are JSP built-in objects
JSP built-in objects: refer to objects that can be used directly in JSP pages;
2. What are the built-in objects of JSP
Object name
effect
Specific type
request
The request object sent from the client to the server
HttpServletRequest
response
Responding object from server to client
HttpServletResponse
session
The server creates a session for the client
HttpSession
application
On behalf of the application, the obtained ServletContext object
ServletContext
out
Object that writes content to the output stream
ServletConfig
page
Reference to the object after the current JSP is translated into Servlet
Object
pageContext
The context object of the current JSP page
PageContext
config
ServletConfig object of this JSP
JspWriter
exception
Represents the object that generates an exception when the JSP page is running
Throwable
Seven, an overview of the pageContext object
1. Introduction to pageContext object
pageContext: page context object, which represents some attributes of the current page running;
2. The role of the pageContext object
Provides data access methods in the page range;
Obtain other 8 built-in objects through this object;
Eight, the four scopes of JSP
PageScope: The page scope refers to the valid in the current page, out of this page, saving data with pageContext is invalid;
RequestScope: Request scope: Send a request from the client to the server. After the server responds to this request, the data saved with the request is invalid;
SessionScope: Session scope. Each browser sends a request (multiple requests) to the server to end the session;
ApplicationScope: Application scope can be obtained anywhere in the entire application;
Nine, JSP action tags
1. What is JSP action tag
JSP action tags are used to provide business logic functions in JSP pages to avoid directly writing java code in JSP pages, which makes jsp pages difficult to maintain;