JSP built-in objects and methods detailed introduction _jsp programming

Source: Internet
Author: User
Tags object object throwable unique id

JSP provides 9 built-in objects, the following JSP's 9 large built-in object method description

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 role
First Category: Servlet Related: page and config
Category II: Related to Input/output: Out,request and response
Category III: Context Related: Application,session and PageContext
Category fourth: Related to the error: exceptio
Object Scope
1, page: Is the set of properties can only be valid on the current page. Through the PageContext of SetAttribute () and getattribute ().
2. Request: The attribute is valid within the scope of one request. If the page jumps from one page to another, the property is invalidated. This refers to the jump refers to the client jump, such as customers click the hyperlink to jump to another page or through the browser address bar to browse other pages. If you use a server-side jump <jsp:forward&gt, the property still takes effect. Use the setattribute () and getattribute () of the request object in the same vein.
3, session: Refers to the client browser and server within a conversation scope, if the server disconnected, then the property is invalid. Similarly through the session object's setattribute () and getattribute (). Exceptions to session scope such as reopening a browser.
4, Application: Refers to the entire server scope, know that the server will not expire after the stop. Similarly through the Application object's setattribute () and getattribute (). Application range is the property that is saved as long as the server does not reboot, it can be obtained in any page, even if the browser is reopened to get properties.
9 Large Object Method Description
1.request Objects
Client requests are encapsulated in the request object to understand the customer's needs.
And then make a response. 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 method
Intgetcontentlength () returns the length of the request body (in bytes)
Stringgetcontenttype () Gets the MIME type of the requested body
Servletinputstreamgetinputstream () gets the binary stream of a row in the request body
Stringgetparameter (Stringname) returns the parameter value of the specified parameter of name
Enumerationgetparameternames () Returns an enumeration of the available parameter names
String[]getparametervalues (Stringname) returns an array of all values that contain the parameter name
Stringgetprotocol () Returns the protocol type and version number requested
Stringgetscheme () returns the plan name for the request, such as: Http.https and FTP
Stringgetservername () returns the server host name that accepts the request
Intgetserverport () returns the port number used by the server to accept this request
Bufferedreadergetreader () returns the decoded request body
STRINGGETREMOTEADDR () Returns the client IP address that sent the request
Stringgetremotehost () returns the client host name that sent the request
Voidsetattribute (stringkey,objectobj) Sets the property value of the property
Stringgetrealpath (Stringpath) returns the true path of a virtual path

3.session Objects
The session object refers to a client-server conversation from a client to a server
WebApplication starts until the client disconnects from the server. It's httpsession.
The instance of the class.
Serial Number Method description

Longgetcreationtime () returns the session creation time
Publicstringgetid () returns the unique ID number that the JSP engine set for the session when it was created
Longgetlastaccessedtime () returns the last time the client requested this session
Intgetmaxinactiveinterval () returns two requests interval how long this session is taken
String[]getvaluenames () returns an array containing all the available attributes in this session
Voidinvalidate () Cancel session so that session is not available
Booleanisnew () returns a session created by the server and whether the client has joined
Voidremovevalue (Stringname) deletes the properties specified in the session
Voidsetmaxinactiveinterval () set two times request interval this session is canceled (MS)

4.out Objects

An Out object is an instance of the JspWriter class and is a commonly used object to output content to the client
Serial Number Method description
Voidclear () Clears the contents of the buffer
Voidclearbuffer () Clears the current contents of the buffer
Voidflush () Purge stream
Intgetbuffersize () returns the size of the buffer in bytes, such as 0 if no buffer is set
Intgetremaining () returns the buffer and how much is left available
Booleanisautoflush () returns whether an exception is automatically emptied or thrown when the buffer is full
Voidclose () turn off output stream

5.page Objects

The Page object is pointing to the current JSP page itself, somewhat like the this pointer in the class, which is
Description of the instance ordinal method of the Java.lang.Object class
Classgetclass returns the class of this object
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
Objectclone () Clones the object object
Stringtostring () Converts the object object to a String class
Voidnotify () wakes up a waiting thread
Voidnotifyall () wakes up all waiting threads
Voidwait (inttimeout) makes a thread wait until timeout is over or awakened
Voidwait () makes a thread wait until awakened
Voidentermonitor () lock on object
Voidexitmonitor () to unlock object

6.application Objects
The Application object enables the sharing of data among users and holds global variables. It starts at the server
Until the server is shut down, during which time this object will always exist; so that before the user
The same property of this object can be manipulated in a connection between a post connection or a different user;
Local actions on this object's properties will affect other users ' access to this property. Startup of the server and
The closure determines the life of the Application object. It is an instance of the ServletContext class.

Objectgetattribute (Stringname) Returns the property value of the given name
Enumerationgetattributenames () Returns an enumeration of all available property names
Voidsetattribute (stringname,objectobj) Set property value
Voidremoveattribute (Stringname) deletes a property and its property value
Stringgetserverinfo () returns the JSP (SERVLET) engine name and version number
Stringgetrealpath (Stringpath) returns the true path of a virtual path
Servletcontextgetcontext (Stringuripath) returns the Application object for the specified WebApplication
Intgetmajorversion () returns the maximum version number of the SERVLETAPI supported by the server
Intgetminorversion () returns the maximum version number of the SERVLETAPI supported by the server
Stringgetmimetype (Stringfile) returns the MIME type of the specified file
Urlgetresource (Stringpath) returns the URL path for the specified resource (file and directory)
Inputstreamgetresourceasstream (Stringpath) returns the input stream for the specified resource
Requestdispatchergetrequestdispatcher (Stringuripath) returns the RequestDispatcher object for the specified resource
Servletgetservlet (Stringname) returns the servlet of the specified name
Enumerationgetservlets () Returns an enumeration of all servlet
Enumerationgetservletnames () Returns an enumeration of all servlet names
Voidlog (STRINGMSG) writes the specified message to the servlet's log file
Voidlog (exceptionexception,stringmsg) writes the stack trajectory and error messages for the specified exception to the servlet log file
Voidlog (stringmsg,throwablethrowable) writes a description of the stack trajectory and the throwable exception given to the servlet log file

7.exception Objects
The exception object is an exception object that, when an exception occurs during a page run, produces
Give birth to this object. If you want to apply this object to a JSP page, you must set the Iserrorpage to true.
Otherwise, it cannot be compiled. He's actually a java.lang.Throwable object.

Stringgetmessage () returns a message describing the exception
Stringtostring () Returns a short descriptive message about the exception
Voidprintstacktrace () display exception and its stack trajectory
Throwablefillinstacktrace () overrides the execution stack trajectory of an exception

8.pageContext Objects
The PageContext object provides access to all objects and namespaces within a JSP page, which is
Says he can access the session where this page is located, or you can take the application
A property value that corresponds to a synthesizer of all the features in the page, and its name is called
PageContext.

Jspwritergetout () returns the JspWriter stream (out) to which the current client response is being used
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 for the current page (request)
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) Set properties and property values
Voidsetattribute (Stringname,objectobj,intscope) Sets properties and property values within a specified range
Publicobjectgetattribute (Stringname) takes the value of a property
Objectgetattribute (Stringname,intscope) takes the value of a property within a specified range
Publicobjectfindattribute (Stringname) looks for a property that returns a property value or null
Voidremoveattribute (Stringname) Deletes a property
Voidremoveattribute (Stringname,intscope) Deletes a property in the specified range
Intgetattributescope (Stringname) returns the scope of a property
Enumerationgetattributenamesinscope (Intscope) Returns an enumeration of the property names available in the specified range
Voidrelease () frees the resources that PageContext occupies
Voidforward (Stringrelativeurlpath) to redirect the current page to another page
Voidinclude (Stringrelativeurlpath) contains another file in its current location

9.config Objects
The Config object is used by the JSP engine to pass information to a servlet when it is initialized, including the parameters that the servlet uses to initialize (by property name and attribute value), and information about the server (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.