JSP series: (3) JSP advanced-Nine large built-in objects and four domain objects

Source: Internet
Author: User

1, JSP nine large built-in objects


1.1. What is a built-in object?

In JSP development, a number of objects, such as HttpSession, ServletContext, ServletContext, Httpservletrequet, are frequently used. It is cumbersome to create these objects every time we want to use them. So when Sun design jsp, when the JSP page is loaded, it will automatically help developers to create these objects, and developers only need to directly use these objects call methods, these created objects called built-in objects !!!!


1.2, 9 large built-in objects


9 Large built-in objects
Serial Number built-in object name type
1 Request HttpServletRequest
2 Response HttpServletResponse
3 Config ServletConfig
4 Application ServletContext
5 Session HttpSession
6 exception Throwable
7 Page Object (This)
8 Out JspWriter
9 PageContext PageContext


1.3. Out Object

Out object type, JspWriter type, equivalent to PrintWriter with cache

Compare JspWriter and PrintWriter:

Printwriter:wrier (content): write content directly to the browser.

Jspwriter:writer (content): Write content to the JSP buffer

When one of the following conditions is met, the buffer contents are written out:

1) The buffer is full.

2) Refresh Buffer

3) Close the buffer

4) Completed JSP page


1.4. PageContext Object

The type of the PageContext object is PageContext, which is called the JSP context object

1) Eight additional built-in objects can be obtained

The PageContext class provides a series of getxxx methods to get the 7 built-in objects of a JSP

In addition, PageContext inherits from the Jspcontext class, and the Jspcontext class provides a getout () method for obtaining an Out object.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/00/wKioL1dHGwWxS0dPAAC-RlM6TM8255.png "title=" Pagecontext.png "alt=" Wkiol1dhgwwxs0dpaac-rlm6tm8255.png "/>

Usage scenarios: When customizing labels, PageContext objects are used frequently!!! If a method takes the PageContext object as an incoming parameter, the other 8 JSP built-in objects can be indirectly obtained through the PageContext object.

2) itself is a domain object

Function: Save data and get data for sharing data

# Save Data

(1) By default, save to Page field: Pagecontext.setattribute ("name");

(2) data can be saved to four domain objects: Pagecontext.setattribute ("name", domain-wide constant)

# Get Data

(1) By default, gets from the page domain: Pagecontext.getattribute ("name")

(2) data can be obtained from four domains: Pagecontext.getattribute ("name", domain-wide constant)

Domain-wide constants:

Pagecontext.page_scope

Pagecontext.request_scope

PageContext. Session_scope

Pagecontext.application_scope

(3) Automatically search for data in four domains: Pagecontext.findattribute ("name");

Order: page domain, request domain, session domain-> context domain (application domain)

<%@ page language= "java"  import= "java.util.*"  pageencoding= "Utf-8"%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >


2. Four domain objects in a JSP
four domain objects in a JSP
Serial Number domain object name type Domain Notes
1 PageContext PageContext Page field Learning in JSP
2 Request HttpServletRequest Request Domain Learning from the servlet
3 Session HttpSession Session field Learning from the servlet
4 Application ServletContext Context Domain Learning from the servlet

(There are 3 domain objects in the servlet, and 4 domain objects in the JSP.) )

1) The role of domain objects : Saving data and acquiring data for data sharing.

2) Domain object method :

SetAttribute ("name", Object) to save data

GetAttribute ("name") Gets the data

RemoveAttribute ("name") clears the data

3) domain object scope:

Page field: Can only be used in the current JSP page (current page)

Request domain: Can only be used in the same request (forward)

Session field: Can only be used in the same session (the Session object) (private)

Context domain: can only be used in the same web app. (global)


3. Best practices for JSP

Servlet Technology: Developing dynamic resources. Is a Java class that is best at writing Java code .

JSP Technology: Developing dynamic resources. The Java code is best at outputting html code .


logic is involved in a Web project :

1) Receive Parameters-->servlet do

2) Process business logic, return results-->servlet do

3) Show data to browser-->jsp do

4) Jump to other pages-->servlet do


servlet+jsp mode

servlet:

1) Receive parameters

2) Process business logic

3) Save the results to the domain object

4) Jump to JSP page

Jsp:

1) Extracting data from a domain object

2) display the data to the browser



JSP series: (3) JSP advanced-Nine large built-in objects and four domain objects

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.