Java nine large built-in objects

Source: Internet
Author: User

Java nine large built-in objects
1. Request Object

The object encapsulates the information submitted by the user by invoking the object's corresponding method to obtain the encapsulated information, that is, the object can be used to obtain user-submitted information.
When the request object obtains the character of a customer-submitted kanji, a garbled problem occurs and special handling is required. First, you will get the
The string is encoded with iso-8859-1 and encoded in a byte array of the island, and then the array is converted to a string object
Can. As follows:

String textContent=request.getParameter("boy");byte b[]=textContent.getBytes("ISO-8859-1");textContent=newString(b);

Common methods of Request:

1.01GetParameter (StringStrtextname) Gets the information for the form submission.StringStrname=Request. GetParameter ("Name");1.02Getprotocol () Gets the agreement used by the customer.StringStrprotocol=Request. Getprotocol ();1.03Getservletpath () Gets the page where the customer submits the information.Stringstrservlet=Request. Getservletpath ();1.04GetMethod () How to obtain the Customer submission information,Get|post.StringStrmethod =Request. GetMethod ();1.05Getheade () Gets the values of accept, accept-encoding, and host in the HTTP header file.StringStrheader =Request. GetHeader ("Accept");1.06GETRERMOTEADDR () Gets the IP address of the customer.StringStrIP =Request. GETREMOTEADDR ();1.07Getremotehost () Gets the name of the client.StringClientName =Request. Getremotehost ();1.08getServerName () Gets the server name.StringServerName =Request. getServerName ();1.09Getserverport () Gets the port number of the server.intServerPort =Request. Getserverport ();1.10Getparameternames () Gets the name of all parameters submitted by the client. Enumeration enum =Request. Getparameternames (); while(Enum.hasmoreelements ()) {StringS= (String) enum.nextelement (); Out.println (s);}

2. Response Object
Respond dynamically to customer requests and send data to the client.
2.1 Dynamic Response ContentType Properties
When a user accesses a JSP page, the JSP engine responds with this property value if the page is text/html with the page directive setting the ContentType property of the pages. If you want to dynamically change this property value in response to a customer, you need to use the setContentType (String s) method of the response object to change the ContentType property value.
Format: Response.setcontenttype (String s);
Parameter S is preferable to Text/html,application/x-msexcel,application/msword and so on.
2.2 Response redirect
In some cases, when responding to a customer, you need to redirect the customer to another page, you can use the response Sendredirect (URL) method to implement customer redirection. For example:
Response.sendredirect ("index.jsp");
3. Session Object
(1) What is a Session object
The session object is a JSP built-in object that is created automatically when the first JSP page is loaded, and completes conversation-period management. Open a browser from a client and connect to the server to start, to the client close the browser to leave the end of this server, known as a session. When a client accesses a server, it may switch between several pages of the server, and the server should know by some means that it is a client and needs a session object.
(2) ID of Session object
When a client accesses a JSP page on the server for the first time, the JSP engine generates a session object, assigning a string ID number, and the JSP engine sends the change ID number to the client, which is stored in a cookie so that the Session object Until the client closes the browser, the server side of the client's session object is canceled, and the conversation correspondence with the customer disappears. When the customer re-opens the browser and then connects to the server, the server creates a new session object for the customer.
(3) Common methods of Session objects
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 assigns an index keyword to the added object.
public Object GetAttribute (String key): Gets the object that contains the keyword in the session object.
Public Boolean isnew (): Determines whether a new customer is.
4. Application Object
(1) What when Application object
The Application object is generated when the server is started, and the Application object is the same until the server shuts down when the client browses through the pages of the Web site visited. However, unlike the session object, all clients ' application objects are the same, that is, all customers share this built-in Application object.
(2) Common methods of application objects
SetAttribute (String key,object obj): Adds the object specified by the parameter object, obj, to the Application object, and assigns an index keyword to the added object.
GetAttribute (String key): Gets the object that contains the keyword in the Application object.
5. Out Object
An output stream when an out object is used to output data to the client. The Out object is used for the output of various data. The usual methods are as follows.
Out.print (): Outputs various types of data.
Out.newline (): Outputs a newline character.
Out.close (): Closes the stream.
6. Cookie Object
(1) What is a cookie
A cookie is a piece of text that a Web server saves on a user's hard disk. A cookie allows a Web site to save information on a user's computer and then retrieve it later.
For example, a Web site might generate a unique ID for each visitor and then save it as a cookie file on each user's machine.
If a user accesses the Web using IE browser, the user will see all cookies saved on their hard drive. The most common places they store are: C:\Windows\Cookies. A cookie is a record that is saved in the format "keyword key= value".
(2) Create a Cookie object
A cookie object can be created by invoking the constructor of the cookie object. The constructor of a cookie object has two string arguments: The cookie name and the cookie value.
For example: cookie c = new Cookie ("username", "John");
(3) Transferring the cookie object to the client
In JSP, if you want to transfer encapsulated cookie objects to the client, you can use the Addcookie () method of the Response object.
For example: Response.addcookie (c).
(4) Read the cookie saved to the client
Using the GetCookie () method of the Request object, the cookie object from all clients is executed as an array, and if you want to remove the cookie object that meets your needs, you need to loop through the keywords of each object in the array.
For example:
Cookie[] C = request.getcookies ();
if (c! = null)
for (int i = 0;i < c.length;i++) {
if ("username". Equals (C.getname ()))
Out.println (C.getvalue ());
}
(5) Setting the effective time of the cookie object
The Setmaxage () method of the cookie object can be invoked to set the effective time of the cookie object.
For example: cookie c = new Cookie ("username", "John");
C.setmaxage (3600);
(6) Cookie Application
A typical application of a cookie object is used to count the number of visitors to a website. Because of the use of proxies, caches, and so on, the only way to help the site accurately count the number of visitors is to create a unique ID for each visitor. Using cookies, websites can do their job.
Determine how many people have visited.
Determine how many visitors are new (that is, the first visit) and how many are old users.
Determine how often a user visits a website
When a user first accesses, the site establishes a new ID in the database and transmits the ID to the user via a cookie. When the user visits again, the website adds 1 to the counter of the user ID to get the number of visitors.
7. config object "not used"
Configuration objects
The Page object.
The config built-in object is an instance of the ServletConfig class that is used by the JSP engine to pass information to it (servlet) through config when the servlet is initialized. This information can be a property name/value matching parameter, or it can be information about the server that is passed through the ServletContext object. Typically, in JSP development, the use of config built-in objects is less used, only if you need to overload the servlet's init () method when writing a servlet.

Common methods for config objects

    getServletContext():返回 一个含有服务器相关信息的ServletContext对象。    getIntParameter(String name):返回初始化参数的值。    getIntParameterNames():返回包含了Servlet初始化所需要的所有参数,返回类型是枚举型。

PageContext object "not used"
The PageContext built-in object is a special object that is the largest integrator of all other object features in the page, even though it provides access to all other objects on this page, such as the request, response, out, and Page objects already described earlier. Since objects such as request and response can be used directly by calling methods in JSP, PageContext objects are seldom used in the actual JSP development.
Common methods for PageContext objects

  getrequest (): Returns the Request object from the current page.     GetResponse (): Returns the Response object in the current page.     GetSession (): Returns the Session object in the current page.     Getservletcontext (): Returns the Application object in the current page.     GetPage (): Returns the Page object in the current screen.     Getout (): Returns the Out object in the current page.     GetException (): Returns the exception object in the current page.     Getservletconfig (): Returns the Config object from the current page.     SetAttribute (String name): Sets the property value for the specified property name.     GetAttribute (String naem): The corresponding property value is found based on the property name.     SetAttribute (String name, Object obj, int scope): Sets the corresponding property value within the given scope.     GetAttribute (String name, int scope): Gets the corresponding property value within the given range.     Findattribute (String name): looks for an attribute and returns null if it is not found.     RemoveAttribute (String name): Removes an attribute from the property name.     RemoveAttribute (String name, int scope): Deletes a property in a specified range.     Getattributescope (String name Scope): Returns the scope of a property.      Getattributenamesinscope (int scope): Returns an enumeration of all property names within the specified range.      Release (): Releases all data occupied by PageContext.      Forward (String Relativeurlpath): Redirects to another page using the current page. Include (String Relativeurlpath): Uses another page that is contained in the current location. 

8. Page object "not used"
The Page object is somewhat similar to the this pointer in Java programming, meaning the current JSP page itself. The page is an object of the Java.lang.Object class. Page objects are not often used in the actual development process.
Common Methods for Page objects
GetClass (): Returns the class of the object at that time.
Hashcode (): Returns the hash code of the object at this time.
ToString (): Converts the object class at this point to a string.
Equals (Object OB): Compares whether this object is equal to the specified object.
Copy (Object OB): Copies this object to the specified object.
Clone (): Clones this object.
9. Exception Object
The exception implicit object can be accessed directly in the Web page that handles the exception.
Page Context Object
JSP introduces a class of Fame PageContext that provides access to many of the properties of a page.
The PageContext class has methods such as getrequest,getresponse,getout,getsession.
The PageContext variable stores the value of the PageContext object associated with the current page.
Tween
If the method requires access to multiple page-related objects,
It is easier to pass PageContext than to pass independent references such as Request,response,out. (Although both methods can achieve the same purpose)

Java nine large built-in objects

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.