Jsp built-in object, jsp built-in

Source: Internet
Author: User

Jsp built-in object, jsp built-in

A total of nine such objects are pre-defined in JSP: request, response, session, application, out, pagecontext, config, page, exception

1. request object

The request object represents the request information of the client and is mainly used to accept data transmitted to the server over HTTP. (Including header information, system information, request method, and request parameters ).

The request scope is one request.

String s = request. getParameter ("xxxx"); // receives the request value. setAttribute ("key", "value"); // output request. getAttribute ("key"); // receives data based on the key value

2. response object

Response refers to the response to the client, mainly to pass the objects processed by the JSP Container Back to the client. The response object also has a scope, which is only valid on the JSP page.

Response. sendRedirect ("url"); // jump to response. getWriter (). append ("xxxxx"); // enter content to the page

3. session Object

It is mainly used to save the information of each user separately and the session associated with the request.

The session is saved on the server. It stores the status in each session. By default, the life cycle is 30 minutes.

// Create sessionHttpSession session = request. getSession (); // write sessionsession. setAttribute ("key", "value"); // read sessionsession. getAttribute ("key ");

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 ".

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, when it is different from the Session object, all clients share the same Application object.

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): Get the object containing keywords 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.

An output stream when an Out object is used to output data to the client. The Out object is used to output various types of data. The common method is as follows.

Out. print (): outputs various types of data.

Out. newLine (): outputs 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 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.

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.

8. cookie object

Cookie is a piece of text stored on the user's hard disk by the Web server. Cookie allows a Web site to save information on the user's computer and then retrieve it. For example, a Web site may generate a unique ID for each visitor and store the ID in the form of a Cookie file on each user's machine.

It is divided into temporary cookies and persistent cookies. When the temporary cookie browser is closed, it will be left empty. Long-lived cookies are stored on the hard disk and are lost when they expire.

// Write cookieCookie c = new Cookie ("key", "value"); response. addCookie (c); // read cookieCookie [] cc = request. getCookies (); for (Cookie c: cc ){}

A typical Cookie object is used to count the number of visitors to the website. Because of the use of proxy servers and caches, the only way to help the website accurately count the number of visitors is to create a unique ID for each visitor. With cookies, the website can do the following:

Determine the number of visitors. Determine the number of new users (the first visit) and the number of old users.

Determine how often a user visits a website. When a user visits the website for the first time, the website creates a new ID in the database and sends the ID to the user through cookies. When a user visits the website again, the counter corresponding to the user ID is added to 1 to obtain the number of visits of the user.

9. exception object

The exception object is used to display exception information. It can only be used on pages containing isErrorPage = "true". Using this object on a common JSP page will not 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.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.