Transferred from: http://www.blogjava.net/RoyPayne/archive/2012/01/05/367930.html
————————————————————————————————————————————————————
JSP built-in objects: we use JSP for page programming can be used directly without the need to create their own web containers have been created for the user good JSP built-in objects. such as Request,session,response,out and so on.
Here are the 9 built-in objects given by JSP2.0:
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 |
We can also classify them according to their role:
First Category: Related to servlets: page and Config
Type II: Related to Input/output: Out,request and response
Category III: Related to the context: Application,session and PageContext
Class Fourth: Related to Error: exception
I. Attribute Preservation scope
In JSPs, you can set and get properties by using both setattribute () and GetAttribute () methods. So as to realize the sharing of data. The JSP provides four properties to save the range: Response,request,session and application.
1, page: Is the set of properties can only be valid on the current page. Through PageContext's setattribute () and getattribute ().
2. Request: The attribute is valid within the scope of a request. If the page jumps from one page to another, the property is invalidated. The jump referred to here refers to a client jump, such as a customer clicking a hyperlink to jump to another page or browsing another page through the browser's address bar. If the server-side jump <jsp:forward> is used, the property remains in effect. Similarly, use the setattribute () and getattribute () of the Request object.
3, session: Refers to the client browser and server a session within the scope, if the server disconnects, then the attribute is invalid. Similarly, the setattribute () and getattribute () of the session object are passed. Exceptions to session scope such as reopening a browser.
4, Application: Refers to the entire server scope, know that the server will not expire after the failure. In the same vein, setattribute () and getattribute () are passed through the Application object. The application range is the saved property that can be retrieved on any page if the server does not restart, even if the browser is reopened for properties.
Second, remove the attribute: You must clear the program if you want to clear the property before the scope of the property has been reached. That is, the RemoveAttribute () method of the corresponding object can be called to remove the specified property.
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
Result page Output: Page Range: Name property value is James
Page Range: Age property value is 12
Page Range: Sex property value is null
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 |
Iv. Response object: Used to transmit output information to the client.
Method |
return value |
Method description |
Addcookie (Cookie cookie) |
void |
|
Adddateheader (String name,long date) |
void |
|
AddHeader (String name,string value) |
void |
|
Addintheader (String name,int value) |
void |
|
Setdateheader (String name,long date) |
void |
|
SetHeader (String name,string value) |
void |
|
Setintheader (String name,int value) |
void |
|
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".
V. Out object: used to output information to the page.
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 |
Vi. Session object: Used to represent the user's conversational state, typically used to hold various information about a user until the lifecycle expires or is considered released.
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 |
Vii. Application object: Used to obtain and set information about the servlet.
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 |
Viii. 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.
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 servlet that is translated by the JSP, and the Page object makes it very easy to invoke the methods defined in the Servlet class.
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 "%>
Output: This is the page Object practice.
Config object: can be used to obtain configuration information for the servlet.
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 |
Xi. 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