Recently always insomnia, fortunately my soul chicken move, learning jsp (striking).
The nature of the JSP and the servlet is the same, the JSP will eventually be compiled into Servle to run, or JSP just generate the servlet's draft file.
JSP feature is to embed Java code in HTML, or use a variety of JSP tags, including custom tags, dynamically provide page content, and now evolve into a presentation layer technology.
(Other presentation technology: Freemarker,velocity,tapestry, etc., where JSP is the most widely used)
The nature of JSP is servlet. The JSP page consists of two parts:
Static part: Standard HTML tag, static page content, same as HTML page
Dynamic parts: Content controlled by Java code that is dynamically generated by Java scripts
JSP mutation will become Servletjava code, such as the original one called Zpoor.jsp, compiled after it became Zpoor_jsp.java and Zpoor_ Jsp.class file, after compiling the code in the localhost folder under Tomcat's work.
JSP working principle Conclusion:
The JSP file must be run within the JSP server.
Each JSP file must generate a servlet to execute
The first visitor for each JSP page is slow because it must wait for the JSP to compile into a servlet
The visitors to the JSP page do not need to install any clients and do not have to install an environment that can run Java because the standard HTML page of the JSP page output
JSP Comment: (source code not see)
<%--Comment Content--%>
HTML Comment: (source code can see)
<!--comment Content--
JSP declaration: (Declaring variables and methods: Of course, he will naturally be replaced by the servlet member variables and member methods, JSP declarations still conform to the Java syntax)
<%! Declaration Section%>
Note: The methods and variables defined by the JSP declaration can use the private, public modifier, or static, to turn it into a class property or a class method, but you cannot use the
The method of the abstract modification part, because the abstraction method causes the JSP corresponding servlet to become an abstract class, inversion cannot be instantiated.
(a method of independent existence in the declaration of a JSP, an illusion of knowledge)
JSP Output expression:
<%= expression%>
Cannot have semicolon after output expression syntax
3 compiler directives for JSP:
Page: the directive is for the current page
Include: Used to specify that another page is included
Taglib: Used to define and access custom tags (jsp new features)
Grammar:
<%@ compiler directive Name Property name = "attribute value"%>
Page directives (typically at the top of a JSP page, a JSP can contain multiple page directives):
Language: Declares the scripting language type, which is typically Java and can only be Java
Extends: Specifies the parent class or interface inherited by the Java class that is generated after the JSP page is compiled
Import: For importing Packages
session:jsp whether the page needs to be httpsession
Buffer: Specifies the size of the buffers for JSP pages, with a default value of 8KB
AutoFlush: Whether to force the contents of the output buffer when the buffer overflows
Info: Set up information for this JSP program
ErrorPage: Specifying error Handling interface
Iserrorpage: Sets whether this JSP program is an error handler
Pageencoding: Set the character set encoding of the generated Web page
Include directive:
Embed an external file into the current JSP page and parse the JSP statement for the page, which is a static include statement that will also include other compilation instructions for the target page if the two-page compile instruction conflicts, the page will go wrong, but the dynamic include will not.
Syntax <%@ include file= "file path/filename"%>
7 Action commands for JSP:
The compile instruction is to notify the servlet engine to process messages
Action of Operation instruction Knowledge run time
Jsp:forward: Perform a page turn to forward the processing of the request to the next page
Jsp:param: Pass parameters, must be used in conjunction with other labels that support parameters
Jsp:include: Dynamic introduction of a JSP page
Jsp:plugin: Download JavaBean and applets to client execution
Jsp:usebean: Create an instance of Usebean
Jsp:setproperty: Setting property values for JavaBean instances
Jsp:getproperty: Output attribute values for JavaBean instances
Forward directive:
Forward a page request to a new page, either a static HTML page or a dynamic JSP page, or a servlet that is forwarded to the container
Grammar:
For jsp1.0:
<jsp:forward page= "URL" >
For jsp1.1: (Additional request parameters are added for forwarding)
<jsp:forward page= "URL" >
<jsp:param.../>
</jsp:forward>
Attention:
The client's request parameters are not lost when executing the forward instruction forwarding request. On the surface, he forwards the user request to another new page, in fact the knowledge uses a new interface to generate the corresponding user, so that the request is still a request, the function request parameters and request properties will not change.
Include directive:
He is a dynamic include directive, used to contain a page, he will not be imported by the Include page of the compilation instructions, only import the body content of the page to insert this page.
Syntax (common):
<jsp:include page= "URL" flush= "True|false" >
<jsp:param.../>
</jsp:include>
Where Flush property: Specifies whether the output cache is transferred to the file being imported, and if true, is included in the file being imported and, if False, is included in the original file.
Three differences between static and dynamic imports:
1, static import is to be imported the code of the page completely fused, two pages fused into a whole servlet; Dynamic import uses the Include method in the servlet to introduce the imported page
The contents of the polygon.
2. When static import, the compiled instruction of the imported page will play a role; the compiled instruction that is imported when importing dynamically will lose its function, just insert the body content of the imported page
3, dynamic Import can also add additional parameters
Forward the difference between an action instruction and an include action directive:
When the forward is executed, the forward page will completely replace the original page, and when the include is executed, the page knowledge of the include is inserted into the original page.
Bottom line: Forward takes the target page instead of the original page, and the include takes the target page into the original page.
Usebean, SetProperty, GetProperty directives:
Three instructions are JavaBean-related instructions.
Usebean Syntax:
<jsp:usebean id= "name" class= "classname" scope= "Page|request|session|application"/>
The id attribute is the instance name of JavaBean, and the class attribute determines the implementation class of the JavaBean
The scope property specifies the scope of the JavaBean city:
Page: The JavaBean instance is valid only on this page
Request: Valid for the time being requested
Session: Valid in this session
Application: Effective throughout the application
(The underlying implementation of the following two tags is also implemented based on getter and setter methods)
SetProperty (Syntax):
<jsp:setproperty name= "Beanname" property= "PropertyName" value= "value"/>
The Name property determines the instance name of the JavaBean, the property attribute determines the attribute name to be set, and the Value property determines the property value that needs to be set
Getpeoperty (Syntax):
<jsp:getproperty name= "Beanname" property= "PropertyName"/>
The Name property determines the instance name of the JavaBean that needs to be output, and the property attribute determines the name of the attribute that needs to be output.
Place the JavaBean in the specified scope:
Put in page range
Pagecontext.setattribute ("P1", p1);
Put it in the request range
Request.setattribute ("P1", p1);
Put it in the session range.
Session.setattribute ("P1", p1);
Put it in the application range.
Application.setattribute ("P1", p1);
Plugin directive:
Mainly used to download the server JavaBean or applets to the client execution, the client must be installed virtual chicken (hey hehe).
Of course, this thing is very small, and now the HTML support for the applet is very good, basically do not use this action instruction.
PARAM directive:
Used to set parameter values, which cannot be used alone, is meaningless.
It is generally combined with the following three instructions:
1, Jsp:include
The param directive passes parameters to the imported page
2, Jsp:forward
The param command passes the parameters to the page being transferred.
3, Jsp:plugin
The param directive passes parameters to the JavaBean or applet in the page
A summary of JSP learning (I.)