First, JSP technology 1. JSP Scripts and annotations
JSP script:
1) <%java Code%>-----Internal Java code translated into the internal of the service method
2) <%=java variable or expression >-----will be translated into service method internal Out.print ()
3) <%!java Code%>----will be translated into the content of the servlet members
JSP annotations: Different annotation visibility ranges are different
1) HTML comment:<!--annotation Content-----Visible range JSP source code, translated servlet, page display HTML source code
2) Java Comment://single-line comment/* Multiline comment */-visible range JSP source translated servlet
3) JSP Comment: <%--comment Content--%>-----Visible range JSP source visible
2. JSP operation principle-----jsp nature is servlet (interview)
When the JSP is first accessed, it is translated by the Web container into a servlet, which executes
Process:
First time access---->helloservlet.jsp---->helloservlet_jsp.java----> Compile Run
PS: The translated servlet can be found in Tomcat's work directory
3. JSP directives (3)
The JSP directive is the command that guides the JSP to translate and run, JSP includes three big instructions:
1) Page directive---The most attribute of the instruction (the page directive default in the actual development)
A directive with the most attributes, which guides the entire page property according to different attributes
Format: <%@ page property Name 1 = "Property value 1" Property Name 2 = "Property value 2" ...%>
Common properties are as follows:
Types of languages that can be embedded in language:jsp scripts
Pageencoding: The current JSP file itself is encoded---internal can contain contenttype
ContentType:response.setContentType (Text/html;charset=utf-8)
Session: Whether the JSP automatically creates a session when translating
Import: Importing Java Packages
ErrorPage: Which page to jump to when the current page is faulted
Iserrorpage: The current page is a page that handles errors
2) include directive
Page contains (statically included) directives that can include one JSP page in another JSP page
Format: <%@ include file= "included file Address"%>
3) taglib Instruction
Introduction of tag libraries in JSP pages (Jstl tag library, struts2 Tag library)
Format: <%@ taglib uri= "tag Library address" prefix= "prefix"%>
4. JSP built-in/implicit objects (9)-----Written examination
After the JSP is translated into a servlet, the service method has 9 objects defined and initialized, and we can use these 9 objects directly in the JSP script
Name |
Type |
Describe |
Out |
Javax.servlet.jsp.JspWriter |
For page output |
Request |
Javax.servlet.http.HttpServletRequest |
Get user request information, |
Response |
Javax.servlet.http.HttpServletResponse |
Response information from the server to the client |
Config |
Javax.servlet.ServletConfig |
Server configuration, can get initialization parameters |
Session |
Javax.servlet.http.HttpSession |
Information to save the user |
Application |
Javax.servlet.ServletContext |
Shared information for all users |
Page |
Java.lang.Object |
Refers to an instance of the current page-converted Servlet class |
PageContext |
Javax.servlet.jsp.PageContext |
JSP's page container |
exception |
Java.lang.Throwable |
Represents an exception that occurs on a JSP page and does not work in the error page |
(1) Out Object
Type of Out: JspWriter
Out function is to want the client output content----Out.write ()
The out buffer default 8KB can be set to 0 for closing the out buffer content directly to the respons buffer
(2) PageContext object
The context object for the JSP page, which acts as follows:
The Page object is not the same as the PageContext object
1) PageContext is a domain object
SetAttribute (String name,object obj)
GetAttribute (String name)
Removeattrbute (String name)
PageContext can access data to a specified other domain
SetAttribute (String name,object obj,int scope)
GetAttribute (String name,int scope)
Removeattrbute (String name,int scope)
Findattribute (String name)
---Get Properties from PageContext domain, request domain, session field, application domain, and get it in a domain, not looking backwards
Summary of four scopes:
page domain: Current JSP page range
Request domain: One request
Session Domain: one session
Application domain: Entire Web App
2) Other 8 large implicit objects can be obtained
Example: Pagecontext.getrequest ()
Pagecontext.getsession ()
5. JSP tags (action)
1) page contains (dynamically included): <jsp:include page= "included pages"/>
2) Request Forwarding: <jsp:forward page= "resources to be forwarded"/>
What is the difference between static inclusion and dynamic inclusion?
Second, El Technology 1. EL Expressions Overview
El (express lanuage) expression can be embedded inside the JSP page, reduce the writing of JSP script, El appears to replace the JSP page script writing.
2. El extracting data from the domain
El's main function is to obtain data in four fields, format ${el expression}
El gets the value in the PageContext domain: $ (pagecontextscope.key);
El obtains the value in the request domain: $ (request.key);
El gets the value in the Session field: $ (session.key);
El gets the value in the application domain: $ (application.key);
El obtains a value of $ (key) from four domains;
---also gets the properties from the PageContext domain, the request domain, the session field, the application domain, and gets it in a domain, not looking backwards.
3. El's Built-in objects
Pagescope,requestscope,sessionscope,applicationscope
----Get data from a domain in a JSP
Param,paramvalues-Receive parameters.
Header,headervalues-GET Request header information
Initparam-Get Global initialization parameters
Cookie-web Cookies in development
Pagecontext-web in the development of PageContext.
$ (PageContext.request.contextPath)
Equivalent
<%=pagecontext.getrequest () .getcontextpath%>
Get the name of your web App
Third, JSTL technology 1. JSTL Overview
JSTL (JSP standard tag Library), JSP standards tag libraries, can be embedded in the JSP page using the form of tags to complete business logic and other functions. The purpose of Jstl is to refer to the scripting code in the JSP page as well as El. The JSTL standard standard Tag Library has 5 sub-libraries, but with the development, his core library is often used today
Tag Library |
The URI of the tag library |
Prefix |
Core |
Http://java.sun.com/jsp/jstl/core |
C |
i18n |
Http://java.sun.com/jsp/jstl/fmt |
Fmt |
Sql |
Http://java.sun.com/jsp/jstl/sql |
Sql |
Xml |
Http://java.sun.com/jsp/jstl/xml |
X |
Functions |
Http://java.sun.com/jsp/jstl/functions |
Fn |
2. Jstl Download and Import
Jstl Download:
Download the Jstl jar package from the Apache website. Go to the "http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" url to download the JSTL installation package. Jakarta-taglibs-standard-1.1.2.zip, and then unzip the downloaded JSTL installation package, you can see two jar files in the Lib directory, respectively, Jstl.jar and Standard.jar. Where the Jstl.jar file contains the interfaces and related classes defined in the JSTL specification, the Standard.jar file contains the. class file used to implement JSTL and 5 tag Library descriptor files (TLDs) in Jstl
3. Common tags for jstl core libraries
1) <c:if> label
<test= "${count==10}"> xxxx</c:if >
2) <c:for> label
<!--foreach impersonation for (int i=0;i<5;i++) {System.out.println (i); } - <C:foreachbegin= "0"End= "5"var= "I">${i}</C:foreach> <!--foreach Impersonation enhancement for loop productlist--list<product> for (Product pro:productlist) {syso (pro.get Name); } - <C:foreachItems= "ProductList"var= "Pro">${pro.pname}</C:foreach>
Jsp&el&jstl