Lesson 4-JSP-Explanation of nine built-in objects

Source: Internet
Author: User
Tags map class

Lesson 4-JSP-Explanation of nine built-in objects
1. Basic Knowledge

1. request object

 

This object encapsulates the information submitted by the user. You can call the corresponding method of this object to obtain the encapsulated information.
Obtain information submitted by the user.
When the Request object obtains the Chinese characters submitted by the customer, garbled characters may occur and special processing is required. First
The string is encoded with a ISO-8859-1, And the encoding is stored in a byte array, and then the array is converted into a string object
You can. As follows:

 

String textContent=new String(request.getParameter(boy).getBytes(ISO-8859-1),GBK);

 

Common Request methods:

 

1.01 getParameter (String strTextName) obtains information about form submission.

 

 

 

String strName=request.getParameter(name);
1.02 getProtocol () obtains the protocol used by the customer.

 

 

 

 

String strProtocol=request.getProtocol();
1.03 getServletPath () page for obtaining information submitted by the customer.

 

 

 

 

String strServlet=request.getServletPath();
1.04 getMethod (): get | post.

 

 

String strMethod = request.getMethod();

 

1.05 getHeade () obtains the values of accept, accept-encoding, and Host in the HTTP header file.

 

 

 

String strHeader = request.getHeader(accept);
1.06 getRermoteAddr () obtains the customer's IP address.

 

 

 

 

String strIP = request.getRemoteAddr();
1.07 getRemoteHost () gets the client name.

 

 

 

 

String clientName = request.getRemoteHost();
1.08 getServerName () gets the server name.

 

 

 

 

String serverName = request.getServerName();
1.09 getServerPort () gets the server port number.

 

 

 

 

int serverPort = request.getServerPort();
1.10 getParameterNames () obtains the names of all parameters submitted by the client.

 

 

Enumerationenum=request.getParameterNames();while(enum.hasMoreElements()){    String s=(String)enum.nextElement();    out.println(s);}
2. response object

Response refers to the response to the client, making a dynamic response to the customer's request, and sending data to the client.

2.1 Dynamic Response contentType attribute
When a user accesses a JSP page, if the page uses the page command to set the page's contentType attribute text/html, the JSP Engine will respond according to this attribute value. If you want to dynamically change the value to respond to the customer, you need to use the setContentType (String s) method of the Response object to change the value of the contentType.
Format:

 

Response. setContentType (String s); // The parameter s can be text/html, application/x-msexcel, and application/msword.
2.2 Response redirection
In some cases, when responding to a customer, you need to redirect the customer to another page. You can use the sendRedirect (URL) method of Response to redirect the customer.

 

 

response.sendRedirect(index.jsp);

 

 

3. session Object

A session object is a user request-Related Object automatically created by the server. The server generates a session object for each user to save the user's information and track the user's operation status. The session object uses the Map class to save data. Therefore, the format of the stored data is "Key/value ". The value of the session object can make complex object types, not limited to string types.

(1) What is a Session object?
The Session object is a JSP built-in object. It is automatically created when the first JSP page is loaded to complete Session management. A session starts when a client opens a browser and connects to the server, and ends when the client closes the browser and leaves the server. When a client accesses a server, it may switch between several pages of the server. The server should know in some way that this is a customer and the Session object is required.
(2) Session Object ID
When a customer visits a JSP page on the server for the first time, the JSP engine generates a Session object and assigns a String-type idnumber. the JSP engine also sends this idnumber to the client, store the Session object in the Cookie so that the Session object will not be canceled on the server until the client closes the browser and the Session relationship with the client disappears. When the client re-opens the browser and connects to the server, the server creates a new Session object for the client.
(3) common methods of Session objects

 

Public String getId () // gets the Session object number. Public void setAttribute (String key, Object obj) // Add the Object obj specified by the parameter Object to the Session Object and specify an index keyword for the added Object. Public Object getAttribute (String key) // gets the Object containing the keyword in the Session Object. Public Boolean isNew () // determines whether it is a new customer.


 

4. application Object

The application object can save the information on the server until the server is closed. Otherwise, the information saved in the application object will be valid throughout the application. Compared with the session object, the application object has a longer life cycle, similar to the system's "global variable ".

(1) What is an Application object?
The Application object is generated after the server is started. When the client browses the pages of the website that the client accesses again, the Application object is the same until the server is closed. However, different from the Session object, the Application object of all customers is the same, that is, all customers share this built-in Application object.
(2) common methods of Application objects

SetAttribute (String key, Object obj) // Add the Object obj specified by the parameter Object to the Application Object and specify an index keyword for the added Object. GetAttribute (String key) // obtain the object containing the keyword in the Application object.

 

5. out object

The out object is used to output information in a Web browser and manage the output buffer on the application server. When you use an out object to output data, you can operate on the data buffer, and immediately clear the residual data in the buffer, giving up the buffer space for other outputs. After the data is output, close the output stream in time.

 

Out. print () // output various types of data. Out. newLine () // output a line break. Out. close () // close the stream.

 

6. pageContext object

The pageContext object is used to obtain parameters of any range. It can be used to obtain out, request, reponse, session, application, and other objects of the JSP page. The creation and initialization of the pageContext object are completed by the container. You can directly use the pageContext object on the JSP page.

 

The pageContext object provides access to all the objects and namespaces on the JSP page, that is, it can access the SESSION on this page, or take a property value of the application on this page, it is equivalent to the aggregator of all functions on the page. Its Class Name is also called pageContext. The pageContext object can be called a page context object during literal translation. It represents the running attributes of the current page. Common methods include findAttribute, getAttribute, getAttributesScope, and getAttributeNamesInScope.
In general, not many pageContext objects are used. Page properties are used for processing only when the project is complex.
PageContext indicates a javax. servlet. jsp. PageContext object. It is used to facilitate access to various namespaces and servlet-Related Object APIs, and encapsulates common servlet-related functions.

 

Forward (String UriPath) // redirect getAttribute (String name, [int scope]) // optional scope. It is used to retrieve the range of a specified named object. You can also call the getAttributeNamesInScope () method to retrieve the enumeration of each attribute String name in a specific range. GetException () // returns the current exception object getRequest () // returns the current request object getResponse () // returns the current response object getServletConfig () // return the ServletConfig object getServletContext () on the current page // return the ServletContext object. This object is shared with all pages. getSession () // returns the current session object findAttribute () // It can be used to follow the page, request, sessions and application range order are used to search for a named attribute. SetAttribute () // you can set the default page range or named objects in a specific range. RemoveAttribute () can be used to delete named objects in the default range or within a specific range.

 


7. config object

The main function of the config object is to obtain the configuration information of the server. You can use the getServletConfig () method of the pageConext object to obtain a config object. When a Servlet is initialized, the container passes some information to the Servlet through the config object. Developers can provide initialization parameters for Servlet programs and JSP pages in the application environment in the web. xml file.

Common methods include getInitParameter and getInitParameterNames to obtain the parameters during Servlet initialization.

8. page Object

The page object represents the JSP itself and is valid only on the JSP page. The page implicit object essentially contains the variables referenced by the current Servlet interface, similar to the this pointer in Java programming. It allows you to access many attributes of a page. Such as out, request, and response.

9. exception object

The exception object is used to display exception information. It can only be used on pages that contain isErrorPage = true. Using this object on common JSP pages will not be able to compile JSP files. The annotation object is the same as all Java objects and has the inheritance structure provided by the system. The exception object defines almost all exceptions. In Java programs, you can use the try/catch keyword to handle exceptions. If exceptions are not caught on the JSP page, an exception object is generated, transfer the exception object to the error page set in the page command, and then process the exception object on the error page.












Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog master Xiaoshu (qsuron)




Related Article

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.