JSP in-depth
Understand the basics of JSP before. Start learning more in depth now
Comments for JSP
supports three types of annotations
1,jsp note <%----%> only exist in the JSP code source, when translated into a servlet, such annotations will disappear, not in the compiled file exists
2,java comments,/** */document comments, generating Javadoc, mainly used to annotate packages, classes, member variables, member methods
/* */Multiline comment and single line comment//, Comment code implementation logic.
Annotations exist when translated into a servlet, but are ignored when executed, and do not exist in the generated HTML code
3.HTML Notes <!---->
When a JSP is translated into a servlet, it is translated into a out.print printed statement, which is present in the HTML page source code for that type of comment.
JSP directive element
Used to instruct the JSP to perform certain steps and specific behavior
Syntax format
<%@ directive[attribute= "Value"]*%>
Classification of Directives
Page directive tag
Syntax format <%@ page[attribute= "value"]*%> used to define the global properties of a JSP file
Common Properties
1,language; Declaring the type of script, temporarily supporting only Java
2,extends, table name JSP compile with the Java full class name to be added, the inner class will inherit from the class, so generally not set, if set, the class must be a servlet implementation class
3,session; Specifies whether the session object can be used directly in the JSP, which defaults to True, and automatically provides a null reference to the session when compiled into Serlvet
HttpSession Session=null;
Session=pagecontext.getsession ();
This allows you to manipulate the session object directly in the JSP file
4,import property; The Guide package work of servlet after the completion of JSP translation
JSP is the default guide package when translating to servlet
Import javax.servlet.*;
Import javax.servlet.http.*;
Import javax.servlet.jsp.*;
5,buffer AutoFlush Property set out suppressed object properties
Buffer size, AutoFlush set the current buffer full, automatically brush out.
6,iselignored sets whether the El expression is parsed and defaults to False
7,contenttype and Pageencoding set the JSP page encoding, pageencoding is the encoding format that the JSP file is saved on the hard disk. ContentType passes the encoding format used by the JSP file when it is compiled into a servlet
8,errorpage and Iserrorpage are used to set the jump after the error page, errorpage to specify the page to jump when an error occurs
Error page processing is generally not done by setting the properties of this method, but in the configuration of Web. XML, this configuration can handle the error page globally.
<error-page>
<error-code></error-code>
<location></location>
</error-page>
Include directive tags
Use to statically include pages, extract common parts of the page, and complete page layouts with include
Syntax format <%@ include file= "url"%>
Be aware that include contains the entire content of the contained page, and that the included page may not be a complete HTML file, but can be just an HTML fragment.
Static inclusions, regardless of the number of JSP pages, are eventually generated with only one target servlet file, and the servlet statements translated into the included JSP pages are merged.
Taglib instruction Mark
Used to reference a tag library file on a JSP page
The purpose of defining tags is to simplify the development of JSP pages, common tag libraries Jstl
<%taglib uri= "" prefix= ""%>
URI definition label unique namespace
Prefix prefix for namespaces
Nine built-in objects for JSPs
These objects are created by default when a JSP file is translated into a servlet
Request HttpServletRequest-----Requested Object
Response HttpServletResponse-----Response Object
Session HttpSession-----Conversation Object
Application ServletContext-----Web App Global Unique object
Out JspWriter
PageContext PageContext
Page this (httpservlet)-----The Servlet class file object type generated by the current JSP file, generally does not use
Config ServletConfig
Exception Throwable
PageContext Object
JSP on the basis of the servlet also a page data range PageContext pages Context object
Pagecontext.findattribute ("name");
This method finds the data range nearest to the name according to the page-request-session-application order.
The PageContext object encapsulates the other eight hidden objects, which can be obtained by means of a method, and the PageContext object is equivalent to acquiring other objects, often applied to the writing of the framework.
Out Object
The output of the information to the browser is itself a JspWriter type, implemented internally using Priterwriter, with a separate buffer
You can set the buffer size and whether to refresh automatically through the page directive
The difference from Response.getwriter (), Response.getwriter buffers are independent from the out buffers, and out buffer flush data is output to the buffer area of the response.getwriter, so out is a Resonse object that must be written back.
JSP common Tag Action element
<jsp:useBean>
JavaBean associated with an ID and a given scope and the same ID
<jsp:setProperty>
Set the property value of JavaBean
<jsp:getProperty>
Get the property value of JavaBean
<jsp:include>
When requested, the file contains, equivalent to Request.getrequestdispatcher (). include (), which is dynamically included, performs the include action at run time, builds multiple servlet programs at compile time
<jsp:forward>
Receives user input and assigns the request to another page, equivalent to Request.getrequestdispatcher (). Forward ();
<jsp:param>
Parameters that can be passed when forwarding, typically nested inside the forward tag
El expression
Function
1, can or have JSP four data range saved data (Access JavaBean property)
${pagescope. Property name}${requestscope. property name}${sessionscope. property name}${applicationscope. Property name}
If you do not specify a lookup range directly ${property name} will call Pagecontext.findattribute (name) once in four data ranges, and if not found returns an empty string instead of NULL
For complex objects that are stored in a data domain, such as the JavaBean property, can also be obtained via the El tag, where JavaBean is used as an attribute, the object is accessed as a property value, and the member in the Access object is directly ${pagescope.person.name}. For collection Array object get ${pagescope.list[i]} for map with ${pagescope.map["key"}
2,el Expressions Support Operations
Keep keyword And,true,instanceof,empty,div,false, etc., the name of the variable should avoid the keyword
3,el built-in 11 common objects for Web development
11 built-in map objects such as Pagescope,requestscope,sessionscope,applicationscope
4,el can call Java methods
The El function library, which exists in the JSTL tag library, is mainly the processing function of the string
Jstl Tag Library
In the case of strict requirements, the JSP is not allowed to define script elements inside, you need to use the JSTL tag library to implement functionality, similar to a writing specification.
Jstl Tag Library is divided into five directions
Core tag libraries (cores)
International Tag Library (FMT)
Database Label SQL
XML tag XML
El function library fn
Each tag library has a corresponding TLD file under the Meta-inf folder in the jar package
Import tag libraries with taglib tags
<% @taglib uri= "", prefix= ""%> specific URI and prefix values can be found in TLD files, corresponding to <uri> and <short-name> tags
JSP in-depth