Detailed parsing of nine implicit objects in JSP

Source: Internet
Author: User
Tags ranges

1. Page Object

The page object represents the JSP itself. More accurately, it represents the servlet after JSP is translated. It can call the methods defined by the servlet class.

 

Ii. config object

The Config object stores some initial servlet data structures.

The Config object is implemented on the javax. servlet. servletconfig interface. It has the following four methods:

Public String getinitparameter (name)

Public java. util. Enumeration getinitparameternames ()

Public servletcontext getservletcontext ()

Public sring getservletname ()

 

Iii. Request object

The request object contains information about all requests, such as the request source, header, cookies, and request-related parameter values.

The request object implements the javax. servlet. http. httpservletrequest interface. The methods provided can be divided into four categories:

1. Storage and acquisition of attributes;

Void setattribute (string name, object Value): set the value of the name attribute to value.

Enumeration getattributenamesinscope (INT scope) gets attributes of all scope ranges

Object getattribute (string name) obtains the value of the name attribute.

Void removeattribute (string name) removes the value of the name attribute.

2. Methods for obtaining Request Parameters

String getparameter (string name) obtains the parameter value enumeration of name.

Getparameternames () Get all parameter names string []

Getparametervalues (string name) obtains the parameter values of all names.

Map getparametermap () gets a map that requires Parameters

3. Methods for obtaining the HTTP header of a request

String getheader (string name) gets the name header

Enumeration getheadernames () get all header names

Enumeration getheaders (string name) gets the headers of all names

Int getintheader (string name) gets the header of the integer name

Long getdateheader (string name) gets the date type name header

Cookie [] getcookies () get cookies related to requests

4. Other Methods

String getcontextpath () Get the context path (that is, the platform name)

String getmethod () get http method (get, post)

String getprotocol () gets the HTTP/1.1 and HTTP/1.0 protocols used)

String getquerystring () gets the request parameter string. However, the HTTP method must be get

String getrequestedsessionid () obtains the session ID of the user end.

String getrequesturi () gets the request URL, but does not include the request parameter string

String getremoteaddr () Get the user's IP address

String getremotehost () obtains the host name of the user.

Int getremoteport () gets the user's host port

String getremoteuser () Get the user name

Void etcharacterencoding (string encoding) sets the encoding format to solve the problem of passing Chinese characters in the form.

 

4. Response object

The response object mainly transmits the result of JSP processing data back to the client.

The response object implements the javax. servlet. http. httpservletresponse interface. The method provided by the response object.

1. How to set the table Header

Void addcookie (cookie) adds a cookie

Void adddateheader (string name, long date) adds the value of the long type to the name header.

Void addheader (string name, string value) adds a string type value to the name header.

Void addintheader (string name, int value) adds the int type value to the name header.

Void setdateheader (string name, long date) specifies the value of the long type to the name header.

Void setheader (string name, string value) specifies the string type value to the name header

Void setintheader (string name, int value) specifies the int type value to the name header

2. How to set the response status code

Void senderror (int SC) Status Code)

Void senderror (int SC, string MSG) Transfer status code and error message

Void setstatus (int SC) Status Code

3. Method Used for URL rewriting (rewriting)

String encoderedirecturl (string URL) encode the URL using the sendredirect () method

 

V. Out object

The out object can output the result to the webpage.

Out is mainly used to control the buffer and output stream used to manage the output ).

Void clear () clears the content of the output buffer

Void clearbuffer () clears the content of the output buffer

Void close () Close the output stream and clear all content

Int getbuffersize () gets the current buffer size (KB)

Int getremaining () obtains the remaining buffer size (Kb) after use)

Boolean isautoflush () returns true, indicating that the buffer is cleared automatically when the buffer is full; false indicates that the buffer is not cleared automatically and Exception Processing is triggered.

 

Vi. Session Object

The Session Object indicates the session status of an individual user.

Implement the javax. servlet. http. httpsession interface and the methods provided by the httpsession interface.

Long getcreationtime () indicates the time the session is generated, in milliseconds.

String GETID () gets the session ID

Long getlastaccessedtime () gets the time when the user finally sends the request through this session

Long getmaxinactiveinterval () gets the maximum time when the session is not active. If this time is exceeded, the session will become invalid.

Void invalidate () cancels the session object and completely discards the content stored in the object

Boolean isnew () determines whether the session is "new"

Void setmaxinactiveinterval (INT interval) sets the maximum time when the session is not active. If this time is exceeded, the session will become invalid.

 

VII. Application Object

The information of the Application object that is most often used in the access environment.

Because the environment information is usually stored in servletcontext, the application object is often used to access the information in servletcontext.

The Application Object implements the javax. servlet. servletcontext interface, the method provided by the servletcontext interface container

Int getmajorversion () gets the main servlet API version of the iner.

Int getminorversion () gets the minor servlet API version of the iner.

String getserverinfo () gets the name and version of the container.

String getmimetype (string file) gets the MIME type of the specified file

Servletcontext getcontext (string uripath) gets the application context of the specified local URL

String getrealpath (string path) gets the absolute path of the local path

Void log (string message) writes information to the log file

Void log (string message, throwable) writes the exception information generated by stack trace to the log file.

 

8. pagecontext object

The pagecontext object can access other hidden objects.

1. The pagecontext object accesses other implicit object attributes. In this case, you need to specify the range parameter.

Object getattribute (string name, int scope)

Enumeration getattributenamesinscope (INT scope)

Void removeattribute (string name, int scope)

Void setattribute (string name, object value, int scope)

The range parameters include page_scope, request_scope, session_scope, and application_scope.

2. Methods for obtaining other implicit objects from the pagecontext object

Exception getexception () returns an exception to the current webpage, but this webpage must be an error page,

Jspwriter getout () returns the output stream of the current webpage, for example, out

Object getpage () returns the servlet object (Instance) of the current webpage, for example, page

Servletrequest getrequest () returns the current webpage request, for example: Request

Servletresponse getresponse () returns the current webpage response, for example: Response

Servletconfig getservletconfig () returns the current servletconfig object of this webpage, for example: config

Servletcontext getservletcontext () returns the current execution environment (context) of the web page, for example, Application

Httpsession getsession () refers to the session associated with the current webpage. For example, session

3. The pagecontext object provides the method for obtaining attributes.

Object getattribute (string name, int scope): return the name attribute. The range is the attribute object of scope. The return type is object.

Enumeration getattributenamesinscope (INT scope) returns the attributes whose range is scope, and the return type is enumeration.

Int getattributesscope (string name) the range of attributes whose return attribute name is name

Void removeattribute (string name) removes an attribute object named name.

Void removeattribute (string name, int scope) removes attribute objects whose names are name and scope.

Void setattribute (string name, object value, int scope) specifies the attribute object name as name, value as value, range as scope

Object findattribute (string name) searches for Attribute objects with the property name in all ranges

 

9. Exception object

To use an exception object, you must set it in the page command. Can be used.

Exception provides three methods:

Getmessage ()

Getlocalizedmessage (),

Printstacktrace (New java. Io. printwriter (out ))

 

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.