jsp built in objects and methods are described in detail

  • 2020-05-17 06:09:00
  • OfStack

jsp provides 9 built-in objects. The following is a description of jsp's 9 built-in object methods

Built-in object type scope
request javax.servlet.http.HttpServletRequest request
response javax.servlet.http.HttpServletResponse response
pageContext javax.servlet.jsp.PageContext page
session javax.servlet.http.HtpSession session
application javax.servlet.jsp.ServletContext application
out javax.servlet.jsp.JspWriter page
config javax.servlet.ServletConfig page
page java.lang.Object page
exception java.lang.Throwable page

Classify them according to their functions:
Category 1: related to Servlet: page and config
Category 2: related to Input/Output: out, request and response
Category 3: related to Context: application, session and pageContext
Category 4: related to Error: exceptio
Scope of the object:
1. page: the properties set can only be valid on the current page. setAttribute() and getAttribute() via pageContext.
2. request: the attribute is valid within the scope of one request. If the page jumps from one page to another, the property becomes invalid. The jump here refers to a client jump, such as a client clicking on a hyperlink to jump to another page or browsing another page through the browser's address bar. If using a server-side jump < jsp:forward > , the property remains in effect. Similarly, setAttribute() and getAttribute() of request objects are used.
3, session: refers to the client browser and the server within the scope of one session, if the server is disconnected, then the property is invalid. The same goes through setAttribute() and getAttribute() of the session object. An exception to the session scope is to reopen a browser.
4. application: refers to the whole server scope, which will not fail until the server stops. The same applies to setAttribute() and getAttribute() of application objects. The application scope is a property that can be retrieved from any page as long as the server is not restarted, even if the browser is reopened.
9 big object method description:
1. request object
The request information of the client is encapsulated in the request object, through which the client's requirements can be known.
Then respond. It is an instance of the HttpServletRequest class.

objectgetAttribute(Stringname) returns the property value of the specified property
EnumerationgetAttributeNames() returns an enumeration of all available property names
StringgetCharacterEncoding() returns the character encoding
intgetContentLength() returns the length (in bytes) of the request body
StringgetContentType() gets the MIME type of the request body
ServletInputStreamgetInputStream() gets a binary stream of 1 line in the body of the request
StringgetParameter(Stringname) returns the parameter value of name specified parameter
EnumerationgetParameterNames() returns an enumeration of available parameter names
String[]getParameterValues(Stringname) returns an array containing all the values of the parameter name
StringgetProtocol() returns the protocol type and version number used for the request
StringgetScheme() returns the requested scheme name, e.g. http.https, ftp, etc
StringgetServerName() returns the host name of the server that accepted the request
intgetServerPort() returns the port number used by the server to accept this request
BufferedReadergetReader() returns the decoded body of the request
StringgetRemoteAddr() returns the client IP address that sent this request
StringgetRemoteHost() returns the client host name that sent this request
voidsetAttribute(Stringkey,Objectobj) sets the property value of the property
StringgetRealPath(Stringpath) returns the real path of the 1 virtual path

3. session object
The session object refers to a session between the client and the server, and a connection from the client to the server
WebApplication starts until the client is disconnected from the server. It is HttpSession
Class.
Number method description

longgetCreationTime() returns SESSION creation time
publicStringgetId() returns the number 1ID which the JSP engine set for SESSION when it was created
longgetLastAccessedTime() returns the last time the client requested this SESSION
intgetMaxInactiveInterval() returns how long between requests this SESSION was fetched
String[]getValueNames() returns an array containing all the available properties in this SESSION
voidinvalidate() cancels SESSION, making SESSION unavailable
booleanisNew() returns one SESSION created by the server and whether the client has joined
voidremoveValue(Stringname) removes the properties specified in SESSION
voidsetMaxInactiveInterval() sets how long between requests this SESSION is canceled (ms)

4. out object

The out object is an instance of the JspWriter class, which is a common object to output content to the client
Number method description
voidclear() clears the contents of the buffer
voidclearBuffer() clears the current contents of the buffer
voidflush () to empty flow
intgetBufferSize() returns the buffer in bytes or 0 if no buffer is set
intgetRemaining() returns how much of the buffer is still available
When booleanisAutoFlush() returns the buffer full, does it automatically empty or throw an exception
voidclose() closes the output stream

5. page object

The page object is a pointer to the current JSP page itself, sort of like the this pointer in the class, which is
Class java.lang.Object
classgetClass returns this Object class
inthashCode() returns the hash code for this Object
booleanequals(Objectobj) determines whether this Object is equal to the specified Object object
voidcopy(Objectobj) copies this Object to the specified Object object
Objectclone() clones this Object object
StringtoString() converts this Object object into an object of the String class
voidnotify() wakes up a waiting thread
voidnotifyAll() wakes up all waiting threads
voidwait(inttimeout) keeps a thread waiting until timeout ends or is awakened
voidwait() keeps a thread waiting until it is awakened
voidenterMonitor() locks Object
voidexitMonitor() unlocks Object

6. application object
The application object realizes the sharing of data between users and can store global variables. It starts on the server
The start of the server, until the shutdown of the server, during which time, this object will 1 directly exist; So it's in front of the user
The same 1 attribute of this object can be manipulated in a post connection or a connection between different users. In any
Local operations on the properties of this object will affect other users' access to it. Server startup and
Closure determines the life of the application object. It is an instance of the ServletContext class.

ObjectgetAttribute(Stringname) returns the value of the named property
EnumerationgetAttributeNames() returns an enumeration of all available property names
voidsetAttribute(Stringname,Objectobj) sets the property value of the property
voidremoveAttribute(Stringname) deletes the 1 attribute and its attribute value
StringgetServerInfo() returns the JSP(SERVLET) engine name and version number
StringgetRealPath(Stringpath) returns the real path of a virtual path
ServletContextgetContext(Stringuripath) returns the application object specifying WebApplication
intgetMajorVersion() returns the maximum version number of ServletAPI supported by the server
intgetMinorVersion() returns the maximum version number of ServletAPI supported by the server
StringgetMimeType(Stringfile) returns the MIME type of the specified file
URLgetResource(Stringpath) returns the URL path of the specified resource (files and directories)
InputStreamgetResourceAsStream(Stringpath) returns the input stream for the specified resource
RequestDispatchergetRequestDispatcher(Stringuripath) returns the RequestDispatcher object for the specified resource
ServletgetServlet(Stringname) returns Servlet with the specified name
EnumerationgetServlets() returns all enumeration of Servlet
EnumerationgetServletNames() returns an enumeration of all Servlet names
voidlog(Stringmsg) writes the specified message to the log file of Servlet
voidlog(Exceptionexception,Stringmsg) writes the specified exception stack trace and error message to the Servlet log file
voidlog(Stringmsg,Throwablethrowable) writes the stack trace and the given Throwable exception description to the Servlet log file

7. exception object
The exception object is an exception object, which is produced when an exception occurs during the execution of a page
Give birth to this object. If an JSP page is to apply this object, isErrorPage must be set to true,
Otherwise it won't compile. He is actually the object of java.lang.Throwable

StringgetMessage() returns a message describing the exception
StringtoString() returns a short description message about the exception
voidprintStackTrace() displays the exception and its stack trace
ThrowableFillInStackTrace() overrides the execution stack trace of the exception

8. pageContext object
The pageContext object provides access to all objects and namespaces within the JSP page, i.e
Says he can go to SESSION where this page is located, or he can take application where this page is located
The value of a 1 attribute, which is equivalent to the aggregator of all the functions in the page, is also called the class name
pageContext.

JspWritergetOut() returns the current client response to the JspWriter stream being used (out)
HttpSessiongetSession() returns the HttpSession object (session) in the current page
ObjectgetPage() returns the Object object (page) on the current page
ServletRequestgetRequest() returns the ServletRequest object (request) of the current page
ServletResponsegetResponse() returns the ServletResponse object (response) of the current page
ExceptiongetException() returns the Exception object (exception) on the current page
ServletConfiggetServletConfig() returns the ServletConfig object (config) on the current page
ServletContextgetServletContext() returns the ServletContext object of the current page (application)
voidsetAttribute(Stringname,Objectattribute) sets properties and their values
voidsetAttribute(Stringname,Objectobj,intscope) sets properties and their values within the specified range
publicObjectgetAttribute(Stringname) takes the value of the property
ObjectgetAttribute(Stringname,intscope) takes the value of the property within the specified range
publicObjectfindAttribute(Stringname) looks for the 1 property and returns either the property value or NULL
voidremoveAttribute(Stringname) removes an attribute
voidremoveAttribute(Stringname,intscope) deletes an attribute in the specified range
intgetAttributeScope(Stringname) returns the scope of an attribute
EnumerationgetAttributeNamesInScope(intscope) returns the enumeration of property names available within the specified range
voidrelease() releases the resources occupied by pageContext
voidforward(StringrelativeUrlPath) redirects the current page to another page
voidinclude(StringrelativeUrlPath) contains another 1 file at the current location

9. config object
The config object is used by the JSP engine to pass information to an Servlet when it is initialized. This information includes the parameters needed to initialize Servlet (by means of the property name and property value) and information about the server (by passing an ServletContext object)


Related articles: