Nine built-in JSP objects, commands, and Action tags, and nine jsp objects

Source: Internet
Author: User

Nine built-in JSP objects, commands, and Action tags, and nine jsp objects

I. Nine built-in JSP objects

(1) Nine objects that can be used without being created in JSP

Input and Output objects

1. response (HttpServletResponse): process the response generated by JSP and send the response result to the client. It is the esponse parameter of the service method.
2. out (JspWriter): indicates the output stream, which is sent to the client as a request response. It is an instance of PrintWriter.

Scope object

3. request (HttpServletRequest): indicates the client request, which contains all request information and is the resquest parameter of the service method.
4. session (HttpSession): indicates the user's session status. This mechanism can be used to easily identify each user and save and track the user's session status. It is an instance of HttpSession.
5. pageContext: on this page, you can access all the implicit objects defined in the page scope as an instance of PageContext.
6. application (ServletContext): acts on the entire application. It is an instance of ServletContext.

Servlet object

7. page: provides access to all objects defined on the web page. It is an Object. It is a reference of this Servlet. Such as Object page = this;
8. config (ServletConfig): stores some initial Servlet Information as an instance of ServletConfig.

Exception object
9. exception (Throwable): This object is responsible for handling exceptions thrown during program execution.

(2) pageContext

  • A maximum of 9,
  • Servlet has three fields, while JSP has four fields, which are the last domain object.
    • ServletContext: the entire application.
    • Session: the entire session (only one user in one session ).
    • Request: A request chain.
    • PageContext: a jsp page in which data is shared between the current jsp page and the tags used in the current jsp page.
      • Domain object.
      • Other proxy domains: pageContext. setAttribute ("xxx", "XXX", PageContext. SESSION_SCOPE );
      • Global Search: pageContext. findAttribute ("xxx"); from small to large, dependent search. To avoid duplicate names, such as request_xxx can be used for naming;
      • Obtain the other 8 built-in objects:
        • JspWriter getOut (): gets the built-in out object;
        • ServletConfig getServletConfig (): Get the built-in config object
        • Object getPage (): Get the built-in Object of page
        • ServletRequest getRequest (): gets the built-in request object
        • ServletResponse getResponse (): gets the built-in response object.
        • HttpSession getSession (): gets the built-in session Object
        • ServletContext getServletContext (): gets the built-in application Object
        • Exception getException (): Get the built-in object of exception

 

Ii. Three JSP commands

A jsp page can contain 0 ~ N commands.

1. page: the most complex: <% @ page language = "java" info = "xxx" ...... %>

  • PageEncoding and contentType:
    • PageEncoding: it specifies the encoding of the current jsp page. As long as the page is not lying, no garbled code exists. PageEncoding is required when the server compiles jsp into. java;
    • ContentType: It indicates adding a response header: Content-Type; equivalent to response. setContentType ("text/html; charset = UTF-8 ");
    • If only one attribute is provided, the other default value is set.
    • If neither attribute is set, the default value is iso.
  • Import: Export package, which can appear multiple times
  • ErrorPage and isErrorPage
    • ErrorPage: if an exception is thrown on the current page, the error page specifies which page to forward.
    • IsErrorPage: It specifies whether the current page is an error page. If this attribute is true, the status code 500 is set for this page, in addition, this page can use exceptions from nine built-in objects.

Configure common error pages in the web. xml file

 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         <exception-type>java.lang.RuntimeException</exception-type>11         <location>/index.jsp</location>12     </error-page> 
  • AutoFlush and buffer
    • AutoFlush: Specifies whether to automatically refresh when the jsp output stream buffer is full! The default value is true. If it is false, an exception is thrown when the buffer is full!
    • Buffer: Specifies the buffer size. The default value is 8 KB, which is usually not required to be modified.
  • IsELIgnored: whether to ignore el expressions. The default value is false. If this parameter is not ignored, this parameter is supported.
  • Basic attributes:
    • Language: Specifies the language type after jsp compilation. The default value is java.
    • Info: information.
    • IsThreadSafe: whether the current jsp supports concurrent access.
    • Session: whether the current page supports session. If it is set to false, the current page does not have the session built-in object.
    • Extends: Let the servlet generated by jsp inherit the class specified by this attribute.

2. include: static include

  • Similar to the include () method of RequestDispatcher.
  • <% @ Include %> it is completed when jsp is compiled into a java file. They generate a Java (servlet) file together and then generate a class.
  • RequestDispatcher's include () is a method that contains and contains two Servlets, namely, two. classes. They only merged the response content at runtime.
  • Purpose: break down the page and combine the contained methods. The unchanged part of the page is an independent jsp, and we only need to process the changed page.

 

3. taglib: import the tag Library

  • Two attributes
    • Prefix: Specifies the prefix of the tag library on this page, which is named by ourselves.
    • Uri: Specifies the location of the tag library.
    • <% @ Taglib prefix = "pre" uri = "/struts-tags" %> prefix usage <s: text>

Iii. JSP action tags

These jsp action tags are essentially different from the tags provided by html.

  • Action labels are interpreted and executed by Tomcat (server). They are executed on the server like Java code.
  • Html is executed by the browser.
  • <Jsp: forward>: forward. It is similar to the forward method of RequestDispatcher. One is used in Servlet and the other is used in jsp.
  • <Jsp: include>: include. It is similar to the include method of RequestDispatcher. One is used in Servlet and the other is used in jsp.

 

  • <Jsp: param>: it is used as the sub-labels of forward and include to pass parameters to the forwarded or contained pages.
1 <body> 2 
1 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.