Learn Java nine built-in objects _java

Source: Internet
Author: User
Tags unique id

This article examples for you to introduce the Java nine built-in objects for your reference, the specific contents are as follows
1. Request Object
This object encapsulates the information submitted by the user, and it is possible to obtain the encapsulated information by invoking the object's corresponding method, which means that the object can be used to obtain information submitted by the user.
When the request object gets the characters submitted by the customer, there will be garbled problems and special treatment must be done. First, you will get the
The string is encoded with iso-8859-1 and encodes the island into a byte array, then converts the array to a string object
Can. As follows:

String textcontent=request.getparameter ("Boy");
Byte b[]=textcontent.getbytes ("Iso-8859-1");
Textcontent=new String (b);

Common methods to request:

 1.01 getparameter (String strtextname) Gets the information submitted by the form.
  String strname=request.getparameter ("name");
1.02 Getprotocol () Gets the protocol used by the customer.
 String Strprotocol=request.getprotocol ();
1.03 Getservletpath () Gets the page where the customer submits the information.
 String Strservlet=request.getservletpath ();
1.04 GetMethod () to obtain the customer submits the information the way, Get|post.
 String strmethod = Request.getmethod ();
1.05 Getheade () Gets the values of accept, accept-encoding, and host in the HTTP header file.
 String Strheader = Request.getheader ("accept");
1.06 getrermoteaddr () Gets the client's IP address.
 String StrIP = Request.getremoteaddr ();
1.07 Getremotehost () Gets the name of the client.
 String clientname = Request.getremotehost ();
1.08 getServerName () gets the server name.
 String serverName = Request.getservername ();
1.09 Getserverport () Gets the port number of the server.
 int serverport = Request.getserverport ();
1.10 Getparameternames () Gets the name of all the parameters submitted by the client.
enumeration enum = Request.getparameternames ();
  while (Enum.hasmoreelements ()) {string s= (String) enum.nextelement ();
Out.println (s); }

The

2, Response object
responds dynamically to a customer's request and sends data to the client.
2.1 Dynamic Response ContentType Properties
When a user accesses a JSP page, if the page uses the page directive to set the ContentType property text/ HTML, the JSP engine responds with this attribute value. If you want to dynamically change this property value to respond to the customer, you need to use the Response object's setContentType (String s) method 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 redirection
In some cases, when responding to a customer, you need to reboot the customer to another page, and you can use the Sendredirect of response ( 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, It is automatically created when the first JSP page is loaded and completes session management. From a customer to open the browser and connect to the server to start, to the customer close the browser to leave the server end, is called a session. When a client accesses a server, it may switch between several pages of the server, and the server should somehow know that this is a customer and requires a session object. ID of the
(2) Session object
When a customer first accesses a JSP page on a server, the JSP engine produces a session object, Assigning a string ID number, the JSP engine sends the ID number to the client at the same time, which is stored in a cookie, so that the session object is canceled until the client closes the browser, and the conversation relationship with the client disappears. When the client reopened the browser and then connected to the server, the server creates a new session object for the client.
(3) Common method for session object
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 it is a new customer.

4, Application Object
(1) What is the Application object

This Application object is generated when the server is started, and the Application object is the same until the server is shut down when the customer browses through the pages of the site visited. However, unlike the session object, all customers have the same application object, that is, all customers share this built-in application object.
(2) Common methods of application objects
setattribute (String key,object obj): Adds the object obj specified by the parameter object 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 Objects
an Out object is an output stream that 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 (): Output various types of data.
Out.newline (): Output a newline character.
Out.close (): Closes the stream.

6, Cookie object
(1) What is a cookie
Cookie is a piece of text that the Web server holds on the user's hard disk. Cookies allow a Web site to save information on a user's computer and then retrieve it later.
For example, a Web site might produce a unique ID for each visitor, and then save it in the form of a cookie file on each user's machine.
If a user accesses the Web using IE, the user will see all cookies saved on their hard disk. The most common places they store are: C:\Windows\Cookies. Cookies are saved in the format "keyword key= value". The
(2) Creates a cookie object
invokes the constructor of the cookie object to create the cookie object. The constructor for the cookie object has two string parameters: the cookie name and the cookie value.
For example: cookie c = new Cookie ("username", "John");
(3) Send a cookie object to the client
in JSP, if you want to transfer a packaged cookie object to a client, you can use the Addcookie () of the Response object Method.
For example: Response.addcookie (c). The
(4) reads cookies saved to the client
uses the GetCookie () method of the Request object, which, when executed, arranges the cookie objects that come from all clients in the form of an array , if you want to remove the cookie object that matches your needs, you need to iterate through the keywords for 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) Set the valid time of the cookie object
You can set the valid time for a cookie object by calling the Setmaxage () method 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 the site. Because of the use of proxy servers, caching, and so on, the only way to help the site to accurately count the number of visitors is to create a unique ID for each visitor. With cookies, the site can do the job.
Determine how many people have visited.
Determine how many visitors are new (ie first visits) and how many are old users.
Determine how often a user accesses a Web site
When a user first accesses, the Web site creates a new ID in the database and transmits the ID to the user through a cookie. When the user visits again, the website adds 1 to the counter corresponding to the user ID, and gets the number of visits by the user.

7, config object "not used"
Configuration Object
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 through config to it (servlet) when the servlet is initialized. This information can be an attribute name/value matching parameter, or it can be information about the server passed through the ServletContext object. In general, the config built-in object is less used in JSP development, and is only used if you need to overload the servlet's init () method when writing the servlet.

Config Object common methods

Getservletcontext (): Returns a ServletContext object that contains information about the server.
Getintparameter (String name): Returns the value of the initialization parameter.
Getintparameternames (): Returns all the parameters that are required to contain servlet initialization, and the return type is an enumerated type.

PageContext Object "not used"

The PageContext built-in object is a special object that corresponds to the largest integrator of all other object functions in the page, even with which you can access all other objects on this page, such as the request, response, out, and page objects that have been described previously. The PageContext object is rarely used in real-world JSP development because objects such as request and response in JSP can be used by direct calling methods.

Common methods for PageContext objects

 Getrequest (): Returns the Request object on the current page.
   GetResponse (): Returns the Response object on the current page.
   GetSession (): Returns the Session object in the current page.
   Getservletcontext (): Returns the Application object on the current page.
   GetPage (): Returns the Page object in the current page.
   Getout (): Returns an Out object from the current page.
   GetException (): Returns the exception object on the current page.

   Getservletconfig (): Returns the Config object in the current page.
   SetAttribute (String name): Sets the property value for the specified property name.
   GetAttribute (String Naem): Finds the corresponding property value based on the property name.
   SetAttribute (String name, Object obj, int scope): Sets the corresponding property value within a given range.
   GetAttribute (String name, int scope): Gets the corresponding property value in the given scope.
   Findattribute (String name): looks for a property and returns null if it is not found.
   RemoveAttribute (String name): Removes a property from a 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 a specified range.
   Release (): Releases all material occupied by PageContext.
   Forward (String Relativeurlpath): Use the current page to redirect to another page.


Include (String Relativeurlpath): Use another page contained in the current location. 

8, Page object "not used"

The Page object is somewhat similar to the this pointer in Java programming, which refers to the current JSP page itself. The page is an object of the Java.lang.Object class. Page objects are not used frequently in the actual development process.
Common methods for page objects
getclass (): returns the class of object at that time.
hashcode (): Returns the hash code for the object at this time.
toString (): converts the object class at this time 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

Exception implicit objects can be accessed directly in a Web page that handles exceptions.
Page Context Object
JSP introduces a fame PageContext class that can access many of the page's properties.
The PageContext class has getrequest,getresponse,getout,getsession and other methods.
The PageContext variable stores the value of the PageContext object associated with the current page.
Complement:
If the method needs to access multiple page-related objects,
Passing PageContext is easier than passing independent references such as Request,response,out. (Although both ways can achieve the same goal)

The above is the entire content of this article, I hope to help you learn

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.