JSP nine large built-in objects of the role and usage summary? __jsp

Source: Internet
Author: User
Tags map class unique id

There are 9 such objects predefined in the JSP, namely request, response, session, application, out, PageContext, config, page, exception 1, Request Object Javax.servlet.http.HttpServletRequest

The Request object represents the requested information of the client and is used primarily to accept data that is delivered to the server through the HTTP protocol. (including header information, System Information, request method, request parameters, etc.). The request object is scoped to the requested one time.

When the request object gets the characters submitted by the customer, there will be garbled problems and special treatment must be done. First, the acquired string is encoded with iso-8859-1, and the encoding is encoded in a byte array of the island, and then the array is converted to a string object as follows

The commonly used method of request: GetParameter (String strtextname) Gets the information submitted by the form.

Getprotocol () Gets the protocol used by the customer.

String Strprotocol=request.getprotocol ();

Getservletpath () Gets the page where the customer submits the information. String Strservlet=request.getservletpath ();

GetMethod () Gets the way the customer submits the information String Strmethod=request.getmethod ();

GetHeader () Gets the value of accept,accept-encoding and host in the HTTP header file, String strheader=request.getheader ();

GETRERMOTEADDR () Gets the client's IP address. String strip=request.getremoteaddr ();

Getremotehost () Gets the name of the client. String Clientname=request.getremotehost ();

getServerName () Gets the server name. String Servername=request.getservername ();

Getserverport () Gets the port number of the server. int Serverport=request.getserverport ();

Getparameternames () 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 Javax.servlet.http.HttpServletResponse

Response represents the response to the client, primarily by passing objects processed by the JSP container back to the client. The response object also has scope, and it is only valid within the JSP page.

With the dynamic response ContentType property, when a user accesses a JSP page, the JSP engine responds with the property value if the page ContentType property is text/html.

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.

Response.setcontenttype (String s); Parameter s are preferable to text/html,application/x-msexcel,application/msword and so on.

In some cases, when responding to a customer, you need to reboot the customer to another page, and you can use the response Sendredirect (URL) method to achieve customer redirection.

For example Response.sendredirect (index.jsp); 3, Session object Javax.servlet.http.HttpSession

The session object is a JSP built-in object that is automatically created when the first JSP page is loaded, and is completed for conversation 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.

When a customer first accesses a JSP page on a server, the JSP engine produces a session object and assigns a string ID number, which the JSP engine sends to the client at the same time, stored in a cookie, so that the session object, The client's session object is canceled on the server side until the client closes the browser, and the conversation relationship with the customer disappears. When the client reopened the browser and then connected to the server, the server creates a new session object for the client.

The Session object is an object that is automatically created by the server that is related to a user request. The server generates a Session object for each user that holds the user's information and tracks the user's operational status.

The map class is used internally by the session object to hold the data, so the format for saving the data is "Key/value". The value of a session object can make a complex object type, not just a string type.

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 (): Determine if it is a new customer. 4, Application object Javax.servlet.ServletContext

The Application object can store information in the server until the server shuts down, otherwise the information saved in the Application object is valid throughout the application. The Application object has a longer life cycle than the session object, similar to the system's global variables.

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, when different from the session object, all customers have the same application object, that is, all customers share the built-in Application object.

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

An Out object is used to output information within a Web browser and to manage the output buffers on the application server. When using out objects to output data, the data buffer can be manipulated, the residual data in the buffer is cleared in time, and the buffer space for other output is made. When the data output is finished, the output stream should be turned off in time.

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, PageContext object Javax.servlet.jsp.PageContext

PageContext object is to obtain any range of parameters, through which you can get the JSP page out, request, reponse, session, application and other objects.

The creation and initialization of the PageContext object is done by the container, and the PageContext object can be used directly from the JSP page.

The Page object represents the JSP itself and is valid only within the JSP page. The page implied 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 role of the Config object is to obtain configuration information for the server. You can get a config object by using the Getservletconfig () method of the Pageconext object. When a servlet initializes, the container passes some information through the Config object to the servlet. Developers can provide initialization parameters for servlet programs and JSP pages in the application environment in the Web.xml file. 8 Cookie Object

A cookie is a piece of text that the Web server saves on a 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.

Creating a Cookie object invokes the constructor of the cookie object to create a 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"); The cookie object is routed to the client.

JSP, you can use the Addcookie () method of the Response object if you want to pass the encapsulated cookie object to the client.

For example: Response.addcookie (c) to read cookies saved to the client.

Using the GetCookie () method of the Request object, the cookie object from all clients is arranged in an array, and the keyword for each object in the array needs to be compared by looping if the cookie object is to be taken out. Sets the valid time for the cookie object, using the Setmaxage () method of the cookie object to set the valid time for the cookie object.

For example: Cookie c = Newcookie (username ", John"); C.setmaxage (3600);

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. Using cookies, a Web site can do the following:

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. 9, exception object Java.lang.Throwable

The purpose of the exception object is to display exception information, which can only be used in a page containing iserrorpage= "true", and it will not be possible to compile the JSP file using the object in a generic JSP page.

The Excepation object, like all objects in Java, has a system-supplied inheritance structure.

The exception object defines almost all exceptions. In Java programs, you can use the Try/catch keyword to handle an exception, and if an exception is not caught in the JSP page, the exception object is generated and the exception object is routed to the error page set in the page instruction. The corresponding exception object is then processed in 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.