JSP basic introduction
1.1 What is JSP
Concept:
JSP
, i.e.,Java Server Pages
.Java
Server pages, that is, pages running on the server side. It is made ofSun
The company advocates, many international large companies to participate in the establishment of a dynamic web technology.JSP
The technology is in traditional static web pagesHTML
Insert into fileJava
Code snippets andJSP
A file formed after a label with a suffix named.jsp
. useJSP
The development ofWeb
The application is cross-platform and can be used inLinux
Can also run on other operating systems.JSP
The essence of the document isServlet
. Just,JSP
withServlet
The difference is,JSP
It’s designed for data presentationServlet
, it has a special way of writing. The ordinaryServlet
Is used to complete business logic processing. Due to theServlet
Is running in a singleton multithreaded environment, soJSP
It also runs in a singleton multithreaded environment.
The JSP 1.2 specification
Concept:
- will
JSP
Page translation asServlet
The process ofTomcat
To complete. inTomcat
There is a built-in one inJSP
Translation engine when first accessing theJSP
Page when the translation engine willJSP
Page translation asServlet
the.java
File, and compile it to.class
File to run. SUN
company-madeJavaEE
The specification contains two important sub-specifications:Servlet
The specification, andJSP
Specification. Among themJSP
The specification is about how to putJSP
Page translation asServlet
. For example,JSP
The pageHTML
.css
.JavaScript
, and the normal text will be translatedout.write()
In the.Tomcat
In theJSP
The translation engine follows thisJSP
Specification.
1.3 the JSP comment
- in
JSP
Using annotations in a page, you can use the content to be annotatedThe < % -- -- % >
Enclosed. - Of course, in
JSP
It can also be used in the pageHTML
The annotation<! -- -->
. But the effect of their use is different.- (1) :
HTML
Comments will beJSP
The translation engine translates toServlet
theout.write()
; whileJSP
Comments will beJSP
Translation engine ignore, inServlet
You can’t see it in. - (2) :When viewing the source code in the client browser,
HTML
Comments are viewable; butJSP
Comments are not viewable on the client side.
- (1) :
This is my JSP page<br> <! -- HTML --> <%-- JSP -->Copy the code
1.4 Java code block in JSP
JSP
Java code block is also calledJSP
Small script, withThe < % % >
Bracketed parts, which can be writtenJava
The code. Writing in theJava
The code in the code block will beJSP
The translation engine puts it intoServlet
the_jspService()
Method as methodJava
Statement appears. So, it must end with a semicolonJava
Statements.- It’s important to note that,
JSP
Code blocks can be placed inJSP
Anywhere on the page, you can place as many as you want. But they’re all going to be placed in orderServlet
the_jspService()
Methods. That is, the order in which it is executedJSP
The order of occurrence on the page is consistent. For example,
The < %double a = 0;
int i = 5;
System.out.println("Java snippet");
System.out.println("Java snippet");
System.out.println("Java snippet");
System.out.println("Java snippet");
a = d * i;
system.out.println( "a = "+a); % >Copy the code
- Be translated into
servlet
the_jspService()
Method, butJSP
The following cannot appear in a small script- (1) : declared variables cannot add permission access control characters
- (2) : Methods cannot be defined
- (3) : static statement blocks cannot be defined
1.5 JSP statement block
- in
JSP
Page useThe < %! % >
The enclosed part is called a declarative block. Declare the contents of the statement block to beJSP
Engine translation toServlet
Class body, which is not contained in any method body. - In that case, in
JSP
You can declare instance variables, instance methods, static methods, static code blocks, etc. In addition, these contents can beJSP
theJava
Code access in a code block. Because they are one and the sameServlet
Class code. However, it is still not recommendedJSP
Declare instance variables in the declaration statement block of becauseJSP
Is run in a singleton multithreaded environment, instance variables will cause thread safety issues. - It should be noted that in the
JSP
Declaration statement block, is unable to write ordinaryJava
The statement. Otherwise, these statements will appear directly in theServlet
In the class body of. - Declaration blocks can also be defined in
JSP
Any location on the page, and you can define as many as you want.
Such as:
The < %!// You can add access control characters
privatel int amount = 3;
// Methods can be defined
public void showData(double data){
system.out.print1n( "data = " + data);
}
// You can define the static statement block static {
system.out.println( "= = = = = = = = = = = = = ="); } % >Copy the code
1.6 JSP expression block
- in
JSP
Page use< % = % >
The enclosed part is called an expression block. It can be inJSP
Outputs variables, constants, and the values of the various expressions they compose in the page. Note that expressions, not statements, do not have semicolons. The expression will beJSP
Engine translation to_jspService()
methodsout.write()
Method. For example,
The < %int count = 5; % >// The count value is displayed on the page
<%=count %>
Copy the code