JSP----Nine large built-in objects

Source: Internet
Author: User
Tags map class

Recently, in the code, the page used to JavaScript, purely self-study, so scattered to record some of the recent use of many functions, the wrong more than some aspects.

                            

On JSP pages, there are nine large built-in objects that you will often use:

Built-in object type scopes

Built-in objects 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

First, request

Request object: The Request object not only can be used to set and get the request range variable, but also can be used to obtain the client request parameters, the source of the request, header, cookies and so on.

1. Request parameter method for client requests

Method return value type Method description
GetParameter (String name) String Gets the parameter value named name for the parameter
Getparameternames () Enumeration Gets the name of all parameters, which can be used in combination with the previous method to get the values of all parameters
Getparametervalues (String name) String[] Gets all parameters named name of the parameter, such as the parameter is multiple checkboxes
Getparametermap () Map Gets the map instance encapsulated by all parameters, and returns an array of values with the corresponding parameter named ID through the map instance's string[] get ("id") method

2. Other methods of request

Method return value type Method description
GetHeader (String name) String Gets the header named name for the specified header
Getheadername () Enumeration Get all the header names
Getintheader (String name) Int Gets the header named name, with the content returned as an integer type
Getdateheader (String name) Long Gets the header named name, with the content returned as a date type
GetCookies () Cookies Get the relevant cookie
Getcontextpath () String Get the path to the context
GetMethod () String Get how clients are submitted
Getprotocol () String Get the HTTP protocol used
GetQueryString () String Gets the requested string
Getrequestsessionid () String Gets the session ID of the client
Getrequesturi () String Gets the requested URI
GETREMOTEADDR () String Get Client IP Address

Second, Response object: Used to transmit output information to the client.

Method return value Method description
Addcookie (Cookie cookie) void Refer to request
Adddateheader (String name,long date) void Refer to request
AddHeader (String name,string value) void Refer to request
Addintheader (String name,int value) void Refer to request
Setdateheader (String name,long date) void Refer to request
SetHeader (String name,string value) void Refer to request
Setintheader (String name,int value) void Refer to request
Senderror (int SC) void Transfer Status Code
Senderror (int sc,string msg) void Transfer status codes and error messages
SetStatus (int SC) void Set the status code
Sendredirect (String URL) void Page redirection for page jumps

Note: Here the Response object's Sendredirect (String URL) method Setting page redirection will change the browser address bar information, so also called the client jump.

The instance 1:response object implements the automatic refresh of the page: just the early JSP page plus

<%--uses the Setintheader of the response object to set the value of the refresh (in seconds) for automatic page refresh--%> <% response.addintheader ("Refresh", 10); %>

Example 2: Implementation of automatic page jump: You can use the Response object's SetHeader () method to add a header titled Refresh, and set the page jump time and jump page, so that the page automatically jump. <% Response.setheader ("Refresh", "10; Url=http://www.baidu.com "); %> here, use the SetHeader method to add a header titled "Refresh" with a value of "10,url=http://www.baidu.com".

Third, out object: Used to output information to the page. The Out object is used to output information within a Web browser and to manage The output buffers on the application server . When you use an Out object to output data, you can manipulate the data buffers to clear the remaining data in the buffer and make buffer space for the other output. When the data output is complete, the output stream should be closed in time.

Method return value type Method description
Clear () void Clear the output on a webpage
Clearbuffer () void Clear Buffer Contents
Close () void Close buffer, clear all contents
GetBufferSize () Int Get buffer size
Getremaining () Int Get buffer remaining size
Isautofulsh () Boolean Get information about whether the buffer is automatically purged
Print (String str) void For page output
println (String str) void Page output and line wrapping

Session object: Used to represent the user's conversational state, generally used to save the user's various information until the life cycle timeout or is considered to be released. The Session object is an object that is automatically created by the server that is related to the user request. The server generates a Session object for each user that holds the user's information and tracks the user's operational status. The session object uses the map class internally to hold the data, so the format of the saved data is "Key/value". The value of the session object can make complex object types, not just string types.

Method return value type Method description
GetId () String Gets the ID of the session
GetCreationTime () Long Gets the session's build time
Getlashaccessedtime () Long Get user last Send request time via session
Getmaxinactiveinterval () Long Gets the session life cycle, which expires if this time is exceeded
Invalidate () void Clear Session Contents
IsNew () Boolean Determine if the session is "new"
Setmaxinactiveinterval () void Sets the session life cycle, which expires if this time is exceeded

V. Application object: Used to obtain and set information about the servlet. The Application object saves information in the server until the server shuts down, otherwise the information saved in the Application object will be valid throughout the application. The Application object has a longer life cycle than the session object, similar to the system's global variables.

Method return value type Method description
Getmajorversion () Int Get the main servlet API version
Getminorversion () Int Get the minor servlet API version
Getserverinfo () String Get Server version
GetMimeType () String Gets the MIME type of the specified file
GetContext () ServerContext Gets the application context for the specified local
Getrealpath String Gets the absolute path of the specified path

Vi. PageContext objects: Properties that can be used to set the page range, and other range properties, but you need to specify a range parameter, and you can also get other built-in objects. The function of the PageContext object is to obtain any range of parameters, through which can get the JSP page out, request, reponse, session, application and other objects. The creation and initialization of PageContext objects is done by the container, and the PageContext object can be used directly in the JSP page.

Method return value Method description
GetException () Exception Gets the current exception built-in object
Getout () JspWriter
GetPage () Object
Getrequest () Servletrequset
GetResponse () Servletresponse
Getservletconfig () ServletConfig
Getserveltcontext () ServletContext
GetSession () HttpSession
GetAttribute (String name,int scope) Object Gets the Name property value for the specified range
Getattributenamesinscope (int scope) Enumeration Gets all property names for the specified range
Getattributesscope (String name) Int Get property range with Name
RemoveAttribute (String name) void Remove property with Name property
RemoveAttribute (String name,int scope) void Removes the attribute with the name of the specified range
SetAttribute (String name,object value,int scope) void Sets the name property of the specified range
Findattribute (String name) Object Look for properties with name for all scope property names

The Page object: The Page object represents a JSP-translated servlet, which makes it very easy to invoke the methods defined in the Servlet class through the Page object. The Page object represents the JSP itself and is only legal within the JSP page. The page implied object essentially contains the variables referenced by the current servlet interface, similar to the this pointer in Java programming.

Example 1: Invoking a method defined in a servlet class from a Page object

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%> <%@ page info= "This is the page Object practic E "%>

The Config object: can be used to obtain configuration information for the servlet. The main purpose of the Config object is to obtain the server configuration information. A config object can be obtained by using the Getservletconfig () method of the Pageconext object. When a servlet initializes, the container passes some information through the Config object to the servlet. Developers can provide initialization parameters for servlet programs and JSP pages in the application environment in the Web. xml file.

Method return value type Method description
Getinitparameter (name) String Get servlet initialization parameters
Getinitparameternames () Enumeration Get all initialization parameter names of the servlet
Getservletcontext () ServletContext Get Current application context
Getservletname () String Get the servlet name

Ix. Exception object: Used to handle error exceptions, if you want to use the exception object, you must specify the Iserrorpage property value in the page to True.

Example: Exception to handle error exceptions

1. Page with Error: error.jsp

<%@ page language= "java" contenttype= "text/html;charset=gb2312" errorpage= "exceptiondemo01.jsp"%>

Note: error.jsp in arr[3] array The following table is out of bounds. You specify the property value of ErrorPage in the page directive as the appropriate exception handler.

2. Exception Handling page: exceptiondo.jsp

<%@ page language= "java" contenttype= "text/html;charset=gb2312" iserrorpage= "true"%> <% @page import= " Java.io.PrintStream "%>

Note: You must specify the property Iserrorpage property value to true in the page directive. This means that the page can be used for exception handling.

Array subscript out-of-bounds exception information in error.jsp will be printed in exceptiondo.jsp



JSP----Nine large built-in objects

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.