Java EE built-in objects and attribute ranges

Source: Internet
Author: User
Tags ranges

Built-in objects and attribute ranges
    • Four range of attributes
    • Nine built-in objects
1. Built-in Objects

If you want to use an object, it must be new, but in our JSP operation, we find that the Out,request object we used is not instantiated, similar to this object, which we call the JSP's built-in object, That is, there are 9 objects that do not need to be instantiated .

Serial number

Object

Type

1

PageContext

Javax.servlet.jsp.PageContext

2

Request

Javax.servlet.http.HttpServletRequest

3

Response

Javax.servlet.http.HttpServletResponse

4

Session

Javax.servlet.http.HttpSession

5

Application

Javax.servlet.ServletContext

6

Config

Javax.servlet.ServletConfig

7

Out

Javax.servlet.jsp.jspWriter

8

Page

Java.lang.Object

9

exception

Java.lang.Throwable

2. Attribute Scope

In JSP, there are four kinds of attribute range, so-called attribute scope, that is, the property is set in a JSP, whether it can be accessed on other pages. The four attribute ranges refer to the following four types, respectively:

    •       attribute scope is valid on the current page: Set a property, the page of the setting property can get the property, jump to other page cannot get the property of setting The
    •       attribute range is valid within the service-side jump range: one page sets the properties and then jumps through the server (n times), you can still get the properties of the set
    •       The attribute scope is valid in one session: A page setting property, either through a server or a client jump, as long as the browser does not restart, the properties set can be obtained. The
    •       attribute scope is valid in the context of the server: the properties of the page set, whether it is a client or a service-side jump, Regardless of the session, the properties that are set can be accessed as long as the server does not restart.

NO

Method

Describe

1

public void SetAttribute (String name,object val)

Setting properties

2

Pulblic Object getattribute (String name)

Get Properties

3

public void RemoveAttribute (String name)

Delete Property

2.1page attribute Range

The page property range is the first attribute range we mentioned above, using the PageContext built-in object set and obtained, referring to a page using PageContext set the properties, this screen can be PageContext gets the properties of the setting that cannot be set after jumping to another page. It is important to note that the so-called page attribute range is set and obtained using the PageContext built-in object.

<% @page contenttype= "text/html; Charset=utf-8 "%><%    // Set Properties    Pagecontext.setattribute (" Company "," Xiamen ");    Pagecontext.setattribute ("Address", "Xiamen City");    Pagecontext.setattribute ("Tel", "0592-2565166"); %><!--Gets the properties of the set-->

If we jump through the server now, see if we can get the properties of the set

<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->

When we find that we cannot get the properties of the page range before we jump through the server , we have to use the second property range if we want to get the properties of the set after we jump through the server.

2.2request Attribute Range

The Request property scope is the second property scope, which means that the Set property is available on the current page and can be obtained after jumping through the server .

 <% @page contenttype= "text/html; charset=utf-8"%><% //  set property  Request.setattribute ("Company", "Xiamen"  "Address", "Xiamen City"  tel "," 0592-2565166 " %><!--Gets the properties of the set-->
<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->

Pass

<% @page contenttype= "text/html; Charset=utf-8 "%><%    // Set Properties    Request.setattribute (" Company "," Xiamen ");    Request.setattribute ("Address", "Xiamen");    Request.setattribute ("Tel", "0592-2565166"); %><!--Gets the properties of the set-->

Now, we find that therequest scope attribute can still get the set properties after several jumps, so what if the client jumps now?

<% @page contenttype= "text/html; Charset=utf-8 "%><%    // Set Properties    Request.setattribute (" Company "," Xiamen ");    Request.setattribute ("Address", "Xiamen");    Request.setattribute ("Tel", "0592-2565166"); %><!--Gets the properties of the set-->
<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->

Through the program running results found that through the client jump can not get the settings of the request scope of the properties, then if I now want to get the properties set by the client? Then we can use the third Range property.

3.session

The scope of the session attribute is our third attribute range, which is mentioned previously, and the attribute scope is valid in a single session. Regardless of whether the client or the server jump, you can get the settings of the properties, of course, if the browser can not restart.

<% @page contenttype= "text/html; Charset=utf-8 "%><%    // Set Properties    Session.setattribute (" Company "," Xiamen ");    Session.setattribute ("Address", "Xiamen");    Session.setattribute ("Tel", "0592-2565166"); %><!--Gets the properties of the set-->
<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->
<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->

The above program describes the properties of our session scope, after the client or server jump, set the properties can still be obtained. Now let's reboot the browser: by Operation Discovery, after restarting the browser, the set Session scope property is not available, then if you want to get the property set after the browser restarts, we must use the fourth attribute range.

4.application Attribute Range

The application Range property is the fourth Range property, which is set in the context of the server, and the properties are all available as long as the server does not restart. You can get the properties of the settings, whether you're on the client side or the server or browser restarts.

<% @page contenttype= "text/html; Charset=utf-8 "%><%    // Set Properties    Application.setattribute (" Company "," Xiamen ");    Application.setattribute ("Address", "Xiamen");    Application.setattribute ("Tel", "0592-2565166"); %><!--Gets the properties of the set-->
<% @page contenttype= "text/html; Charset=utf-8 "%><!--Get the Set property-->

Set the properties of the application range, whether through the client or the server or the browser restart, you can get the property set. So now what if the server restarts? By Operation Discovery, after restarting the server, the properties that were previously set are not taken, so if you want to get the set properties after restarting the server? There is no way to do that.

3. Attribute Scope Usage Instructions

In the development, we commonly use the attribute scope is the request and the session scope, then in the development, how to distinguish between using the two? In general, use the request scope property only for the delivery of properties of the same feature . So for the session range, it is mainly used for landing above.

4. Further instructions on the PageContext attribute

Above we are explaining four kinds of attribute range, in fact four kinds of attribute range are set through PageContext, in PageContext,setAttribute () mode is actually overloaded:

• Set properties:

· Public abstract void SetAttribute (java.lang.String name, java.lang.Object value)

· Public abstract void SetAttribute (java.lang.String name,

Java.lang.Object value,

int scope)

For these two methods, the first method we used to use, for the second method, compared to the first method more than one parameter (int scope), actually this parameter is the parameter of the specified attribute range:

· Application_scope

· Page_scope

· Session_scope

· Request_scope

That is, we can set four property ranges by this method.

<% @page contenttype= "text/html; Charset=utf-8 "%><%    pagecontext.setattribute (" j1m "," 111 ", Pagecontext.session_scope); %><a href= "pagecontextdemo02.jsp" > Jump </a>
<% @page contenttype= "text/html; Charset=utf-8 "%>

Summary, know nine built-in objects (pagecontext,session,request,response,applicatino,config,page,exception), get a good grasp of four attribute ranges (page only values for its own page) The request is applied to the service-side pass value, but the client jump cannot pass the value; The session should normally be saved in a session by the user's login registration, the end of the conversation, the property value disappears; The application attribute scope applies to the entire server, and the server shutdown property value disappears.

Java EE built-in objects and attribute ranges

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.