To simplify the process of Web page development,JSP provides some hidden objects that are implemented and managed by the container, which can be used directly in JSP pages without the need for JSP page writers to instantiate them. In JSP pages, you can access these hidden objects to interact with the JSP page and the servlet environment.
In essence, the local variables in the translated servlet, a total of 9, of which
There are 4 of scope categories:
pagecontext– represents the PageContext object, valid only on the current page
request– represents the HttpServletRequest object, valid in one request
session– represents the HttpSession object, valid in one session
application– represents the ServletContext object and is valid throughout the Web application
The output category has 2
Response--representing the HttpServletResponse object associated with the response
Out--Represents the JspWriter object associated with the response output stream, which can be output character streams
There are 3 other
config– represents servletconfig, used to configure a Servlet object
page– current JSP object, equivalent to this
exception– represents the Throwable object and can only be used in the JSP error page
Learn more about the nine suppressed objects in the JSP, the Page object represents the JSP itself, or rather, it represents the JSP-translated servlet, which can invoke the method defined by the Servlet class.
One, Page object
The Page object represents the JSP itself, or rather, it represents the JSP-translated servlet, which can invoke the method defined by the Servlet class.
two, config object
The Config object holds some of the servlet's initial data structures.
The Config object is implemented in the Javax.servlet.ServletConfig interface, which has the following four methods:
Public String Getinitparameter (name) public java.util.Enumeration getinitparameternames () public ServletContext Getse Rvletcontext () public sring getservletname ()
third, Request object
The Request object contains all the requested information, such as the source of the request, the header, the cookies and the parameter values associated with the request, and so on.
The request object implements the Javax.servlet.http.HttpServletRequest interface, and the method provided can divide it into four main categories:
1. Methods of storing and acquiring property;
void SetAttribute (String name, Object value) sets the value of the Name property to enumeration getattributenamesinscope (int scope) to obtain all scope The range's property Object getattribute (string name) removes the value of the Name property by obtaining the value void RemoveAttribute (string name) of the Name property
2. Method of obtaining request parameter
String GetParameter (string name) obtains the parameter value of name Enumeration Getparameternames () gets all the parameter names String[] Getparametervalues (stri ng name) gets all the name's parameter values map Getparametermap () to obtain a map of the required parameters
3. The ability to obtain the request HTTP header method
String GetHeader (string name) obtains name Header enumeration Getheadernames () obtains all header name enumeration Getheaders (string name) obtains all n AME's header int Getintheader (string name) gets the header long Getdateheader (string name) of the integer type name to get the header Cookie for date type name [] Getcook IES () obtain cookies relating to the request
4. Other Methods
String Getcontextpath () obtains the protocol used by the context path (that is, platform name) string GetMethod () to obtain HTTP (GET, POST) string Getprotocol () http/ 1.1, http/1.0) string getquerystring () gets the requested parameter string, but the HTTP method must obtain the client's session ID for the Get string Getrequestedsessionid () String Getrequesturi () Gets the requested URL, but does not include the requested parameter string string getremoteaddr () Gets the user's IP address string getremotehost () Gets the user's host name int Getremoteport () Obtains the user's host port String Getremoteuser () to obtain the user's name void etcharacterencoding (String encoding) encoding format, which is used to resolve forms that pass Chinese Problem
Iv. Response Objects
The response object mainly passes the results of the JSP processing data back to the client.
The response object is the implementation Javax.servlet.http.HttpServletResponse interface. Response the method provided by the object.
1. Set the table head method
void Addcookie (Cookie cookie) New cookie void Adddateheader (String name, long date) adds a value of type long to the name header void AddHeader (Strin G name, string value adds a value of type string to the name header void Addintheader (String name, int value) with a new value of type int to name header void Setdateheader ( String name, long date specifies a long value to the name header void SetHeader (string name, string value) to specify a string value to the name header void Setintheade R (String name, int value) to specify the value of type int to name header
2. The method of setting the response status code
void Senderror (int sc) Transfer status code (status code) void Senderror (int sc, String msg) transfer status code and error information void setstatus (int sc) set state code
3. Method for URL rewriting (rewriting)
String encoderedirecturl (string url) encodes a URL that uses the Sendredirect () method
Five, out objects
An Out object can output the results to a Web page.
Out is mainly used to control the output stream of buffers (buffer) and output streams.
void Clear () clears the contents of the output buffer void Clearbuffer () clears the contents of the output buffer void close () closes the output stream, clears all content int getbuffersize () gets the current buffer size (KB) int getremaining () the buffer size (KB) that is left after the current use, Boolean Isautoflush () True indicates that the buffer will be cleared automatically when full;
six, Session object
The Session object represents the current sessions status of individual users.
The session object implements the Javax.servlet.http.HttpSession interface, the method provided by the HttpSession interface
Long GetCreationTime () Gets the time that the session was generated, in milliseconds String getId () to get the ID of the session Long Getlastaccessedtime () Gets the time that the user finally sends out the request through this session long Getmaxinactiveinterval () obtains the maximum session inactivity time, if surpasses this time, sessions will fail void invalidate () Cancels the session object and completely discards the contents of the object by Boolean isnew () to determine whether the session is a "new" void Setmaxinactiveinterval (int interval) when the maximum session inactivity is set , if you exceed this time, the session will be invalidated.
Vii. Application Objects
Application objects are most commonly used in accessing information in the environment.
Because information about the environment is usually stored in ServletContext, application objects are often used to access information in ServletContext.
The Application object implements the Javax.servlet.ServletContext interface, ServletContext the method provided by the interface container
the int getmajorversion () obtains the container main servlet API version int getminorversion () obtains container minor servlet API version String Getserveri NFO () obtains the container name and version string GetMimeType (string file) to obtain the specified file's MIME type ServletContext getcontext (string uripath) to obtain the designation LOC The application Context string Getrealpath (string path) of the Al URL obtains the absolute path void log (String message) of the local path to write information to the log file void lo G (String message, throwable throwable) writes exception information generated by stack trace to the log file
Eight, PageContext object
PageContext objects can access other suppressed objects.
1.pageContext object to access other suppressed object properties, you need to specify a range of parameters.
Object getattribute (String name, int scope) enumeration Getattributenamesinscope (int scope) void RemoveAttribute (Strin g name, int scope) void setattribute (String name, Object value, int scope)
There are four range parameters, representing four different ranges: Page_scope, Request_scope, Session_scope, Application_scope
2.PageContext object method for obtaining other suppressed objects
Exception getexception () return the exception to the current page, but this page will be the error page,
JspWriter getout () return the current page's output stream, for example: out
Object GetPage () The servlet entity (instance) that is returning the current page, for example: page
ServletRequest getrequest () to return the current Web page request, for example: request
Servletresponse GetResponse () the response to the current page, for example: response
ServletConfig Getservletconfig () return the current ServletConfig object for this page, for example: Config
ServletContext Getservletcontext () Return the current execution Environment (context) for this page, for example: application
HttpSession getsession () the Conversation (session) that is associated with the current Web page, for example: Sessions
3.PageContext Object provides methods to obtain properties
Object GetAttribute (String name, int scope) to return the Name property, scoped to the Property object of scope, with the return type of object
Enumeration Getattributenamesinscope (int scope) return the name of all properties scoped to scope, and the return type is enumeration
int Getattributesscope (String name) property range with name return property
void RemoveAttribute (String name) removes a Property object with the property name named
void RemoveAttribute (String name, int scope) Removes a Property object with the name of the property named, scoped to scope
void SetAttribute (String name, Object value, int scope) specifies that the name of the Property object is named, values is value, range is scope
Object Findattribute (String name) looks for property objects with name names in all scopes
Nine, exception objects
To use the exception object, you must set it in the page directive. To use.
The three methods offered by exception are:
GetMessage ()
Getlocalizedmessage (),
Printstacktrace (New Java.io.PrintWriter (out))
This article describes the main implied objects used in JSP pages. It is because of these hidden objects that you can enter the code snippet of the JSP page without having to delve into the details of too many Servler APIs. In essence, these hidden objects of JSP are actually generated by a specific Java class, automatically generated when the server is running, so if you have a good Java base, you can refer to the corresponding class description.