I. INTRODUCTION of JSP
JSP (full name Java Server pages) is a technical standard created by Sun Microsystems and many companies involved in creating a Web page that enables software developers to respond to client requests and dynamically generate HTML, XML, or other format documents.
JSP technology is in the Java language as a scripting language, JSP Web page for the entire server-side Java Library unit provides an interface to serve the HTTP application.
Second, the JSP life cycle
The key to understanding the underlying functionality of the JSP is to understand the life cycle that they follow.
The JSP lifecycle is the entire process from creation to destruction, similar to the servlet life cycle, except that the JSP life cycle also includes compiling the JSP file into a servlet.
Here are a few stages of the JSP life cycle:
Compile phase: Loads the servlet class corresponding to the JSP, creates an example, and invokes its initialization method
Initialization phase: Invoking the service method of the servlet instance corresponding to the JSP
Execution phase: Invokes the Destroy method of the servlet instance corresponding to the JSP, and then destroys the servlet instance
Destroy phase: servlet container compiles servlet source file, generates Servlet class
Third, JSP syntax
(1) Scripting syntax
<%%>
Or
<jsp:scriptlet>
</jsp:scriptlet>
(2) Declaration syntax
<%! %>
(3) Expression syntax
<%=%>
(4) Comment syntax
<%----%>
Iv. directives
(1) page
A, properties
buffer specifies the size of the Out object using buffers
AutoFlush controlling the buffers of out objects
CONTENTTYPE Specifies the MIME type and character encoding of the current JSP page
ERRORPAGE Specifies the error handling page that needs to be turned when a JSP page exception occurs
ISERRORPAGE Specifies whether the current page can be used as an error-handling page for another JSP page
extends specifies which class the servlet inherits from
Import imports the Java class to use
Info defines the description information for the JSP page
ISTHREADSAFE Specifies whether access to the JSP page is thread safe
Language defines the scripting language used for JSP pages, which by default is Java
session Specifies whether the JSP page uses the session
iselignored Specifies whether to execute an EL expression
Isscriptingenabled determines whether script elements can be used
B, the syntax format of the page directive:
<%@ page attribute= "value"%>
Equivalent XML format:
<jsp:directive.page attribute= "Value"/>
(2) Include
<%@ include file= "file relative URL address"%>
The file name in the include directive is actually a relative URL address. If you do not associate a path to the file, the JSP compiler looks for it by default under the current path.
Equivalent XML syntax:
<jsp:directive.include file= "file relative URL address"/>
(3) Taglib
The JSP API allows users to customize the label, a custom tag library is a collection of custom labels.
The taglib directive introduces the definition of a custom label collection, including the library path, the custom label.
Syntax for the taglib directive:
<%@ taglib uri= "uri" prefix= "Prefixoftag"%>
The URI property determines the location of the tag library, and the prefix property specifies the prefix of the tag library.
Equivalent XML syntax:
<jsp:directive.taglib uri= "uri" prefix= "Prefixoftag"/>
V. Elements of Action
Jsp:include introduces a file when the page is requested.
Jsp:usebean Find or instantiate a JavaBean.
Jsp:setproperty sets the properties of the JavaBean.
Jsp:getproperty outputs the properties of a JavaBean.
Jsp:forward to move the request to a new page.
Jsp:plugin generates an object or embed tag for the Java plug-in based on the browser type.
Jsp:element defining dynamic XML elements
Jsp:attribute sets the dynamically defined XML element properties.
Jsp:body sets the content of dynamically defined XML elements.
Jsp:text using a template that writes text in JSP pages and documents
Six or nine large implicit objects
instance of the request HttpServletRequest class
Response examples of HttpServletResponse classes
An instance of the Out JspWriter class for outputting the results to a Web page
Example of Session HttpSession class
Application an instance of the ServletContext class, related to the application context
Example of config ServletConfig class
PageContext instances of the PageContext class, providing access to all objects and namespaces of the JSP page
Page is similar to the This keyword in a Java class
Exception object of the Exception class that represents the corresponding exception object in the JSP page where the error occurred
VII. Standard Tag Library (JSTL)
(1) According to the functions provided by the JSTL tag, it can be divided into 5 categories
Core tags
Formatting labels
SQL tags
XML tags
JSTL function
(2) Configuration Jstl
A.web.xml statement
<jsp-config> <taglib> <taglib-uri>http://java.sun.com/jstl/xxx</taglib-uri> <t Aglib-location>/web-inf/xxx.tld</taglib-location> </taglib> </jsp-config>
b.jsp page References
<%@ taglib prefix= "x" uri= "Http://java.sun.com/jsp/jstl/xxx"%>
Viii. Expression Language
(1) El expression syntax
${EXPR}
(2) El expression configuration
Declaring whether an El expression is enabled on a JSP page
<%@ page iselignored = "True|false"%>
(3) El expression operator
. Access a bean property or a mapping entry
[] Access an array or list of elements
() organize a sub-expression to change the priority
+ Plus
-Minus or negative
* Multiply
/or Div except
% or mod modulo
= = or EQ tests are equal
! = or NE tests for unequal
< or LT test is less than
> or GT test is greater than
<= or Le test is less than or equal to
>= or GE test is greater than or equal to
&& OR and test logic with
|| The OR or test logic or
! Or not test negate
Empty test if NULL value
(4) Functions of El
El function syntax
${ns:func (param1, param2, ...)}
NS refers to the namespace (namespace), Func refers to the name of the function, param1 refers to the first argument, param2 refers to the second argument, and so on.
To use any of the functions in the tag library, you need to install the libraries on the server, and then use the <taglib> tags to include them in the JSP file.
(5) El hidden objects
Pagescope Page Scope
Requestscope Request Scope
Sessionscope Session Scope
Applicationscope Application Scope
param parameters of the Request object, string
Paramvalues the parameters of the Request object, a collection of strings
Header HTTP message header, string
Headervalues HTTP Information Header, string collection
Initparam Context Initialization Parameters
Cookie Cookie Value
PageContext the current page PageContext
Nine, exception handling
(1) Exception object
The exception object is an instance of the Throwable subclass and is only available in the error page. The following table lists some important methods in the Throwable class:
- Public String GetMessage () returns the information for the exception. This information is initialized in the Throwable constructor.
- Public Throwablegetcause () returns the cause of the exception, type Throwable object
- Public String toString () returns the class name
- public void Printstacktrace () outputs the exception stack trajectory to System.err
- Public Stacktraceelement [] Getstacktrace () returns the exception stack trajectory as an array of stack trace elements
- Public Throwablefillinstacktrace () fills the Throwable object with the current stack track
(2) jsp page Declaration exception page
<%@ page iserrorpage= "true"%>
(3) JSP page Set Exception page
<%@ page errorpage= "showerror.jsp"%>
Java JSP Summary