one, JSP nine large built-in objects
The JSP has predefined 9 such objects, namely, request, response, session, application, out, PageContext, config, page, exception
1. Request Object
The Request object is an object of type Javax.servlet.httpServletRequest. This object represents the client's request information and is used primarily to accept data sent to the server through the HTTP protocol. (including header information, System Information, request method, request parameters, etc.). The request object is scoped to the requested one time.
2, Response object
Response represents the response to the client, primarily by passing objects processed by the JSP container back to the client. The response object also has scope, and it is only valid within the JSP page.
3. Session Object
The Session object is an object that is automatically created by the server that is related to a 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 map class is used internally by the session object to hold the data, so the format for saving the data is "Key/value". The value of a session object can make a complex object type, not just a string type.
4, Application Object
The Application object can store information in the server until the server shuts down, otherwise the information saved in the Application object is valid throughout the application. The Application object has a longer life cycle than the session object, similar to the system's global variables.
5. Out Objects
An Out object is used to output information within a Web browser and to manage the output buffers on the application server. When using out objects to output data, the data buffer can be manipulated, the residual data in the buffer is cleared in time, and the buffer space for other output is made. When the data output is finished, the output stream should be turned off in time.
6, PageContext Object
PageContext object is to obtain any range of parameters, through which you can get the JSP page out, request, reponse, session, application and other objects. The creation and initialization of the PageContext object is done by the container, and the PageContext object can be used directly from the JSP page.
7. config Object
The main role of the Config object is to obtain configuration information for the server. You can get a config object 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.
8, Page Object
The Page object represents the JSP itself and is valid only 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.
9, Exception object
The purpose of the exception object is to display exception information, which can only be used in a page containing iserrorpage= "true", and it will not be possible to compile the JSP file using the object in a generic JSP page. The Excepation object, like all objects in Java, has a system-supplied inheritance structure. The exception object almost +-+ all the anomalies. In Java programs, you can use the Try/catch keyword to handle an exception, 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 instruction. The corresponding exception object is then processed in the error page.
Second, redirect and forward
Compare items |
Forward |
redirect |
Call method |
Req.getrequestdispatcher ("acctmain.jsp"). Forward (req, resp) |
Resp.sendredirect (URL); |
Number of requests sent by client |
1 |
2 |
Whether to share Request,response objects |
Is |
Whether |
Resource Address restrictions |
Only within this application |
No Limit |
Browser Address bar |
Address does not change |
Address Change |
Third, the status of the management of Cookies
1. The role of Cookies: Client Management status (data)
2. Maintain a set of data between the server-client, transmitted using HTTP header Set-cookie domain for transmission
3. Use of cookies:
Cookie C = new Cookie ("CookieName", "Cookievalue");
Response.addcookie (c);
4. How to access cookies
Cookie[] cs = req.getcookies ();
if (CS!= null) {
for (Cookie C:cs) {
if (C.getname (). Equals ("IsLogin") && c.getvalue (). Equals ("true")) {
Return
}
}
}
Modify Cookie:c.setvalue ("NewValue"), after the modification is completed, you need to Addcookie again
5. Lifetime of Cookies
You can call C.setmaxage to set the lifetime, with parameters divided into three different cases:
Seconds>0: The maximum time the browser wants to save a cookie is a set parameter value that, if the specified time is exceeded, the browser deletes the cookie. The cookie is saved on the hard disk seconds=0: Deletes the cookie. After modifying the cookie's lifetime of 0, as the response is sent back to the client, replacing the original cookie is due to the lifecycle of the cookie deletion seconds<0: Default, the browser saves cookies to memory
6. Restrictions on the use of cookies
1) can be disabled by the client, and if the client disables cookies, then the function that relies on and implements it will also be disabled;
2 Save and client, unsafe;
3 The amount of data saved is small, and the number of cookies is limited;
4) can only save character type data;
7.Session
Function: Save the server side and record the status of a session
Implementation: Using cookies to transfer state data between the server and the client (cannot be delivered if the client disables the cookie SessionID)
Life cycle of Session:
Open a browser from a user, to close the browser, or log in to logout
Session has expiration time, you can set timeout by configuration file, call Setmaxinactiveinterval method
8 Server
Main API methods:
The session.isnew ()//method determines whether a new session can be used to count the number of accesses
<