JSP built-in object details (UP) < reprint >

Source: Internet
Author: User

first question: JSP how many types of built-in objects are there?
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.
second question: JSP What types of built-in objects are they? What are the roles of these objects? What are their common methods?
(1 ) Application object:An instance 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 <context-param: The/> element configures a parameter.
Note: There is only one ServletContext instance per web app, and the JSP page can access the instance through the application built-in object. In the servlet, it must be obtained by code: ServletContext sc= getservletconfig (). Get ServletContext ();
(2 ) config object: Javax.servlet.ServletConfig instance, which represents the configuration information of the JSP, in fact, the JSP page is usually not configured, there is no configuration information. More for use in Servlets.

method

return value type

getinitparameter (name)

string

getinitparameternames ()

enumeration

get servlet all initialization parameter name

getservletcontext ()

servletcontext

Get current application context

getservletname ()

string

get servlet name

/td>

(3 ) Exception object:An instance of java.lang.Throwable that can be used only 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, and usually does not have much effect. This is the this in the servlet.
(6 ) PageContext object: the instance of PageContext, which represents the JSP page context, Use this object 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 within the object. The methods for obtaining parameters are:

  method

return Return value type

method description

 getparameter (String na Me)

string

  get parameter value named name

 getparameternames ()

enumeration

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

 getparametervalues (S Tring name)

 string[]

  get all parameters named name, for example, parameter is Multiple checkboxes

 getparametermap ()

&NB Sp Map

  gets the map instance encapsulated by all parameters and returns an array of values with the corresponding parameter named ID via 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, which represents the server's response to the client, usually rarely responds directly to the object, but uses an 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:An instance of Javax.servlet.http.HttpServletReponse that 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 problem: JSP Under what circumstances does the built-in object occur?
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.

Reproduced please specify: Struts tutorial network to gather the strength of the people to do China's most comprehensive struts tutorial. 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.