Dark Horse programmer [nine built-in JSP objects and Four Scopes] and nine jsp

Source: Internet
Author: User

Dark Horse programmer [nine built-in JSP objects and Four Scopes] and nine jsp

Nine built-in JSP objects and four scopes

------- Android training, java training, and hope to communicate with you! ----------

When learning JSP, we first need to understand the built-in JSP objects. What are the built-in objects? Built-in objects are also called implicit objects. They can be used in script code and expressions without pre-declaration. Such built-in objects have a total of nine and four scopes in JSP. We will explain them one by one in the following articles.

Now let's take a look at the four scopes to describe the scopes of each major object. Next we will look at the information sharing scopes of each major scope through a simple table:

The first scope is page, which is valid only on the current page, that is, the page requested by the user. When the current page is closed or switched to another page, the page object will be released after the response is returned to the client.

The second scope is request, which is valid in the current request. The request can pass information on the page through the setAttribute () method, or jump between pages through the forward () method, it should be noted that the request is not a redirection, and the forwarding is transparent to the browser, that is, no matter how the page jumps, the address bar still displays the original address.

The third scope is session, which is valid in the current session. When the same browser on a computer accesses the server multiple times, the information transmitted between these accesses is the scope of the session scope. When the browser sends the first HTTP request, it determines that the session starts, but the end time of the session is uncertain, because the server is not notified when the browser is closed, generally, the default time set for Tomcat is 120 minutes. You can also use the setMaxInactiveInterval (int) method to set the time, or use the invalidate () method to force the end of the current session.

The fourth scope is application, which is valid in all applications. That is, when the server starts to end with the server, the data stored in the application scope is valid, you can also use setAttribute values and getAttribute values.

Well, I believe everyone has a certain understanding of the Four Scopes. Let's take a look at nine built-in objects.

Built-in object 1 (out ):

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. The most common method is print, which displays string information on the page. The out scope is page:

<%out.print("Hello"); %>

 

Built-in object 2 (request ):

The request object is a javax. servlet. httpServletRequest object. This object represents the client request information and is mainly used to accept data transmitted to the server over HTTP. (Including header information, system information, request method, and request parameters ). The scope of the request object is one request (that is, the request scope ). Common request methods include getParameter (String name), which obtains Page Submission data based on the form component name. getParameterValues (String name) obtains the data submitted by a group of form components with the same name, setCharacterEncoding (String charset) is set before the getParameter () method is called to solve Chinese garbled characters. getRequestDispatcher (String path) returns a javax. servlet. requestDispatcher object. The forward () method of this object is used to forward requests:

<% Object obj = new Object (); request. getParameter ("uname"); request. getParameterValues ("rid"); request. setCharacterEncoding ("UTF-8"); request. getRequestDispatcher ("target page "). forward (request, response); %>

 

Built-in object 3 (response ):

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. Common Methods of response include addCookie (Cookie cookie) to add a cookie to the client, serContentType (String type) to set the contentType type of the HTTP response, setCharacterEncoding (String charset) set the character encoding type used in the response. sendRedirect (String location) locates the request to a new address. This is because the address in the address bar changes:

<% Response. addCookie (cokkie); response. setContentType ("type"); response. setCharacterEncoding ("UTF-8"); response. sendRedirect ("page to be relocated"); %>

 

Built-in object 4 (session ):

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 use the object type. Common Methods of session include setAttribute (String key, Object value) to save the Object to the session in the form of key/value, getAttribute (String key) get the value saved in the session through the key value, invalidate () forces the session object to expire, getId () gets the sessionid, setMaxInactiveInterval (int interval) sets the session inactivity time, getMaxInactiveInterval () get the valid inactivity time of the session. removeAttribute (String key) deletes the value of the corresponding key in the session:

   <%   Object obj = new Object();   session.setAttribute("key",obj);   session.getAttribute("key");   session.setMaxInactiveInterval(100);   session.getMaxInactiveInterval();   session.getId();   session.removeAttribute("key");   session.invalidate();   %>

 

Built-in object 5 (application ):

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 ". Common methods of application include setAttribute (String key, Object value) storing objects in the form of key/value in application. getAttribute (String key) obtains objects stored in application through key, getRealPath (String path) returns the actual path of the relative path:

<% Object obj = new Object (); application. setAttribute ("key", obj); application. getAttribute ("key"); application. getRealPath ("pathname"); %>

 

Built-in object 6 (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. Common Methods of pageContext include getRequest () to get the request object, getResponse () to get the response object, getSession () to get the session object, getOut () to get the out object, setAttribute (String key, object value) Save the attribute, getAttribute (String key) Get the attribute, include ("url") request the specified resource, and include the response result of the target resource in the response on the call page:

<% Object obj = new Object (); pageContext. getRequest (); pageContext. getResponse (); pageContext. getSession (); pageContext. getOut (); pageContext. setAttribute ("key", obj); pageContext. getAttribute ("key"); pageContext. include ("resource address"); %>

 

Built-in object 7 (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.

Built-in object 8 (config ):

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.

Built-in object 9 (exception ):

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.

Because the last three objects are rarely used in JSP pages, we will not introduce them here.

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.