I. JSP scripts and annotationsJSP Script
1) embedded Java code
<%java code%>//Internal Java code translated into the internal of the service method
2) Direct output to the page
<%=java variable or expression >//will be translated into service method internal Out.print ()
3) Setting servlet member content
<%!java code%>//will be translated into the contents of the servlet's members
JSP Annotations
1) HTML Comment:
<!--notes--//Visible range JSP source code, translated servlet, page display HTML source code
2) Java notes:
Single-line Comment/* Multiline comment *//visible range JSP source translated servlet
3) JSP Comments:
<%--Comment Content--%>//Visible range JSP source visible
JSP operating principle:
The nature of JSP is that servlet,jsp is translated into a servlet by the Web container when it is first accessed and then executed
Process:
First time access---->helloservlet.jsp---->helloservlet_jsp.java----> Compile Run
PS: The translated servlet can be found in Tomcat's work directory
second, the JSP Directive
The JSP directive is the command that guides the JSP to translate and run, JSP includes three big instructions:
1) Page directive
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"%>
third, JSP built-in/implicit object
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)---Gets the property from the PageContext domain, the request domain, the session field, the application domain, and gets it in a domain without looking backwards
Summary of four scopes:
Page field: Current JSP page range
Request domain: One-time requests
Session field: One conversation
Application domain: The entire Web application
2) Other 8 large implicit objects can be obtained
- Pagecontext.getrequest ()
- Pagecontext.getsession ()
Iv. JSP tags (action)
1) page contains (dynamically included):
<jsp:include page= "included pages"/>
2) Request Forwarding:
<jsp:forward page= "Resources to be forwarded"/>
the difference between static inclusions and dynamic inclusions
JAVAWEB--JSP Technology