Functions and usage of nine built-in JSP objects, and nine built-in jsp objects

Source: Internet
Author: User
Tags map class

Functions and usage of nine built-in JSP objects, and nine built-in jsp objects

A total of nine 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 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.

When the Request object obtains the Chinese characters submitted by the customer, garbled characters may occur and special processing is required. First, encode the obtained string with a ISO-8859-1, and save the encoding in a byte array, and then convert the array into a string object as follows:

Common Request Methods: getParameter (String strTextName) to obtain information about form submission.

GetProtocol () gets the protocol used by the customer.

String strProtocol = request. getProtocol ();

The getServletPath () page for obtaining information submitted by the customer. String strServlet = request. getServletPath ();

GetMethod (): String strMethod = request. getMethod ();

GetHeader () obtains the values of accept, accept-encoding, and Host in the HTTP header file, String strHeader = request. getHeader ();

GetRermoteAddr () obtains the customer's IP address. String strIP = request. getRemoteAddr ();

GetRemoteHost () obtains the name of the client. String clientName = request. getRemoteHost ();

GetServerName () gets the server name. String serverName = request. getServerName ();

GetServerPort () gets the server port number. Int serverPort = request. getServerPort ();

GetParameterNames () obtains the names of all parameters submitted by the client.

Enumeration enum = request.getParameterNames();  while(enum.hasMoreElements())  {      Strings(String)enum.nextElement();      out.println(s);  }
2. response object javax. servlet. http. HttpServletResponse

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.

Has the 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 to text/html, then 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.

Response. setContentType (String s); parameter s can be text/html, application/x-msexcel, application/msword, etc.

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.

For example, response. sendRedirect (index. jsp );

3. session Object javax. servlet. http. HttpSession

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.

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.

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.

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 specifies an index keyword for the added Object.

Public Object getAttribute (String key): obtains the Object containing the keyword in the Session Object.

Public Boolean isNew (): determines whether it is a new customer.

4. application Object javax. servlet. ServletContext

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 javax. servlet. jsp. jspWriter

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 javax. servlet. jsp. PageContext

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 javax. servlet. ServletConfig

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.

You can create a Cookie object by calling the constructor of the Cookie object. The constructor of a Cookie object has two string parameters: Cookie name and Cookie value.

For example, Cookie c = new Cookie (username ", john"); send the Cookie object to the client.

In JSP, If You Want To transmit the encapsulated Cookie object to the client, you can use the addCookie () method of the Response object.

For example, response. addCookie (c) reads the Cookie stored on the client.

Use the getCookie () method of the Request object to sort all Cookie objects sent from the client in an array during execution. If you want to retrieve the Cookie objects that meet the requirements, you need to compare the keywords of each object in the array cyclically. Set the effective time of the Cookie object. You can use 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 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 java. lang. Throwable

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.

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.