1. a Jsp page is a common text file consisting of HTML statements and embedded JAVa code. The JSP page file extension is. jsp.
2. Both jsp and Servlet can develop dynamic web resources, but the two are different. servlet is used as the Controller component in the web application, while jsp is used as the data display template.
Servlet is responsible for responding to the generated data and bringing the data to jsp through the forwarding technology. Jsp is used to display data.
3. Jsp principles
When the WEB Container (Servlet Engine) receives an access request from a URL with the. jsp extension, it will send the access request to the JSP Engine for processing. The JSP Engine in Tomcat is a Servlet program, which is responsible for interpreting and executing JSP pages.
When each JSP page is accessed for the first time, the JSP Engine translates it into a Servlet Source program, and then compiles the Servlet Source program into a Servlet class file, then, the WEB Container (Servlet Engine) loads and interprets and executes the Servlet program translated from the JSP page in the same way as calling a common Servlet program.
Tomcat 5. x place the Servlet source files and class files created for the JSP page in the "<TOMCAT_HOME> \ work \ Catalina \
The JSP specification does not explicitly require that the script program code in JSP be written in Java. The script code in JSP can be written in other scripting languages except Java. However, the JSP page must eventually be converted into a Java Servlet program.
You can pre-compile all the JSP pages into Servlet programs before the WEB application is officially released.
4. The Servlet translated from the JSP page inherits org. apache. jasper. runtime. httpJspBase class: HttpJspBase class is a subclass of HttpServlet. Therefore, the Servlet translated from a JSP page is a grandson of HttpServlet. The HttpJspBase class implements some methods in the javax. servlet. jsp. HttpJspPage interface. Therefore, the HttpJspBase class is abstract.
SUN provides jsp web Container developers and JSP page developers with a set of Java classes specifically used to develop JSP programs. This set of Java classes is called JSP APIs. The HttpJspPage interface and JspPage interface are JSP APIs. Only one _ jspService method is defined in the HttpJspPage interface, but it inherits the JspPage interface. The JspPage interface defines two methods: jspInit () and jspDestroy ().
The init method of HttpJspBase calls the jspInit and _ jspInit methods. The destroy method calls the jspDestroy and _ jspDestroy Methods internally, and the service method calls the _ jspService method internally. The init, service, and destroy methods implemented in HttpJspBase are declared as final types.
Each text segment located on both <%> and <%> of each line on the JSP page is converted into an out line with the text as the parameter. the java code in the write statement and JSP script snippet (a piece of java code located in <%>) is moved to the corresponding position in the _ jspService method, JSP expressions (Content in <% = and %>) are converted to out with the variables or expressions as parameters. print statement.
5. the execution process of jsp mainly includes the following points:
The client sends a request
Web Container converts jsp into servlet Source Code
The Web Container compiles the generated source code.
The Web container loads and executes the compiled code.
Returns the execution result to the client.
6. Implicit object
Public void _ jspService (HttpServletRequest request,
HttpServletResponseresponse)
Throwsjava. io. IOException, ServletException
{
JspFactory_jspxFactory = null;
PageContextpageContext = null;
HttpSessionsession = null;
ServletContextapplication = null;
ServletConfigconfig = null;
JspWriterout = null;
Objectpage = this;
...
...
Throwableexception =
Org. apache. jasper. runtime. JspRuntimeLibrary. getThrowable (request );
If (exception! = Null ){
Response. setStatus (HttpServletResponse. SC _INTERNAL_SERVER_ERROR );
}
7. jsp syntax
JSP template Elements
The html content on the Jsp page is called the template element of jsp.
The Jsp template element defines the basic skeleton of a webpage, that is, the structure and appearance of the page.
JSP expressions
Jsp script expression (expression) is used to output program data to the client.
Syntax: <% = variable or expression %>
Eg
Current Time: <% = newjava. util. Date () %>
When translating a script expression, the JSP Engine converts the program data into a string and uses out. print (...) Data is lost to the client.
Variables or expressions in JSP script expressions cannot be followed by semicolons (;)
JSP script snippet
The JSP script snippet (scriptlet) is used to write multiple lines of Java code on the JSP page. Syntax:
<%
Multi-line java code
%>
Note: Only java code can appear in JSP script fragments, and no other template elements can appear. The JSP Engine translates JSP pages, the Java code in the JSP script segment will be stored in the Servlet's _ jspService method.
Java code in JSP script snippets must strictly follow the Java syntax. For example, each execution statement must end with a semicolon.
A JSP page can contain multiple script fragments. Text, HTML tags, and other JSP elements can be embedded between two or more script fragments.
Example:
<%
Int x = 10;
Out. println (x );
%>
<P> This is the JSP page text. </p>
<%
Int y = 20;
Out. println (x );
%>
The code in multiple script fragments can access each other, as if all the code is placed in a pair of <%>. For example, out. println (x );
The Java statements in a single script fragment can be incomplete. However, the result after the combination of multiple script fragments must be a complete Java statement, for example:
JSP Declaration
All code written on the JSP page is translated to the servlet service method by default, and the java code in the Jsp declaration is translated to the _ jspService method. Syntax:
<%!
Java code
%>
Therefore, JSP declarations can be used to define static code blocks, member variables, and methods of Servlet programs converted from JSP pages.
Multiple Static code blocks, variables, and functions can be defined in one JSP declaration, or they can be separately defined in multiple JSP declarations.
The scope of JSP implicit objects is limited to Servlet's _ jspService method. Therefore, these implicit objects cannot be used in JSP declarations.
<%!
Static
{
System. out. println ("loadingServlet! ");
}
Private intglobalVar = 0;
Public voidjspInit ()
{
System. out. println ("initializingjsp! ");
}
%>
<%!
Public voidjspDestroy ()
{
System. out. println ("destroyingjsp! ");
}
%>
Jsp comments
JSP annotation format:
<% -- Comment -- %>
When the JSP Engine translates a JSP page into a Servlet program, it ignores the commented content on the JSP page.
There are three ways to comment
HTML comments (output comments): the comments can be seen when the client views the source code. For example,
<! -- This isan html comment. it will show up int the response. -->
JSP page comment (hidden comment): The comment is written in the JSP program but not sent to the customer. Therefore, the Comment cannot be seen when the client views the source code. Such annotations are ignored during JSP compilation.
<% -- This is aJSP comment. it will only be seen in jsp code -- %>
Java Note: it can only appear in the Java code area and cannot appear directly on the page. // Single line comment/* multi-line comment */