A total of 9 such objects are pre-defined in JSP: request, Response, session, application, out, PageContext, config, page, exception
1. Request Object Javax.servlet.http.HttpServletRequest
The Request object represents the requested information of the client 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 is scoped to the requested one time.
When the request object obtains the character of a customer-submitted kanji, a garbled problem occurs and special handling is required. First, the obtained string is encoded with iso-8859-1 and encoded in a byte array of the island, and then the array is converted to a string object as follows
A common method of request: GetParameter (String strtextname) Gets the information of the form submission.
Getprotocol () Gets the agreement used by the customer.
String Strprotocol=request.getprotocol ();
Getservletpath () Gets the page where the customer submits the information. String Strservlet=request.getservletpath ();
GetMethod () Gets the way the customer submits the information String Strmethod=request.getmethod ();
GetHeader () Gets the value of accept,accept-encoding and host in the HTTP header file, String strheader=request.getheader ();
GETRERMOTEADDR () Gets the IP address of the customer. String strip=request.getremoteaddr ();
Getremotehost () Gets the name of the client. String Clientname=request.getremotehost ();
getServerName () Gets the server name. String Servername=request.getservername ();
Getserverport () Gets the port number of the server. int Serverport=request.getserverport ();
Getparameternames () Gets the name of all parameters submitted by the client.
[HTML]View PlainCopy
- Enumeration enum = request.getparameternames ();
- while (Enum.hasmoreelements ())
- {
- Strings (String) enum.nextelement ();
- Out.println (s);
- }
2. Response Object Javax.servlet.http.HttpServletResponse
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.
With the dynamic Response ContentType property, the JSP engine responds to this property value when a user accesses a JSP page, if the page is set with the page directive ContentType property is text/html.
If you want to dynamically change this property value in response to a customer, you need to use the setContentType (String s) method of the response object to change the ContentType property value.
Response.setcontenttype (String s); Parameter S is preferable to Text/html,application/x-msexcel,application/msword and so on.
In some cases, when responding to a customer, you need to redirect the customer to another page, you can use the response Sendredirect (URL) method to implement customer redirection.
For example Response.sendredirect (index.jsp);
3. Session Object Javax.servlet.http.HttpSession
The session object is a JSP built-in object that is created automatically when the first JSP page is loaded, and completes conversation-period management. Open a browser from a client and connect to the server to start, to the client close the browser to leave the end of this server, known as a session. When a client accesses a server, it may switch between several pages of the server, and the server should know by some means that it is a client and needs a session object.
When a client accesses a JSP page on the server for the first time, the JSP engine generates a session object, assigning a string ID number, and the JSP engine sends the change ID number to the client, which is stored in a cookie so that the Session object Until the client closes the browser, the server side of the client's session object is canceled, and the conversation correspondence with the customer disappears. When the customer re-opens the browser and then connects to the server, the server creates a new session object for the customer.
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.
Public String getId (): Gets the Session object number.
public void SetAttribute (String key,object obj): Adds the object obj specified by the parameter object to the session object and assigns an index keyword to the added object.
public Object GetAttribute (String key): Gets the object that contains the keyword in the session object.
Public Boolean isnew (): Determines whether a new customer is.
4. Application Object Javax.servlet.ServletContext
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.
The Application object is generated when the server is started, and the Application object is the same until the server shuts down when the client browses through the pages of the Web site visited. However, unlike the session object, all customer application objects are the same, that is, all customers share this built-in Application object.
SetAttribute (String key,object obj): Adds the object specified by the parameter object, obj, to the Application object, and assigns an index keyword to the added object.
GetAttribute (String key): Gets the object that contains the keyword in the Application object.
5, Out Object Javax.servlet.jsp.jspWriter
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.
An output stream when an out object is used to output data to the client. The Out object is used for the output of various data. The usual methods are as follows.
Out.print (): Outputs various types of data.
Out.newline (): Outputs a newline character.
Out.close (): Closes the stream.
6. PageContext Object Javax.servlet.jsp.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.
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.
7. config Object javax.servlet.ServletConfig
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.
8 Cookie Object
A cookie is a piece of text that a Web server saves on a user's hard disk. A cookie allows a Web site to save information on a user's computer and then retrieve it later. For example, a Web site might generate a unique ID for each visitor and then save it as a cookie file on each user's machine.
Creating a Cookie object invokes the constructor of the cookie object to create a cookie object. The constructor of a cookie object has two string arguments: The cookie name and the cookie value.
For example: cookie c = new Cookie (username ", John"); The cookie object is passed to the client.
In a JSP, you can use the Addcookie () method of the Response object if you want to deliver the encapsulated cookie object to the client.
For example: Response.addcookie (c), read the cookie saved to the client.
Using the GetCookie () method of the Request object, the cookie object from all clients is executed as an array, and if you want to remove the cookie object that meets your needs, you need to loop through the keywords of each object in the array. Sets the effective time of the cookie object, using the Setmaxage () method of the cookie object to set the effective time of the cookie object,
For example: Cookie c = Newcookie (username "," John "); C.setmaxage (3600);
A typical application of a cookie object is used to count the number of visitors to a website. Because of the use of proxies, caches, and so on, the only way to help the site accurately count the number of visitors is to create a unique ID for each visitor. Using cookies, websites can do the following:
Determine how many people have visited. Determine how many visitors are new (that is, the first visit) and how many are old users.
Determine how often a user visits a website. When a user first accesses, the site establishes a new ID in the database and transmits the ID to the user via a cookie. When the user visits again, the website adds 1 to the counter of the user ID to get the number of visitors.
9. Exception Object Java.lang.Throwable
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
What is the role and usage summary of JSP nine built-in objects?