Jsp built-in objects and Methods

Source: Internet
Author: User

Jsp provides nine built-in objects. The following describes the nine built-in objects in 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

Classify Based on their functions:
The first type: Servlet-related: page and config
Class 2: Related to Input/Output: out, request, and response
Category 3: Related to Context: application, session, and pageContext
Category 4: Error-related: exceptio
Scope of the object:
1. page: The set attribute can only be valid on the current page. Use setAttribute () and getAttribute () of pageContext ().
2. request: The property is valid for one request. If the page jumps from one page to another, this attribute becomes invalid. The jump here refers to client jump. For example, the customer clicks a hyperlink to jump to another page or browses other pages through the browser address bar. If you use server-side redirection <jsp: forward>, this attribute still takes effect. Similarly, setAttribute () and getAttribute () of the request object are used ().
3. session: indicates that the client's browser and server are in the same session range. If the server is disconnected, the attribute becomes invalid. Similarly, the setAttribute () and getAttribute () of the session object are used (). For example, you can re-open a browser.
4. application: indicates that the application fails only after the server is stopped. Similarly, the setAttribute () and getAttribute () of the application object are used (). The application range is the saved attribute that can be obtained on any page as long as the server is not restarted. You can also obtain the attribute even if you re-open the browser.
9 object Methods:
1. request object
The request information of the client is encapsulated in the request object, so that the client can understand the customer's requirements,
Then make a response. It is an instance of the HttpServletRequest class.

ObjectgetAttribute (Stringname) returns the attribute value of the specified attribute.
EnumerationgetAttributeNames () returns the enumeration of all available attribute names
StringgetCharacterEncoding () returns the character encoding method
IntgetContentLength () returns the length of the Request body (in bytes)
StringgetContentType () obtains the MIME type of the Request body.
ServletInputStreamgetInputStream () obtains the binary stream of a row in the Request body.
StringgetParameter (Stringname) returns the value of the parameter specified by name
EnumerationgetParameterNames () returns the enumeration of available parameter names
String [] getParameterValues (Stringname) returns an array containing all values of the parameter name
StringgetProtocol () returns the protocol type and version number of the request.
StringgetScheme () returns the scheduler name used for the request, such as http. https and ftp.
StringgetServerName () returns the Host Name of the server that receives the request
IntgetServerPort () returns the port number used by the server to accept this request.
BufferedReadergetReader () returns the decoded Request body.
StringgetRemoteAddr () returns the IP address of the client sending this request.
StringgetRemoteHost () returns the client host name that sent this request
VoidsetAttribute (Stringkey, Objectobj)
StringgetRealPath (Stringpath) returns the actual path of a virtual path.

3. session Object
A session object refers to a session between the client and the server.
WebApplication starts until the client is disconnected from the server. It is an HttpSession
Class instance.
Serial number method description

LonggetCreationTime () returns the SESSION Creation Time
PublicStringgetId () returns the unique ID number set by the JSP Engine during SESSION creation.
LonggetLastAccessedTime () returns the last client request time in this SESSION
IntgetMaxInactiveInterval () returns the interval between two requests.
String [] getValueNames () returns an array containing all available attributes of this SESSION.
Voidinvalidate () cancels the SESSION to make the SESSION unavailable
BooleanisNew () returns a SESSION created by the server, whether the client has been added
VoidremoveValue (Stringname) deletes the attribute specified in the SESSION.
VoidsetMaxInactiveInterval () sets the interval between two requests this SESSION is canceled (MS)

4. out object

An out object is an instance of the JspWriter class and is a common object for outputting content to the client.
Serial number method description
Voidclear () clears the buffer content
VoidclearBuffer () clears the current content of the buffer
Voidflush () clears the stream
IntgetBufferSize () returns the size of the buffer in bytes. If no buffer is set, the value is 0.
IntgetRemaining () returns how much available the buffer is.
BooleanisAutoFlush () indicates whether to automatically clear or throw an exception when the buffer is full.
Voidclose () closes the output stream

5. page Object

The page object is pointing to the current JSP page itself, a bit like the this pointer in the class, it is
Java. lang. Object class instance sequence number method description
ClassgetClass returns the class of this Object
InthashCode () returns the hash code of this Object
Booleanequals (Objectobj) determines whether the Object is equal to the specified Object
Voidcopy (Objectobj) Copies This Object to the specified Object.
Objectclone () clone this Object
StringtoString () converts this Object to a String Object.
Voidnotify () Wake up a waiting thread
Voidpolicyall () Wake up all waiting threads
Voidwait (inttimeout) enables a thread to wait until the timeout ends or is awakened.
Voidwait () enables a thread to wait until it is awakened.
VoidenterMonitor () locks an Object
VoidexitMonitor () locks the Object

6. application Object
The application Object shares data between users and stores global variables. It starts on the server
Until the server is shut down. During this period, this object will always exist.
You can perform operations on the same property of the object in a post-connection or a connection between different users.
Local Operations on this object property will affect access by other users. Server startup and
Closing determines the life of the application object. It is an instance of the ServletContext class.

ObjectgetAttribute (Stringname) returns the attribute value to the name
EnumerationgetAttributeNames () returns the enumeration of all available attribute names
VoidsetAttribute (Stringname, Objectobj) sets the attribute Attribute Value
VoidremoveAttribute (Stringname) deletes a property and its attribute values.
StringgetServerInfo () returns the JSP (SERVLET) engine name and version number.
StringgetRealPath (Stringpath) returns the actual path of a virtual path.
ServletContextgetContext (Stringuripath) returns the application Object of the specified WebApplication.
IntgetMajorVersion () returns the maximum number of ServletAPI versions supported by the server.
IntgetMinorVersion () returns the maximum number of ServletAPI versions 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 ).
InputStreamgetResourceAsStream (Stringpath) returns the input stream of the specified resource
RequestDispatchergetRequestDispatcher (Stringuripath) returns the RequestDispatcher object of the specified resource.
ServletgetServlet (Stringname) returns the Servlet with the specified name
EnumerationgetServlets () returns the enumeration of all servlets
EnumerationgetServletNames () returns the enumeration of all Servlet names
Voidlog (Stringmsg) writes the specified message to the Servlet Log File
Voidlog (Exceptionexception, Stringmsg) writes the stack track and error message of the specified exception to the Servlet Log File
Voidlog (Stringmsg, Throwablethrowable) writes stack tracing and Throwable exception descriptions to the Servlet log file.

7. exception object
An exception object is an exception object.
This object is generated. To apply this object to a JSP page, you must set isErrorPage to true,
Otherwise, compilation fails. It is actually a java. lang. Throwable object.

StringgetMessage () returns a message describing an exception.
StringtoString () returns a brief description message about the exception.
VoidprintStackTrace () displays exceptions and stack traces
ThrowableFillInStackTrace () overwrites the execution stack track of an exception

8. pageContext object
The pageContext object provides access to all objects and namespaces on the JSP page, that is
He can access the SESSION on this page, or take
A property value is equivalent to a set of all functions on the page. Its Class Name is also called
PageContext.

JspWritergetOut () returns the JspWriter stream (out) in use in the current client response)
HttpSessiongetSession () returns the HttpSession object (session) on the current page)
ObjectgetPage () returns the 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 (exception) of the current page)
ServletConfiggetServletConfig () returns the ServletConfig object (config) of the current page)
ServletContextgetServletContext () returns the ServletContext object (application) of the current page)
VoidsetAttribute (Stringname, Objectattribute) sets attributes and attribute values
VoidsetAttribute (Stringname, Objectobj, intscope) set attributes and attribute values within the specified range
PublicObjectgetAttribute (Stringname) takes the attribute value
ObjectgetAttribute (Stringname, intscope) obtains the attribute value within the specified range.
Publicobjectfindattriname (Stringname) is used to search for an attribute. The starting attribute value or NULL is returned.
VoidremoveAttribute (Stringname) deletes an attribute.
VoidremoveAttribute (Stringname, intscope) deletes an attribute within a specified range.
IntgetAttributeScope (Stringname) returns the scope of an attribute.
EnumerationgetAttributeNamesInScope (intscope) returns an enumeration of available attribute names within a specified range.
Voidrelease () releases resources occupied by pageContext
Voidforward (StringrelativeUrlPath) causes the current page to be re-exported to another page
Voidinclude (StringrelativeUrlPath) contains another file at the current position

9. config object
The config object is used by the JSP Engine to transmit information to a Servlet during initialization. This information includes the parameters used during Servlet initialization (consisting of attribute names and attribute values) and server-related information (by passing a ServletContext object)

Related Article

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.