Http://www.cnblogs.com/fanfu1/p/4530980.html
JSP nine large built-in objects and four scopes
-------Android Training, Java training, look forward to communicating with you! ----------
In the study of JSP, the first thing is to understand the JSP built-in objects, what is the built-in object? Built-in objects are also known as hidden objects, which can be used arbitrarily in script code and expressions without the need for pre-declaration. This built-in object has a total of nine and four scopes in the JSP, which we'll explain in the following article.
Now let's take a look at four scopes to describe what scopes each large object belongs to, and here's a simple table that looks at the scope of information sharing for each major scope:
The first scope is page, which is valid only on the current page, that is, the page the user requested is valid, and when the current page closes or goes to another page, the Page object is released after the response is fed back to the client.
The second scope is request, which is valid in the current requests, and request can be passed through the setattribute () method for the information passing through the page, or through the forward () method to jump between pages, note that the request is forwarded not redirected, Forwarding is transparent relative to the browser, that is, regardless of how the page jumps, the address bar is still displayed as the original address.
The third scope is the session, which is valid in the current reply. When multiple accesses are made to a server by the same browser on a computer, the information passed between these multiple accesses is the scope of the session scope. It makes the first HTTP request from the browser to assume that the session starts, but the time at which the session ends is indeterminate, because the server is not notified when the browser is closed, the default time for the general Tomcat setting is 120 minutes, or it can be setmaxinactiveinterval ( int) method, or force the end of the current session through the invalidate () method.
The fourth scope is application, which is valid in all applications, that is, when the server starts to the end of the server, the data stored in the application scope is valid, You can also use setattribute assignment and getattribute to take a value.
OK, I believe now that we have a certain understanding of the four scopes, let's take a look at the nine built-in objects.
Built-in Object one (out):
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. The most commonly used method is print, which displays string information in the page. The scope of Out is page:
<% out. Print ("%>
Built-in Object two (request):
request object is an object of type Javax.servlet.httpServletRequest. This object represents the client's request information and is primarily used to accept data that is delivered to the server over the HTTP protocol. (including header information, System Information, request method, request parameters, etc.). The request object has a scope of one-time requests (that is, the request scope). A common method of request is GetParameter (string name) gets the page submission data based on the form component name, and Getparametervalues (string name) gets a set of data submitted by a form component named with the same name. Setcharacterencoding (String charset) is set before calling the GetParameter () method for solving Chinese garbled characters, Getrequestdispatcher (string path) Returns a Javax.servlet.RequestDispatcher object that is used by the forward () method of the object to forward the request:
<% New Object (); Request.getparameter ("uname"); Request.getparametervalues ("rid"); Request.setcharacterencoding ("UTF-8"); Request.getrequestdispatcher (" target page"). Forward (request,response);%>
Built-in Object three (response):
Response represents the response to the client, primarily the object that the JSP container has processed back to the client. The response object also has scope, which is valid only within the JSP page. A common method of response is to have Addcookie (cookie cookie) Add Cookie,sercontenttype (String type) to the client to set the ContentType type of the HTTP response. Setcharacterencoding (String charset) sets the type of character encoding used for the response, Sendredirect (string location) repositions the request to a new address, which is the address on the address bar that changes:
<% Response.addcookie (Cokkie); Response.setcontenttype ("type"); Response.setcharacterencoding ("Utf-8"); Response.sendredirect ("relocated pages"); %>
Built-in object four (session):
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 use the object type. A common method of the session is setattribute (string key,object value) to save the object to the session in Key/value form, getattribute (string key) Get Value,invalidate () saved in session by key value to force session object to expire, getId () get sessionid,setmaxinactiveinterval (int interval) Set the inactivity time of the session, Getmaxinactiveinterval () Gets the active inactivity time of the session, and RemoveAttribute (String key) deletes the value of the key corresponding to the session:
<% New Object (); Session.setattribute ("key", obj); Session.getattribute ("key"); Session.setmaxinactiveinterval (+); Session.getmaxinactiveinterval (); Session.getid (); Session.removeattribute ("key"); Session.invalidate ();%>
Built-in Object five (application):
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. Application common methods are setattribute (string key,object value) to store objects in application in Key/value form, getattribute (string key) Using key to get the object stored in application, Getrealpath (String path) returns the true path of the relative path:
<% New Object (); Application.setattribute ("key", obj); Application.getattribute ("key"); Application.getrealpath (" path name");%>
Built-in Object Six (PageContext):
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. A common method of PageContext is Getrequest () Gets the request object, GetResponse () Gets the response object, GetSession () obtains the Session object, Getout () Gets the Out object, SetAttribute (string key,object value) holds the property, GetAttribute (string key) Gets the property, the include ("url") requests the specified resource, and the response result of the target resource is included in the response of the calling page:
<% New Object (); Pagecontext.getrequest (); Pagecontext.getresponse (); Pagecontext.getsession (); Pagecontext.getout (); Pagecontext.setattribute ("key", obj); Pagecontext.getattribute ("key"); PageContext. Include (" resource Address");%>
Built-in Object seven (page):
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.
Built-in object Eight (config):
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.
Built-in Object nine (Exception):
The purpose of the exception object is to display exception information that can be used only on pages that contain iserrorpage= "true", which will not compile JSP files in a generic JSP page. The Excepation object, like all objects in Java, has a system-provided inheritance structure. The exception object almost defines all exception conditions. In Java programs, you can use the Try/catch keyword to handle exceptions, and if an exception is not caught in the JSP page, the exception object is generated and the exception object is routed to the error page set in the page directive. The corresponding exception object is then processed in the error page.
Since the latter three objects are seldom used in JSP pages, there is not much to be described here.
Black Horse programmer "jsp nine large built-in objects and four scopes" reproduced