JSP nine large built-in objects and four scopes

Source: Internet
Author: User

JSP nine large objects: built-in objects (also known as hidden objects, there are 9 built-in objects): No need to pre-declaration can be used in script code and expressions.

The nine large built-in objects in the JSP are:

    • Request object Type Javax.servlet.ServletRequest Scope request
    • Response Response Object Type Javax.servlet.SrvletResponse scope Page
    • PageContext page Context object type Javax.servlet.jsp.PageContext Scope page
    • Session object type Javax.servlet.http.HttpSession scope session
    • Application Application Object type Javax.servlet.ServletContext scope application
    • Out output Object type Javax.servlet.jsp.JspWriter scope Page
    • Config configuration object Type Javax.servlet.ServletConfig scope Page
    • Page object Type Javax.lang.Object Scope page
    • Exception exception Object type javax.lang.Throwable Scope page

The Request object: represents the requests from the client, such as the information we fill out in the form form, etc., is the most commonly used object. The client's request information is encapsulated in the requests object to understand the customer's needs and respond. It is an instance of the HttpServletRequest class. The scope is request (user demand period).

Common methods:

  1. Object GetAttribute (String name) returns the property value of the specified property
  2. Enumeration Getattributenames () returns an enumeration of all available property names
  3. String getcharacterencoding () returns the character encoding method
  4. int Getcontentlength () returns the length of the request body (in bytes)
  5. String getContentType () Gets the MIME type of the request body
  6. ServletInputStream getInputStream () gets the binary stream of a row in the request body
  7. String GetParameter (string name) returns the parameter value of name specified parameter
  8. Enumeration Getparameternames () returns an enumeration of the available parameter names
  9. String[] Getparametervalues (String name) returns an array of all values that contain the parameter name
  10. String Getprotocol () returns the protocol type and version number of the request
  11. String Getscheme () returns the requested plan name, such as: Http.https and FTP
  12. String getServerName () returns the host name of the server that accepts the request
  13. int Getserverport () returns the port number used by the server to accept this request
  14. BufferedReader Getreader () returns the decoded request body
  15. String getremoteaddr () returns the IP address of the client that sent this request
  16. String Getremotehost () returns the host name of the client that sent this request
  17. void SetAttribute (String key,object obj) to set property values for a property
  18. String Getrealpath (string path) returns the true path of a virtual path

Response object: The object represents the response to the client, which means that the data sent to the client can be organized through the response object. However, due to the organization of the lower level, it is not recommended for ordinary readers, need to send text to the client directly to use. It is an instance of the HttpServletResponse class. A scope is page (page execution period).

Common methods:

    1. String getcharacterencoding () returns what character encoding is used for the response
    2. Servletoutputstream Getoutputstream () returns a binary output stream of the response
    3. PrintWriter getwriter () returns an object that can output characters to the client
    4. void setcontentlength (int len) to set the response header length
    5. void setContentType (String type) sets the MIME type of the response
    6. Sendredirect (String location) REDIRECT client requests

Third, Session object: Refers to the client and the server of a conversation, from the customer to a server WebApplication start, until the client disconnects from the server. It is an instance of the HttpSession class. Scope session (session period-).

Common methods:

      1. Long GetCreationTime () returns session creation time
      2. Public String GetId () returns the unique ID number that the JSP engine sets for the session when it is created
      3. Long Getlastaccessedtime () returns the client's last request time in this session
      4. int Getmaxinactiveinterval () returns the two request interval how long this session was canceled (MS)
      5. String[] GetValueNames () returns an array containing all the properties available in this session
      6. void Invalidate () cancels session, making session unavailable
      7. Boolean isnew () returns a session created by the server, whether the client has joined
      8. void RemoveValue (String name) deletes the property specified in the session
      9. void Setmaxinactiveinterval () sets the two request interval for how long this session is canceled (MS)

The Out object: An Out object is an instance of the JspWriter class and is an object commonly used to output content to the client. Scope is page (page execution period)

Common methods:

    1. void Clear () clears the contents of the buffer
    2. void Clearbuffer () clears the current contents of the buffer
    3. void flush () empty stream
    4. int GetBufferSize () returns the buffer as the size of the number of bytes, if no buffer is set to 0
    5. int getremaining () returns how much of the buffer is remaining available
    6. Boolean Isautoflush () returns whether the buffer is full, automatically emptied or throws an exception
    7. void Close () to close the output stream

Page object: The Page object is pointing to the current JSP page itself, a bit like the this pointer in the class, which is an instance of the Java.lang.Object class. The "Page" object represents a running class object produced by a JSP file and is not recommended for general audiences. Scope page

Common methods:

  1. Class GetClass Returns the classes of this object
  2. int Hashcode () returns the hash code of this object
  3. Boolean equals (Object obj) to determine whether this object is equal to the specified object
  4. void copy (object obj) copies this object to the specified object
  5. Object Clone () clones this object
  6. String toString () converts the object object to a String class
  7. void Notify () wakes up a waiting thread
  8. void Notifyall () Wakes all waiting threads
  9. void Wait (int timeout) causes a thread to wait until timeout ends or is awakened
  10. void Wait () causes a thread to wait until it wakes up
  11. void Entermonitor () lock on object
  12. void Exitmonitor () to unlock object

VI, Application object: To realize the sharing of data between users, can store global variables. It starts at the start of the server until the server shuts down, during which time this object will persist, so that the same properties of the object can be manipulated on the user's back-and-forth connection or in a connection between different users, and the operation of this object property anywhere will affect access to it by other users. The startup and shutdown of the server determines the life of the Application object. It is an instance of the ServletContext class. Scope application

Common methods:

  1. Object getattribute (String name) returns the property value for the given name
  2. Enumeration Getattributenames () returns an enumeration of all available property names
  3. void SetAttribute (String name,object obj) sets the property value of the property
  4. void RemoveAttribute (String name) deletes an attribute and its property value
  5. String Getserverinfo () returns the JSP (SERVLET) engine name and version number
  6. String Getrealpath (string path) returns the true path of a virtual path
  7. ServletContext getcontext (String uripath) returns the Application object for the specified WebApplication
  8. int Getmajorversion () returns the maximum version number of the Servlet API supported by the server
  9. int Getminorversion () returns the maximum version number of the Servlet API supported by the server
  10. String GetMimeType (String file) returns the MIME type of the specified file
  11. URL getresource (String path) returns the URL path for the specified resource (file and directory)
  12. InputStream getResourceAsStream (String path) returns the input stream for the specified resource
  13. RequestDispatcher Getrequestdispatcher (String uripath) returns the RequestDispatcher object for the specified resource
  14. Servlet getservlet (String name) returns the servlet of the specified name
  15. Enumeration Getservlets () returns an enumeration of all Servlets
  16. Enumeration Getservletnames () returns an enumeration of all servlet names
  17. void log (String msg) writes the specified message to the servlet's log file
  18. void log (Exception exception,string msg) writes the stack trace and error message of the specified exception to the servlet's log file
  19. void log (String msg,throwable throwable) writes the description information of the stack trajectory and the given Throwable exception to the servlet's log file

The PageContext object provides access to all objects and namespaces within the JSP page, which means that he can access the session where this page is located, or a property value of the application on which it is located, which is equivalent to the synthesizer of all the functions in the page. Its class name is also called PageContext. Pageconfig objects with Fields

Common methods:

  1. JspWriter getout () returns the current client response used by the JspWriter stream (out)
  2. HttpSession getsession () returns the HttpSession object (session) in the current page
  3. Object GetPage () returns the current page of Object Objects (page)
  4. ServletRequest getrequest () returns the ServletRequest object of the current page (request)
  5. Servletresponse GetResponse () returns the Servletresponse object of the current page (response)
  6. Exception getexception () returns the Exception object of the current page (Exception)
  7. ServletConfig Getservletconfig () returns the ServletConfig object (config) of the current page
  8. ServletContext Getservletcontext () returns the ServletContext object of the current page (application)
  9. void SetAttribute (String name,object attribute) setting properties and property values
  10. void SetAttribute (String name,object obj,int scope) sets properties and property values within a specified range
  11. Public Object getattribute (String name) takes the value of the property
  12. Object getattribute (String name,int scope) takes the value of the property within the specified range
  13. Public Object Findattribute (String name) looks for a property that returns the property value or null
  14. void RemoveAttribute (String name) Deletes a property
  15. void RemoveAttribute (String name,int scope) Deletes a property at the specified range
  16. int Getattributescope (String name) returns the scope of an attribute
  17. Enumeration Getattributenamesinscope (int scope) Returns the property name enumeration available within the specified range
  18. void release () frees resources occupied by PageContext
  19. void forward (String Relativeurlpath) redirects the current page to another page
  20. void include (String relativeurlpath) contains another file at the current location

The Config object: The Config object is used by the JSP engine to pass information to it when a servlet is initialized. This information includes the parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object). Scope Page

Common methods:

    1. ServletContext Getservletcontext () returns ServletContext object containing information about the server
    2. String Getinitparameter (string name) returns the value of the initialization parameter
    3. Enumeration Getinitparameternames () returns an enumeration of all parameters required by the servlet initialization

Nine, exception object: Is an exception object, when a page in the process of running an exception occurs, the object is generated. If a JSP page is to be applied to this object, the Iserrorpage must be set to true, otherwise it cannot be compiled. He is actually an object of java.lang.Throwable. Scope page

Common methods:

    1. String GetMessage () returns a message describing the exception
    2. String toString () returns a short description message about the exception
    3. void Printstacktrace () shows exceptions and their stack traces
    4. Throwable Fillinstacktrace () overriding the execution stack trajectory of the exception

SCOPE Specifies the validity period of a variable

If you put the variable in the PageContext, it means that its scope is page, its valid range is only in the current JSP page.

You can use this variable from the start of placing the variable in PageContext to the end of the JSP page.

If you put the variable in the request, it means that it is scoped to request, and its valid range is the current demand period.

The so-called request period, that is, from the HTTP request initiation, to the end of the server processing, return the entire process of response. You may use forward to jump through multiple JSP pages in this process, and you can use this variable in these pages.

If you put the variable in the session, it means that it is scoped to the session, and its valid range is the current one.

The so-called current session refers to the process of opening the browser from the user to the user closing the browser. This process may contain multiple request responses. In other words, as long as the user does not close the browser, the server has the means to know that these requests are initiated by a person, the entire process is called a session, and the variables placed in the session can be used in all requests of the current conversation.

If you put the variable in the application, it means that its scope is application, and its effective range is the entire application.

The entire application is launched from the app to the end of the app. We did not say "boot from server to server shutdown" because a server might deploy multiple applications, and of course you shut down the server and shut down all of the applications.

Application The variables in the scope, they survive the longest, and if not manually deleted, they can be used all the time.

Unlike the three above, the variables in the application can be shared by all users. If user A's operation modifies the variable in application, the User B gets the modified value when he accesses it. This will not happen in any other scope, page, request,session are completely isolated, regardless of the modification will not affect other people's data.

JSP nine large built-in objects and four scopes

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.