First, JSP nine large built-in objects
A 9 objects that can be used in a JSP without creating it
Input and Output objects
1.response (httpservletresponse): Processes the response generated by the JSP and sends the response results to the client. is the Esponse parameter of the service method.
2.out (JspWriter): represents the output stream, which is sent to the client as a response to the request. is an example of printwriter.
Scope Object
3.request (HttpServletRequest): Represents the client's request, contains all the request information, and is the resquest parameter of the service method.
4.session (HttpSession): Represents the user's session state, this mechanism can easily identify each user, can save and track the user's session state. Is an example of HttpSession.
5.pageContext (PageContext): Action on this page, you can access all implicit objects defined in the page scope are an instance of PageContext.
6.application (ServletContext): acts on the entire application. is an example of ServletContext.
Servlet Object
7.page: Provides access to all objects defined on the Web page. is an object. is a reference to this servlet. such as Object page = this;
8.config (ServletConfig): Some of the initial information that stores a servlet is an instance of ServletConfig.
Exception Object
9.exception (Throwable): This object is responsible for handling exceptions that are thrown during program execution.
(ii) PageContext
- A top of 9,
- There are three domains in the servlet, and there are four fields in the JSP, which is the last domain object.
- ServletContext: The entire application.
- Session: The entire conversation (only one user in a session).
- Request: A chain of requests.
- PageContext: A JSP page that shares data between the current JSP page and the tags used in the current JSP page.
- The domain object.
- Agent Other domains: Pagecontext.setattribute ("xxx", "xxx", pagecontext.session_scope);
- Global lookup: Pagecontext.findattribute ("xxx"); Small to large, dependent on lookup. In order to avoid the duplicate name phenomenon, it can be used as request_xxx.
- Get the other 8 built-in objects:
- JspWriter getout (): Gets the out built-in object;
- ServletConfig getservletconfig (): Get config built-in object
- Object GetPage (): Gets the page built-in object
- ServletRequest getrequest (): Get request built-in object
- Servletresponse getResponse (): Get response built-in objects
- HttpSession getsession (): Get session Built-in object
- ServletContext getservletcontext (): Get application built-in objects
- Exception getexception (): Get Exception built-in objects
Second, JSP three major directives
A JSP page can have the definition of a 0~n instruction.
1, page: Most complex: <% @page language= "java" info= "xxx" ......%>
- Pageencoding and ContentType:
- Pageencoding: It specifies the current JSP page encoding, as long as not to lie, there will be no garbled. The server needs to use pageencoding when compiling the JSP into. Java;
- ContentType: It means adding a response header: Content-type; equivalent to Response.setcontenttype ("Text/html;charset=utf-8");
- If two properties provide only one, then the other default is the setting of that one.
- If none of the two properties are set, the default is ISO
- Import: Guide package, can appear multiple times
- ErrorPage and Iserrorpage
- ErrorPage: If the current page throws an exception, the page to which it is forwarded is specified by ErrorPage.
- Iserrorpage: It specifies whether the current page is an error-handling page, but when this property is true, the page will have a status code of 500, and the page can use exception from 9 large built-in objects.
Common error page in the Web. xml file Configuration
1 <error-page> 2 <error-code>404</error-code> 3 <location>/error/404.jsp </location> 4 </error-page> 5 <error-page> 6 <error-code>500</error-code> 7 <location>/error/500.jsp</location> 8 </error-page> 9 <error-page>10 <e Xception-type>java.lang.runtimeexception</exception-type>11 <location>/index.jsp</location >12 </error-page>
- AutoFlush and Buffer
- AutoFlush: Specifies whether the JSP output stream buffer is full, automatically refreshed! The default is true, and if False, throws an exception when the buffer is full!
- Buffers: Specifies the buffer size, which defaults to 8kb, and usually does not need to be modified.
- Iselignored: Whether to ignore the El expression, the default value is False, not ignored, that is, support.
- Basic properties:
- Language: Specifies the language type of the current JSP compilation, which is Java by default.
- Info: info.
- IsThreadSafe: Whether the current JSP supports concurrent access.
- Session: Whether the current page supports session, if False, then the current page does not have the session of the built-in object.
- Extends: Let JSP-generated servlet inherit the class specified by this property.
2. Include: static inclusion
- Similar to the functionality of the include () method for RequestDispatcher.
- <% @include%> It was done when the JSP was compiled into a Java file, they co-generated a Java (that is, a servlet) file and then generated a class.
- The include () of RequestDispatcher is a method that contains and is comprised of two servlets, or two. class. They just merge the content of the response at run time.
- Role: The page decomposition, so that the inclusion of the way together, such a page is the same part of the same, is a separate JSP, and we only need to deal with the change of the page.
3. Taglib: Import Tag Library
- Two properties
- Prefix: Specifies the prefix of the tag library in this page, which we name.
- URI: Specifies the location of the tag library.
- <% @taglib prefix= "pre" uri= "/struts-tags"%> prefix usage <s:text>
Third, JSP action tag
The action tags of these jsps are essentially different from the tags provided by HTML.
- The action tag is interpreted by Tomcat (server), which, like Java code, executes on the server side.
- HTML is executed by the browser.
- <jsp:forward>: Forward, it is the same as the forward method of RequestDispatcher, one is used in the servlet, one is used in the JSP.
- <jsp:include>: Contains, it is the same as the include method of RequestDispatcher, one is used in the servlet, one is used in the JSP.
- <jsp:param>: It is used as a sub-label for forward and include to pass parameters to the forwarded or contained page.
1 <body>2 ----%>4 <jsp:forward page= "b.jsp" >5 <jsp:param Value= "Zhangsan" name= "username"/>6 <jsp:param value= "Zhangsan" name= "password"/>7 </jsp:forward>8 </body>
1 = request.getparameter ("username"); 4 string = request.getparameter ("Password" out.print (username+ ":"+password); 6%>
JSP nine built-in objects and instructions, action tags