Five types of built-in objects (out,request,response,session,application) most commonly used by JSPs

Source: Internet
Author: User

To simplify the development process, JSP provides some built-in objects that are implemented and managed by the container. Developers do not need to be declared in JSP pages, and can be used without instantiation . There are mainly nine out,request,response,session,application,pagecontext,page,config and exception, of which the first five are most commonly used.

Out

The Out object opens the output stream for the customer and sends the output stream to the client. In simple terms, the data is output to the client.

Common methods:

Out.print ("Hello World");//Output to BrowserOut.print (23.5f);//Ibid.,can output basic type, character array, date and many other typesOut.println ("Hello World");//output and line wrappingOut.isautoflush ();//determine if the buffer is automatically refreshedOut.getbuffersize ();//Get buffer sizeOut.getremaining ();//Get buffer remaining space sizeOut.flush ();//clears the data in the buffer and outputs it to the client displayOut.clear ();//clears the data in the buffer and does not output to the clientOut.close ();//turn off the output stream

Request

When a user accesses a Server page, an HTTP request is submitted. The request object built into the JSP encapsulates the information that the user submits , and the developer can get the information from the user submission form.

Examples of common methods:

Request.getmethod ();//gets the request method, usually a GET or postRequest.setattribute ("Age", 20);//set a property called Age, and assign a value ofRequest.getattribute ("Age");//Gets the value of the age property above, and returns null if the property does not existRequest.removeattribute ("Age"); Remove the attribute value for name age
Request.getattributenames ();//returns the collection of names for all propertiesrequest.getcaracterencoding (); //returns the encoding format, usually used when solving garbled charactersRequest.getparameter ("user");//The user submits a form with a name of "user" in the Input box (node), you can get the contents of the Input Box (node) in this wayRequest.getparametervalues ("hobbies");//The user submits a check box with the name "hobbies" in the form to get the value of the selected part

Response

The server responds when it receives a request from the user. The response object encapsulates the JSP response information and is sent to the client.

Because the output stream is buffered, you can set the HTTP status code (common 404,500,504, etc.) and the response header.

Common methods:

Response.addcookie (Cookie cook);              // Add a cookie object to hold user information // adds information to the header file, overwriting Response.containsheader (String name) if it already exists;         // determines whether the HTTP header for the specified name already exists response.senderror (int);                      // returns the error message response.sendredirect (String location);       //  redirect // sets the value of the HTTP file header for the specified name. 

Session

Knock on the blackboard!! Crashed crashed crashed!!! This is important!

Many friends of the session are familiar with a strange feeling, the word is too common in development! But the understanding in many places is vague.

The session here is a built-in object to hold each user's information in order to keep track of each user's operational status .

Why keep track of user status? Because HTTP is a stateless protocol , when a client sends a request to the server, the server returns one (response), and the connection is closed (hence, a one-time connection). On the next connection, the server cannot determine whether the current connection is the same user as the previous one. (for example, when a user accesses a different page in the same site, it is not possible to access a single page to log in once ...) At this point, a session is used to record information about the connection.

Session starts from the user opening the browser and the server establishes a connection, to the end of the user closing the browser away from the server. The server writes SessionID to the browser's Cookie as a unique identifier for the user. Each user has his or her own SessionID, and the session disappears automatically when the user exits the system.

The session information is stored on the server side , and the session ID is stored in the client 's Cookie . If the client disables cookies, the session object of the same user between different pages may be different .

Commonly used methods, getattribute (), SetAttribute (), removeattribute (), etc., are the attributes of the object in the increase and deletion of the search, in this will not repeat the

For example, we can construct a simple shopping cart function module through the session.

1. User Login to Shopping mall, JSP engine automatically create session object

2. Add two attributes to the session goods (purchased goods), price, as follows

Session.setattribute ("Goods", "" ");

Session.setattribute ("Price", "");

3. On different pages (daily necessities, food) Select items to add to cart, add items to goods and price each time, for example

Session.setattribute ("Goods", Session.getattribute ("goods") + "egg");//Where Session.getattribute ("goods") You can get items added to your cart from the session

Session.setattribute ("Price", Session.getattribute ("price") +15);//Where Session.getattribute ("price") The price of all items in the current shopping cart can be obtained from the session

Application

The Application object saves shared information for all users.

Although both application and session can save information for the user, the two are different:

1. The server creates a session object for each user, each session is different , and application is the common information for multiple applications, and for a container, all users ' application objects are The same one .

The life cycle of the 2.session and application is different. The life cycle of a session starts with the user opening the browser to establish a connection to the server, and the time when the browser is closed to the end of the server, while the application life cycle is initiated from the server This period of time when the server was shut down .

Examples of scenarios:

For example, the site of the common traffic statistics, you can use application to achieve. Define a field count to count, each click on + +, and then save count in the Application object, Application.setattribute ("Counter", count); When you get the current hits, Application.getattribute ("Counter").

Five types of built-in objects (out,request,response,session,application) most commonly used by JSPs

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.