JSP built-in objects and methods

Time:2021-5-4

JSP provides 9 built-in objects, the following 9 built-in object method description of JSP

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

They are classified according to their functions
The first type: related to servlet: page and config
The second type: related to input / output: out, request and response
The third category: related to context: application, session and pagecontext
The fourth type: related to error: exception
Object scope
1. Page: the property set can only be valid on the current page. Through the setAttribute () and getattribute () of pagecontext.
2. Request: the attribute is valid within the scope of a request. If a page jumps from one page to another, the property is invalid. The jump here refers to the client jump, such as the customer click the hyperlink to jump to other pages or browse other pages through the browser address bar. If server side jump is used< jsp:forward >, the property remains in effect. Similarly, use setAttribute () and getattribute () of the request object.
3. Session: within the scope of a session between the client browser and the server, if the server is disconnected, the attribute will be invalid. Similarly, through setAttribute () and getattribute () of session object. Exceptions to the session scope, such as reopening a browser.
4. Application: in the whole server scope, it will not fail until the server stops. Similarly, through the setAttribute () and getattribute () of the application object. Application scope is the saved property. As long as the server does not restart, it can be obtained in any page. Even if the browser is reopened, it can also obtain the property.
Method description of 9 major objects
1. Request object
The request information of the client is encapsulated in the request object, through which the customer’s needs can be understood,
And then respond. It is an instance of the HttpServletRequest class.

Objectgetattribute (stringname) returns the attribute value of the specified attribute
Enumerationtattributenames() returns an enumeration of all available attribute names
Stringgetcharacterencoding() returns the character encoding method
Intgetcontentlength() returns the length of the request body in bytes
Stringgetcontenttype() gets the MIME type of the request body
Servlet InputStream getinputstream() gets the binary stream of one line in the request body
Stringgetparameter (stringname) returns the parameter value of the parameter specified by name
Enumerationtparameternames() 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 for the request
Stringgetscheme() returns the plan name for the request, such as http.https and FTP
Stringgetservername() returns the host name of the server that accepted the request
Intgetserverport() returns the port number used by the server to accept the request
Bufferedreadergetreader() returns the decoded request body
Stringgetremoteaddr() returns the IP address of the client that sent the request
Stringgetremotehost() returns the host name of the client that sent the request
Void setAttribute (stringkey, objectobj) sets the attribute value of the attribute
Stringgetrealpath (stringpath) returns the real path of a virtual path

3. Session object
Session object refers to a session between the client and the server, a session from the client to the server
Webapplication starts until the client is disconnected from the server. It’s httpsession
Class
Serial number method description

Longgetcreationtime() returns the session creation time
Publicstringgetid() returns the unique ID number set by the JSP engine for the session when it was created
Longgetlastaccessedtime() returns the last request time of the client in this session
Intgetmaxinactive interaction() returns the interval between two requests. This session is fetched
String [] getvaluenames() returns an array containing all the available properties in this session
Void validate() cancels session, making session unavailable
Boolean isnew() returns a session created by the server, whether the client has joined or not
Void remove value (string name) removes the property specified in session
Void setmaxinactive interval() sets the interval between two requests. This session is cancelled (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
Serial number method description
Void clear() clears the contents of the buffer
Void clearbuffer() clears the current contents of the buffer
Void flush() clears the stream
Intgetbuffersize() returns the size of the buffer in bytes, or 0 if no buffer is set
Intgetremaining() returns how much buffer is available
Booleanisautoflush() returns whether to empty automatically or throw an exception when the buffer is full
Void close() closes the output stream

5. Page object

The page object refers to the current JSP page itself, which is a bit like the this pointer in the class
Instance number method description of Java. Lang. object class
Classgetclass returns the class of this object
Inthashcode() returns the hash code of this object
Boolean equals (objectobj) determines whether the object is equal to the specified object
Void copy (objectobj) copies the object to the specified object
Objectclone() clones this object
Stringtostring() converts this object object into an object of the string class
Void notify() wakes up a waiting thread
Voidnotifyall() wakes up all waiting threads
Voidwait (inttimeout) makes a thread wait until the timeout ends or is awakened
Void wait() keeps a thread waiting until it is woken up
Voidentermonitor() locks object
Void exitmonitor() unlocks object

6. Application object
Application object realizes data sharing among users, and can store global variables. It starts on the server
The object will exist until the server is shut down; This is in front of the user
In post connection or connection between different users, the same attribute of this object can be operated; In any
Local operations on this object property will affect the access of other users. Server startup and maintenance
Closing determines the life of the application object. It is an instance of the ServletContext class.

Objectgetattribute (stringname) returns the attribute value to the named attribute
Enumerationtattributenames() returns an enumeration of all available attribute names
Void setAttribute (string name, objectobj) sets the attribute value of the attribute
Void remove attribute (string name) deletes an attribute and its 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 of the specified webapplication
Intgetmajorversion() returns the maximum version number of the servlet API supported by the server
Intgetminorversion() returns the maximum version number of the servlet API supported by the server
Stringgetmimetype (stringfile) returns the MIME type of the specified file
Urlgetresource (stringpath) returns the URL path of the specified resource (file and directory)
InputStream getresourceasstream (stringpath) returns the input stream of the specified resource
Requestdispatcher getrequestdispatcher (stringuripath) returns the requestdispatcher object of the specified resource
Servlet getservlet (string name) returns the servlet with the specified name
Enumerationgetservlets() returns an enumeration of all servlets
Enumerationtservletnames() returns an enumeration of all servlet names
Void log (string MSG) writes the specified message to the servlet log file
Void log (exception exception, string MSG) writes the stack trace and error message of specified exception to the log file of servlet
Voidlog (string MSG, throwablethrowable) writes the stack trace and the description of throwable exception to the servlet log file

7. Exception object
Exception object is an exception object. When an exception occurs in the running process of a page, it will be generated
I don’t like this object. If a JSP page wants to apply this object, it must set iserrorpage to true,
Otherwise, it cannot be compiled. It’s actually an 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() rewrites the stack trace of the exception

8. Pagecontext object
The pagecontext object provides access to all the objects and the name space in the JSP page, that is, the
He said that he can access the session where this page is located, and he can also access the
A certain attribute value is equivalent to the synthesizer of all functions in the page. Its class name is also called
pageContext。

Jspwritergetout() returns the jspwriter stream (out) used by the current client response
Httpsessiongetsession() returns the httpsession object (session) in the current page
Objectgetpage() returns the object object (page) of 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 of the current page
Servletconfiggetservletconfig() returns the ServletConfig object (config) of the current page
Servletcontextgetservletcontext() returns the ServletContext object (application) of the current page
Void set attribute (string name, objectattribute) sets attributes and attribute values
Void set attribute (string name, object obj, intscope) sets the attribute and its value in the specified range
Public object getattribute (string name) takes the value of the attribute
Objectgetattribute (stringname, intscope) takes the value of the attribute in the specified range
Public object findattribute (string name) looks for an attribute and returns the value of the attribute or null
Void remove attribute (string name) to delete an attribute
Void remove attribute (string name, intscope) deletes an attribute in the specified range
Intgetattributescope (stringname) returns the scope of an attribute
Enumeratingtattributenamesinscope (inscope) returns an enumeration of available attribute names in the specified range
Void release() releases the resources occupied by pagecontext
Void forward (string relative urlpath) to redirect the current page to another page
Void include (string relative urlpath) contains another file in the current location

9. Config object
The config object is used by the JSP engine to pass information to a servlet when it is initialized. This information includes the parameters to be used in the initialization of the servlet (composed by attribute name and attribute value) and the information about the server (by passing a ServletContext object)

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]