JSP nine implicit JSP objects

Source: Internet
Author: User

 

Nine implicit objects in JSP
Implicit object Class Description
Request Javax. servlet. http. HttpServletRequest Client request information
Response Javax. servlet. http. HttpServletResponse Response from the webpage to the client
Session Javax. servlet. http. HttpSession Request-related sessions
Out Javax. servlet. jsp. JSPWriter Data Stream output to the client browser
Application Javax. servlet. ServletContext Provides global data. Once created, it is maintained on the server.
PageContext Javax. servlet. jsp. PageContext JSP page context, used to access page properties
Page Java. lang. Object Same as this in Java, that is, the JSP page itself
Config Javax. servlet. servletConfig Servlet configuration object
Exception Java. lang. Throwable For error webpages, capture exceptions that are not captured in general webpages

 

 

 

 

 

 

 

 

 

 

, These implicit objects can be divided into four categories:

1. objects related to input/output: request, response, and out
2. objects related to attribute scopes: session, application, and pageContext
3. Servlet-related objects: page and config
4. exception related to error handling

1. page Object
The page object represents the JSP object. More accurately, it represents the Servlet after JSP is translated. It can call the method 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 results of JSP object 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 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 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 ))

Author evangel_z

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.