jsp Built in Object

  • 2021-12-12 05:18:34
  • OfStack

In JSP, 1 defines nine such objects in advance, which are request, response, session, application, out, pagecontext, config, page and exception

1. request Object

The request object represents the request information of the client and is mainly used to accept the data transmitted to the server through the HTTP protocol. (including header information, system information, request mode and request parameters, etc.).

The scope of the request object is 1 request.


String s = request.getParameter("xxxx");// Received value 
request.setAttribute("key","value");// Outgoing 
request.getAttribute("key");// According to key Value reception 

2. response object

response represents the response to the client, mainly passing the objects processed by the JSP container back to the client. The response object also has scope, which is valid only within the JSP page.


response.sendRedirect("url");// Jump 
response.getWriter().append("xxxxx");// Enter content to the page 

3. session object

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

session is saved on the server. It is state saving in each session. By default, it is a 30-minute lifetime.


// Make session
HttpSession session=request.getSession();
// Write session
session.setAttribute("key","value");
// Read session
session.getAttribute("key");

4. application Object

The application object saves information on the server until the server shuts down, otherwise the information stored in the application object will be valid throughout the application. Compared with session objects, application objects have a longer life cycle and are similar to the "global variables" of the system.

This Application object is generated when the server is started, and when the client browses between pages of the website visited, this Application object is the same one until the server is shut down. However, unlike the Session object, all customers have the same Application object, that is, all customers share this built-in Application object.

setAttribute (String key, Object obj): Adds the object obj specified by the parameter Object to the Application object, and specifies 1 index key for the added object.

getAttribute (String key): Gets the object that contains the keyword in the Application object.

5. out Object

The out object is used to output information within the Web browser and to manage the output buffer on the application server. When using out object to output data, you can operate the data buffer, clear the residual data in the buffer in time, and give up buffer space for other outputs. After the data output is completed, the output stream should be closed in time.

The Out object is an output stream for outputting data to the client. Out objects are used for output of various types of data. The common methods are as follows.

out. print (): Outputs various types of data.

out. newLine (): Outputs 1 line break.

out. close (): Close the stream.

6. pageContext Object

The pageContext object is used to obtain parameters of any range, through which you can obtain out, request, reponse, session, application and other objects of the JSP page.

The creation and initialization of the pageContext object 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 legal only within the JSP page. page implicit objects essentially contain variables referenced by the current Servlet interface, similar to this pointers in Java programming.

7. config Object

The main function of the config object is to obtain the configuration information of the server. One config object can be obtained through the getServletConfig () method of the pageConext object. When an 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 that the Web server saves on the user's hard drive. Cookie allows an Web site to save information on a user's computer and then retrieve it. For example, an Web site might generate a 1-only ID for every 1 visitor and then save it as an Cookie file on each user's machine.

It is divided into temporary cookie and permanent cookie. Temporary cookie browser is gone when it is closed, so don't put it on hard. Long-term cookie is placed on the hard disk and lost when it expires.


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

The Cookie object is typically used to count the number of visitors to a Web site. Due to the use of proxy servers, caches, etc., the only way to help a website accurately count the number of visitors is to create a only 1ID for each visitor. With Cookie, the website can do the following:

Determine how many people have visited. Determine how many visitors are new users (that is, the first visit) and how many are old users.

Measure how often a user visits the website. When a user visits for the first time, the website creates a new ID in the database, and transmits ID to the user through Cookie. When the user visits again, the website adds 1 to the counter corresponding to the user ID to get the number of visits of the user.

9. exception Object

The exception object is used to display exception information and can only be used in pages containing isErrorPage= "true". Using this object in a 1-like JSP page will not compile an JSP file.

excepation objects and all objects of Java have the inheritance structure provided by the system.

The exception object defines almost all exceptions. In the Java program, you can use the try/catch keyword to handle abnormal situations; If an exception is not caught in the JSP page, an exception object is generated and passed to the error page set in the page directive, where the corresponding exception object is processed.


Related articles: