JSP implicit object

Source: Internet
Author: User

Master nine implicit objects:
Request Response out session application pagecontext page config exceptionImplicit objects are objects that can be directly used on the page without the need for new objects. Note that... %> or <% =... %>,

Request object: Stores the information of many client requests. Such as the request source, cookie, and client request data.
Method of the request object:

 

Return Value Method Description
Object Getattribute (string name) Returns the property value of a specified attribute.
Enumeration Getattributenames () Returns the enumeration of all available attribute names.
String Getcharacterencoding () Return character encoding method
Int Getcontentlength () Length of the returned Request body (in bytes)
String Getcontenttype () Obtain the MIME type of the Request body.
Servletinputstream Getinputstream () Obtain the binary stream of a row in the Request body.
String Getparameter (string name) Returns the parameter value of the specified name parameter.
Enumeration Getparameternames () Returns the enumeration of available parameter names.
String [] Getparametervalues (string name) Returns an array containing all values of the parameter name.
String Getprotocol () Returns the protocol type and version number of the request.
String Getscheme () The protocol name used to return the request, such as HTTP. HTTPS and FTP.
String Getservername () Returns the Host Name of the server that receives the request.
Int Getserverport () Return the port number used by the server to accept the request
Bufferedreader Getreader () Returns the decoded Request body.
String Getremoteaddr () Returns the IP address of the client sending the request.
String Getremotehost () Returns the host name of the client that sent the request.
Void Setattribute (string key, object OBJ) Set attribute values
String Getrealpath (string path) Returns the actual path of a virtual path.

Response object: Generate a server response and send the response to the client.
Response. getcontenttype () obtains the header information of the response information.
Response. sendredirect (string URL) redirection page
Out object:Indicates the output stream, which will be sent to the client as a request
1. Public abstract void clear ()
Clear the content in the buffer and do not send data to the client.

2. Public abstract void clearbuffer ()
After the data is sent to the client, the content in the buffer is cleared.

3. Public Abstarct void close ()
Close the output stream.

4. Public abstract void flush ()
Output Data in the buffer.

5. Public int getbuffersize ()
Obtain the buffer size. The buffer size can be set by <% @ page buffer = "size" %>.

6. Public abstract int getremainning ()
Obtains the size of the remaining buffer space.

7. Public Boolean isautoflush ()
Obtain the autoflush value set with <% @ page is autoflush = "True/false" %>.

8. Public abstract void newline ()
Output A line feed character for another line.

9. Public abstract void print ()
Displays various data types.

10. Public abstract void println ()
The branch displays various data types.

The following objects are communication objects in scope and are mainly used to store objects.
Session object:Put a written object in this session object, and we write this object to exist in our sessions.
Use the getattribute ("", "") method to put our object in the session, for example:
Object OBJ;
Session. getattribute ("name", OBJ);, the name of this place can be written by yourself
If we want to extract it, write it.
Session. getattribute ("name"); then, it is taken out after forced conversion.
Application object:Put a written object in this application object, and an object will exist in the whole application.
The put and take method is the same as the session object.
Pagecontext object: Place the written object in this pagecontext object. This page exists.
The put and take method is the same as the session object.
**************************************** **
The pagecontext object is equivalent to the container of the current page and can access all objects on the current page.
Common Methods for pagecontext objects:
Httpsession getsession () gets the session object of the current page.
Servletrequest getrequest () gets the request object of the current page.
Servletresponse getresponse () gets the response object of the current page.
Servletcontext getservletcontext () gets the Application Object of the current page.
Servletconfig getservletconfig () gets the config object of the current page.
Object getpage () gets the Page Object of the current page.
Jspwriter getout () gets the out object of the current page.
Exception getexception () gets the exception object of the current page.

 

 

In practical use, the following objects page, config, and exception are rarely used.
Paget object: Generally, we use the page command to replace this object.
Config object:In the same way, we rarely use this in pages. This is usually used in servlets.
Exception object: Used to handle exceptions,
The following is a column for this exception object;
I have two pages: page1.jsp and page2.jsp.
Page1.jsp code
<% @ Page contenttype = "text/html; charset = GBK" errorpage = "page2.jsp" %>
<HTML>
<Head>
<Title>
Page1
</Title>
</Head>
<Body>
<H1>
<% = (3/0) %>
</Body>
</Html>
Obviously, this page is abnormal, that is, I marked it in green. The place I wrote in red indicates that as long as there is an exception on this page, it will automatically jump to page2.jsp.
Now this page2.jsp is skipped when a page1.jsp error occurs. Therefore, we must specify page2.jsp as an error page, that is, a red place,
In this way, I can write <% = Exception. tostring () %> on the page and display the exception information on page1.jsp,
Code of page2.jsp
<% @ Page contenttype = "text/html; charset = GBK" iserrorpage = "true" %>
<HTML>
<Head>
</Head>
<Body bgcolor = "# ffffff">
<H1> program error <% = Exception. tostring () %>
</Body>
</Html>

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.