Learning Points of knowledge
JSP, which will eventually become a servlet
The JSP eventually becomes a complete servlet that runs in the Web application, except that the Servlet class is written by the container.
The Scriptlet in JSP
The so-called Scriptlet is the Java code placed in the <%...%> tag. Such as:
<%
out.println(com.inspur.Counter.getCount());
%>
Directives in the JSP
The difference between JSP directives and scriptlet is that there is one more @ sign. Note There are no spaces in the middle.
Importing a package using the page directive
<%@ page import="com.inspur.*"%>
<%
out.println(Counter.getCount());
%>
</body>
- Import multiple packages, using an import property, except that multiple packages are separated by commas
<%@ page import="com.inspur.*,java.util.*"%>
JSP expressions
Similar to the Scriptlet code, the expression has one more =. Note that there are no spaces in the middle, and that there are no semicolons at the end of the expression!
Scriptlet Code:
<% out.println(Counter.getCount());%>
Expression code:
<%= Counter.getCount()%>
Why does an expression not end with a semicolon?
The expression becomes a parameter of Out.print ()!
That is <%= Counter.getCount()%>
, the container will be converted to:
<% out.print(Counter.getCounter());%>
, if you add a semicolon, it will be brought into the outer brackets resulting in a compilation error.
In addition, out is an implicit object.
Claims in the JSP
If you declare the count variable using Java syntax:
<% int count=0;%>
In fact, it is scriptlet!
However, there is a real JSP declaration, and the scriptlet difference lies in adding one!:
<%! int count=0;%>
The difference between the above two statements:
All scriptlet and expression codes are placed in service methods, that is, variables declared in Scriptlet are always local variables!
The JSP declares the variable is the class variable, the JSP declares the method is the class Method!
How does a container handle JSP?
The following is a simple example:
An implicit object
API |
an Implicit object |
JspWriter |
Out |
HttpServletRequest |
Request |
HttpServletResponse |
Response |
HttpSession |
Session |
ServletContext |
Application |
ServletConfig |
Config |
Jspexception |
exception |
PageContext |
PageContext |
Object |
Page |
Note that PageContext encapsulates other implicit objects, even if has a pagecontext reference, and can get references to other implicit objects and get the properties of all scopes.
JSP annotations
<!-- HTML 注释 用户在浏览器就可以看到我 -->
<%-- JSP注释,页面开发人员才可能看到我 --%>
The API of the servlet generated by the JSP
Our main focus is on three key methods: Jspinit (), Jspdestroy (), and _jspservice ().
The first two methods can be overridden, and the last method cannot be overwritten, which is why the underscore begins.
You can see how the method is overridden later in this article.
The life cycle of a JSP
Note that the conversion and compilation of the JSP only happens once, similar to the other servlets, once the loading is initialized, only one thing happens when the request is created or assigned a thread to run the service method.
However, the first person to access the JSP page may have to wait a while, but the JSP specification mentions a recommended JSP pre-compilation protocol that you can look at.
Initialize JSP
Servlet initialization can be done in a JSP, but this is slightly different from the practice in a regular servlet.
- Configuring servlet Initialization Parameters
- Cover Jspinit ()
Use the Jspinit () method to get a servlet initialization parameter (the parameter already configured in DD) and use this parameter value to set an Application scope property:
Properties in a JSP
In most cases, you use one of the 4 implicit objects to get and set the properties in the 4 JSP scopes.
4 scopes? Processing the request, session, and application (context) scopes in a standard servlet, the JSP fourth scope is the page scope, which is obtained from the PageContext object.
As I said earlier, PageContext encapsulates references to other scopes, so here's an example:
- Examples of using PageContext to get and set properties:
JSP directives and Directive properties
Page is a JSP directive, import is one of its 13 properties, then what are the other properties of the page directive?
El Expressions in JSPs
It is not good to put servlets, declarations, and expressions in a JSP:
1) Web page designers should not be required to understand Java
2) Java code in JSP is difficult to modify and maintain
The user of EL (Expression Language) provides an easier way to invoke Java code, but the code itself is placed in place.
The code might be in a normal Java class, perhaps a JavaBean, a class with a static method, or some label processor.
In short, according to today's best practice, you can no longer write method code in JSP, if you want to write Java methods Elsewhere, and then use El to invoke.
Block script elements in a JSP
Use <scripting-invalid> in DD
Ignore El
If the JSP has template text (normal HTML or text), which happens to include something like El (${somethind}), tell the container to ignore these seemingly el things. (The default El is enabled.) )
Place in DD
Action elements in a JSP
- Standard action:
<jsp:include page="Footer.jsp" />
- Other actions
<c:set var="rate" value="32" />
"Head First Servlets & JSP" using JSP