I. Summary
In the _jspservice () method of the Servlet class generated in the JSP container, several objects are defined, and we can use these hidden objects when we write JSP pages.
pagecontext pagecontext = null ; HttpSession session = null ; ServletContext application = null = null ; JspWriter out = null ; Object page = this = null = null
Note: The above code does not have a exception built-in object, and the exception object is used only if the page directive's Iserrorpage property is true.
These objects are described in detail below.
second, Application object
The Application object represents the Web app itself, which typically has two functions:
1. Sharing data between multiple JSPs and servlets across a Web application
Set properties and corresponding values in the JSP:
Application.setattribute ("name", "Xujian"); %> </body>
This property value can be obtained by using the Getinitparameter () method of the ServletContext object in the servlet:
protected void throws servletexception, IOException { System.out.print(this. Getservletcontext (). Getinitparameter ("name")); }
2. Accessing configuration parameters for Web appsTo configure the parameters in the Web. xml file:
< Context-param > < Param-name >name</param-name> <Param-value >xujian</param-value> </ Context-param>
The Getinitparameter () method of the Application object can be used in JSPs to obtain the configuration parameter values:
< Body > <% = Application.getinitparameter ("name") %> </ Body >
C. config ObjectThe Config object represents the current JSP configuration information, which is less used on JSP pages. We can configure the information in Web. XML:
<servlet> <Servlet-name>Config</Servlet-name> <Jsp-file>/demo.jsp</Jsp-file> <Init-param> <Param-name>Age</Param-name> <Param-value>20</Param-value> </Init-param> </servlet> <servlet-mapping> <Servlet-name>Config</Servlet-name> <Url-pattern>/config</Url-pattern> </servlet-mapping>
The configuration information is then obtained using the Getinitparameter () method of the Config object:
< Body > <% = Config.getinitparameter ("Age") %> </ Body >
Iv. PageContext ObjectsThe PageContext object represents the page context and is used primarily to access shared data between JSPs.
- A single API to manage the various scoped namespaces
- A number of convenience API ' s to access various public objects
- A mechanism to obtain the JspWriter for output
- A mechanism to manage session usage by the page
- A mechanism to expose page directive attributes to the scripting environment
- Mechanisms to forward or include the current request to other active components in the application
- A mechanism to handle errorpage exception processing
Note: The PageContext object actually provides us with a unified portal to access other hidden objects.
five, out object
The Out object outputs the data as a stream of characters, which is actually a buffered version of the PrintWriter object, which can be resized by the buffer property of the page directive.
Any place where out is used can be substituted with an output expression, and the essence of the <%= ...%> expression is out.write (...).
Vi. Exception objects
The exception object represents the exception that is generated when the JSP page runs, and the object is only available in the error page.
Note: If a JSP page uses the ErrorPage property to define the error page, the error page defined in the Web. xml file will not be used.
Vii. Session Object
The Session object represents a user session (starting from the client browser connection server to the client browser and the server), which is typically used to track user session information, such as determining whether a user is logged in to the system or tracking the user's purchase of merchandise information.
String getId ();//gets the ID of the session LongGetCreationTime ();//gets the session's build time LongGetlashaccessedtime ();//get user last send request time via session LongGetmaxinactiveinterval ();//gets the session life cycle, which expires if this time is exceeded voidInvalidate ();//Clear Session Contents BooleanIsNew ();//determine if the session is "new" voidSetmaxinactiveinterval ();//sets the session life cycle, which expires if this time is exceeded
JSP basic Usage (ii) implied objects