"Go" JSP built-in object details (top)

Source: Internet
Author: User

First question: How many types of built-in objects do JSPs have?
To answer this question, first of all, the basic knowledge and technology of JSP have a deep understanding and mastery. There are nine main
Built-in Objects: Application object, config object, exception object, out object, Page object, PageContext object, Request object, Reponse object, Session object.
The second question: What types of JSP built-in objects are they? What are the roles of these objects? What are their common methods?
(1) Application object: an example of Javax.servlet.ServletContext. This instance represents the Web application itself that the JSP belongs to and can be used to exchange information between JSPs or Servlets. The common methods are:

Method

return value type

Method description

Getmajorversion ()

Int

Get the main servlet API version

Getminorversion ()

Int

Get the minor servlet API version

Getserverinfo ()

String

Get Server version

GetMimeType ()

String

Gets the MIME type of the specified file

GetContext ()

ServerContext

Gets the application context for the specified local

Getrealpath

String

Gets the absolute path of the specified path

Getinitparameter (name)

String

Get servlet initialization parameters

Getinitparameter (String paramname) is used to obtain the configuration parameters for the Web application, which should be configured with the Context-param element in the. xml file, each of the <context-param: The/> element configures a parameter.
Note: Each Web App has only one ServletContext instance, and the JSP page can access the instance through application built-in objects, which in the servlet must be obtained by code: ServletContext sc= Getservletconfig (). Get ServletContext ();
(2) Config object: javax.servlet.ServletConfig instance, which represents the configuration information of JSP, in fact, JSP page usually does not need to be configured, there is no configuration information. More for use in Servlets.

Method

return value type

Method description

Getinitparameter (name)

String

Get servlet initialization parameters

Getinitparameternames ()

Enumeration

Get all initialization parameter names of the servlet

Getservletcontext ()

ServletContext

Get Current application context

Getservletname ()

String

Get the servlet name

(3) Exception object: java.lang.Throwable instance, this property can only be used if the page is an error handling page, that is, the Iserrorpage property of the compile instruction page is true.
(4) Out object: javax. An instance of Servlet.jsp.JspWriter that represents the output stream of a JSP page, which is used to output content and form an HTML page. The essence of <%=%> expression is the Out.writer method.

Method

return value type

Method description

Clear ()

void

Clear the output on a webpage

Clearbuffer ()

void

Clear Buffer Contents

Close ()

void

Close buffer, clear all contents

GetBufferSize ()

Int

Get buffer size

Getremaining ()

Int

Get buffer remaining size

Isautofulsh ()

Boolean

Get information about whether the buffer is automatically purged

Print (String str)

void

For page output

println (String str)

void

Page output and line wrapping

(5) Page object: Represents the page itself, usually does not have much effect. This is the this in the servlet.
(6) PageContext object: an instance of PageContext that represents the JSP page context, which can be used to access shared data for a page. Common methods are: Getservletcontext and Getservletconfig methods. See below for a detailed description.
(7) Request object: an instance of javax.servlet.http.HttpServletRequest that encapsulates a request and the client's request parameters are encapsulated inside the object. The methods for obtaining parameters are:

Method

return value type

Method description

GetParameter (String name)

String

Gets the parameter value named name for the parameter

Getparameternames ()

Enumeration

Gets the name of all parameters, which can be used in combination with the previous method to get the values of all parameters

Getparametervalues (String name)

String[]

Gets all parameters named name of the parameter, such as the parameter is multiple checkboxes

Getparametermap ()

Map

Gets the map instance encapsulated by all parameters, and returns an array of values with the corresponding parameter named ID through the map instance's string[] get ("id") method

The other methods are:

Method

return value type

Method description

GetHeader (String name)

String

Gets the header named name for the specified header

Getheadername ()

Enumeration

Get all the header names

Getintheader (String name)

Int

Gets the header named name, with the content returned as an integer type

Getdateheader (String name)

Long

Gets the header named name, with the content returned as a date type

GetCookies ()

Cookies

Get the relevant cookie

Getcontextpath ()

String

Get the path to the context

GetMethod ()

String

Get how clients are submitted

Getprotocol ()

String

Get the HTTP protocol used

GetQueryString ()

String

Gets the requested string

Getrequestsessionid ()

String

Gets the session ID of the client

Getrequesturi ()

String

Gets the requested URI

GETREMOTEADDR ()

String

Get Client IP Address

(8) Reponse object: an instance of Javax.servlet.http.HttpServletReponse that represents the response of the server to the client, usually using the object directly, rather than the out object, unless a non-character response needs to be generated. The response object is often used for redirection, and the commonly used method is the Getoutstream,sendredirect method.

Method

return value

Method description

Addcookie (Cookie cookie)

void

Adddateheader (String name,long date)

void

AddHeader (String name,string value)

void

Addintheader (String name,int value)

void

Setdateheader (String name,long date)

void

SetHeader (String name,string value)

void

Setintheader (String name,int value)

void

Senderror (int SC)

void

Transfer Status Code

Senderror (int sc,string msg)

void

Transfer status codes and error messages

SetStatus (int SC)

void

Set the status code

Sendredirect (String URL)

void

Page redirection for page jumps

Note: Here the Response object's Sendredirect (String URL) method Setting page redirection will change the browser address bar information, so also called the client jump.
The instance 1:response object implements the automatic refresh of the page: just the early JSP page plus
<%– Use the Setintheader of the response object to set the value of the refresh (in seconds) to automate the page refresh –%> <% response.addintheader ("Refresh", 10); %>
Example 2: Implementation of automatic page jump: You can use the Response object's SetHeader () method to add a header titled Refresh, and set the page jump time and jump page, so that the page automatically jump. <% Response.setheader ("Refresh", "10; Url=http://www.baidu.com "); %>
Here, use the SetHeader method to add a header titled "Refresh" with a value of "10,url=http://www.baidu.com".
(9) Session object: Javax.servlet.http.HttpServletReponse instance, represents a reply. When the client browser establishes a connection to the site, the session begins, and when the client closes the browser, the reply ends.

Method

return value type

Method description

GetId ()

String

Gets the ID of the session

GetCreationTime ()

Long

Gets the session's build time

Getlashaccessedtime ()

Long

Get user last Send request time via session

Getmaxinactiveinterval ()

Long

Gets the session life cycle, which expires if this time is exceeded

Invalidate ()

void

Clear Session Contents

IsNew ()

Boolean

Determine if the session is "new"

Setmaxinactiveinterval ()

void

Sets the session life cycle, which expires if this time is exceeded

The third question: Under what circumstances are JSP built-in objects generated?
A JSP page corresponds to a servlet class with three methods per servlet class:
Init method: Initializes the Jsp/servlet method.
Destory method: Destroys the Jsp/servlet method.
Service method: The method that responds to a user request.
The request object and the Reponse object are the parameters of the service method, the Application object, the Page object, the Out object, the PageContext object, and the session object are the local variables generated within the service's method.

Reprint please from:Struts Tutorial Network (netease) ? JSP built-in object details (top)

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.