Introduction of nine built-in objects in JSP, and jsp built-in objects

Source: Internet
Author: User

Introduction of nine built-in objects in JSP, and jsp built-in objects
Introduction to nine built-in objects in JSP

Built-in objects are objects that can be directly used in jsp without being created (created by the server <container>.

There are a total of nine built-in objects in jsp:

Request, response, out, session, application, pageContext, page, config, exception

Object details:

1. out object: output information to the client

JspWriter class instance

There are two main methods: print and println. The latter cannot implement line feed.

2. request object: get the information that the client sends to the server.

What kind of instance is request ?? (Not Sure)

The classes that implement the request object vary with the server, and their classes are also different.

Take common tomcat as an example:

Request is the object of org. apache. catalina. connector. RequestFacade.

ServletReqest interface-> HttpServletRequest

Request implements the HttpServletRequest interface.

Examples of common request methods:

(1) getParameter

Obtains the value of a parameter passed by the client to the server.

(2) getParameterNames

Obtains the names of all parameters that the client sends to the server.

(3) getParameterValues

Obtain all values of a parameter (for example, in the case of checkbox)

(4) setAttribute

(5) getAttribute

(6) removeAttribute

(7) getAttributeNames

(8) getCookies

(9) getCharacterEncoding

(10) getContentLength

(11) getMethod

(12) getRequestURL

(13) getRemoteAddr

(14) getServerName

(13) getServerPort

(14) getServletPath

3. response object

Outputs Information to the client browser and responds to the customer's request.

ServletResponse

HttpServletResponse

Org. apache. catalina. connector. ResponseFacade (determined by the container)

AddCookie

AddHeader

ContainsHeader

SetHeader

4. session

What is a session object?

Session expiration time. The default value in tomcat is 30 minutes, which can be set. The session space of each user is isolated.

HttpSession)

Org. apache. catalina. session. StandardSessionFacade (determined by the container)

Public Object getAttribute (String name)

Public Enumeration getAttributeNames ()

Public long getCreationTime ()

Public String getId ()

Public long getLastAccessedTime ()

Public int getMaxInactiveInterval ()

Public void invalidate ()

Public boolean isNew (

Public void removeAttribute (String name)

Public void setAttribute (String name, Object value)

Public void setMaxInactiveInterval (int interval)

Used to specify the time. In seconds, the servlet container will keep the session valid during this time.

5. application (valid for servers)

Application, session, and request: You can use setAttribute to set attributes and use getAttribute to obtain attributes, but the visible range is different.

The properties set by the application object are visible to all sessions, and the properties set by the session object are only visible to the same session.

Similarly, the attributes set for the request described earlier are only visible between the same request.

The application will not expire and will be valid during the entire server operation, and will be lost after the server is restarted.

6. page Object

JSP page itself, the page object is an instance of the Servlet class after the current page is converted. From the converted Servlet class code, we can see this relationship: Object page = this; in JSP pages, page objects are rarely used.

7. config object

ServletConfig Interface

This object is usually used to configure the specified jsp parameters, just like the parameter configuration in servlet. Web. xml

8. exception object

Use the page command to specify errorpage in files that may produce errors

In the specified errorpage, set the page command isErrorPage = "true", and then use the exception object

Obtain error information.

<% @ Page errorPage = "error. jsp" %>

9. pageContext object

Type: PageContext

Purpose:

A. You can use it to obtain the handles of other eight built-in objects (you do not need to obtain the handles)

Out pageContext. getOut ()

Request pageContext. getRequest ();

Response pageContext. getResponse ();

Session pageContext. getSession ();

Application pageContext. getServletContext ();

Config pageContext. getServletConfig ();

Exception pageContext. getException ();

Page pageContext. getPage ();

B. You can use it to set or obtain the values of variables in four different ranges (page, request, session, application ).

The value can only be valid on this page.

PageContext. setAttribute ("name", "John ");

The value is valid in the same request.

PageContext. setAttribute ("name", "John", PageContext. REQUEST_SCOPE ))

It is equivalent to request. setAttribute ("name", "John ");

The value is valid in the same session request.

PageContext. setAttribute ("name", "John", PageContext. SESSION_SCOPE ))

Equivalent to session. setAttribute ("name", "John ");

The value is valid throughout the application.

PageContext. setAttribute ("name", "John", PageContext. APPLICATION_SCOPE ))

Equivalent to application. setAttribute ("name", "John ");

Summary

Out: JspWriter

Request: implements the HttpServletRequest interface (this interface inherits from the ServletRequest interface)

Response: implements the HttpServletResponse interface (this interface inherits from the ServletResponse interface)

Session: implements the HttpSession interface.

Application Object-implemented interface ServletContext

9 built-in objects

Out, response, [page, request, session, application] (different scopes), config, exception, pageContext

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.