JSP implicit objects are the Java objects that the JSP container provides for each page, and developers can use them directly without explicitly declaring them. JSP implicit objects are also called predefined variables.
The nine hidden objects supported by JSP:
Request Instances of the HttpServletRequest class
Response Instances of the HttpServletResponse class
out An instance of the PrintWriter class for outputting the results to a Web page
Session Instances of the HttpSession class
Application An instance of the ServletContext class, related to the application context
Config Instances of the ServletConfig class
PageContext An instance of the PageContext class that provides access to all objects in the JSP page and to namespaces
An instance of the page Httpjsppage class, similar to the This keyword in a Java class
Exception An object of the exception class that represents the corresponding exception object in the JSP page where the error occurred
Request Object
The request object is an instance of the Javax.servlet.http.HttpServletRequest class. Each time a client requests a JSP page, the JSP engine creates a new request object to represent the requests.
The Request object provides a series of methods to get the HTTP header information, the Cookies,http method, and so on.
Response Object
The response object is an instance of the Javax.servlet.http.HttpServletResponse class. When the server creates the request object, it also creates a response object that responds to the client.
The response object also defines the interface that handles the HTTP header module. With this object, developers can add new cookies, timestamps, HTTP status codes, and so on.
Out Object
An Out object is an instance of the Javax.servlet.jsp.JspWriter class that is used to write content to the response object.
The original JspWriter class objects perform different instantiation operations based on whether the page has a cache. You can use the buffered= ' false ' property in the page directive to close the cache easily.
The JspWriter class contains the methods in most of the Java.io.PrintWriter classes. However, JspWriter has added some methods designed to handle caching. Also, the JspWriter class throws ioexceptions exceptions, and PrintWriter does not.
The following table lists the important methods that we will use to output the type of data such as Boolean,char,int,double,srtring,object:
1.public abstract void Clear ()
Clears the contents of the buffer and does not send the data to the client.
2.public abstract void Clearbuffer ()
Clears the contents of the buffer after the data is sent to the client.
3.public abstarct void Close ()
Closes the output stream.
4.public abstract void Flush ()
The data in the output buffer.
5.public intgetbuffersize ()
Gets the size of the buffer. The size of the buffer can be <% @page buffer= "size"%> settings.
6.public abstract int getremainning ()
Get the size of the remaining space in the buffer
7.public Booleanisautoflush ()
Gets the AutoFlush value set with <%@ page is autoflush= "True/false"%>.
8.public abstract void NewLine ()
Outputs a newline character and wraps it in a line.
9.public abstract void print ()
Displays the contents of various data types.
10.public abstract void println ()
Branches display the contents of various data types.
Session Object
The session object is an instance of the Javax.servlet.http.HttpSession class. Has the same behavior as the session object in Java Servlets.
Session objects are used to track sessions between individual client requests.
Application Object
The Application object wraps the object of the servlet's ServletContext class directly, and is an instance of the Javax.servlet.ServletContext class.
This object represents this JSP page throughout the entire life cycle of the JSP page. This object is created when the JSP page is initialized and is removed with the invocation of the Jspdestroy () method.
By adding attributes to application, these properties are accessible to all JSP files that make up your web app.
Application, session, request, page their use is basically the same, but the scope is different:
Application: The global scope, the entire application share, is the same webapp share in the deployment file, with a lifecycle of: application start to stop.
Session scope: When the user first accesses a new session, the server can remember the session state. Life cycle: Session timeout, or server-side forcing session to expire.
Request: The requested scope is a request from the client.
Page: One of the JSP pages.
The Application object is generated when the server is started, and the Application object is the same until the server shuts down when the client browses through the pages of the Web site visited. However, unlike the session, all clients ' application objects are the same, that is, all customers share this built-in Application object.
1.getAttribute ()
Returns the value of the property of the Application object for the name specified by name.
2.getAttributeNames ()
Returns the name of all the properties of the Application object, and the result is an instance of an enumeration.
3.getInitParameter (String name)
Returns the initial value of a property of a Application object for the name specified by name.
4.getServletInfo ()
Returns information about the version of the servlet compiler.
5. SetAttribute (String name, Object object)
Sets the value of the property of the Application object to the name specified by name object.
It is necessary to note that when Setattruibute within a range of use, try not to set the same object ID multiple times to prevent the information stored by the object from being changed and useless. (But in some cases it's a useful place!) )
Config object
The Config object is an instance of the Javax.servlet.ServletConfig class that wraps the object of the servlet's ServletConfig class directly.
This object allows the developer to access the initialization parameters of the servlet or JSP engine, such as the file path.
Here's how to use the Config object:
Config.getinitparameter ("name"): Gets the servlet initialization parameter value for the specified name.
Config.getinitparameternames (): Gets a list of servlet initialization parameters and returns an enumeration instance.
Config.getservletcontext (): Gets the servlet context (ServletContext).
Config.getservletname (): Gets the name of the generated servlet.
PageContext Object
The PageContext object is an instance of the Javax.servlet.jsp.PageContext class that represents the entire JSP page.
This object is primarily used to access page information while filtering out most of the implementation details.
This object stores a reference to the request object and the response object. Application objects, config objects, session objects, and out objects can be exported by accessing the properties of this object.
The PageContext object also contains instruction information passed to the JSP page, including cache information, ErrorPage URL, page scope, and so on.
The PageContext class defines some fields, including Page_scope,request_scope,session_scope,application_scope. It also provides more than 40 methods, half of which inherit from the Javax.servlet.jsp.JspContext class.
One of the important methods is Removearribute (), which can accept one or two parameters. For example, Pagecontext.removearribute ("Attrname") removes the related properties from four scopes, but the following method removes only the related properties in a specific scope:
Pagecontext.removeattribute ("Attrname", Page_scope);
1) getout ()
This method returns an instance object of the JspWriter class, the JSP built-in Object--out object, which can be written to the client to the input stream. It is used in the following ways:
2) getsession ()
The return value of the method is an instance object of the HttpSession interface, that is, the JSP built-in Object--session object, which can hold different user information. It is used in the following ways:
3) GetPage ()
The return value of the method is a Java.lang.Object object, which is the JSP built-in object--page object. It is used in the following ways:
4) GetResponse ()
The return object of this method is Javax.servlet.ServletResponse, which is the JSP built-in Object--response object, which is mainly used to control HTTP connections. It is used in the following ways:
5) Getrequest ()
The return object of this method is Javax.servlet.ServletRequest, which is the JSP built-in Object--request object, which is mainly used to obtain the client's information. It is used in the following ways:
6) GetException ()
The return value of the method is the exception object of the current page. It is used in the following ways:
7) Getservletconfig ()
The method can return the current Config object, which is used to get the initial parameters of the Jsp/servlet program. It is used in the following ways:
8) Getservletcontext ()
The return value of the method is an instance object of the ServletConfig interface, which is a copy of the JSP built-in object--application object. It is used in the following ways:
9) SetAttribute (Stringname, Object o, int scope)
The SetAttribute () method of the PageContext object can bind parameters or Java objects to application objects, session objects, request objects, or page objects, where the scope parameter can take the following values.
L Pagecontext.page_scope
L Pagecontext.request_scope
L Pagecontext.session_scope
L Pagecontext.application_scope
When the value of the scope parameter is Pagecontext.session_scope, it is equivalent to call the SetAttribute () method of the PageContext object and the Putvalue () method that invokes the SESSION object. is to bind a parameter or Java object with the current session. If the value of the scope parameter is Pagecontext.application_scope, then the setattribute () of the PageContext object is called if the value of the scope parameter is The method and the SetAttribute () method that invokes the Application object are equivalent. If the scope parameter is a different value, the principle is the same, we will not say more. It is used in the following ways:
<% Pagecontext.setattribute ("username", "George", Pagecontext.application_scope); Pagecontext.setattribute ("username", "Robin", PageContext. Session_scope); %>
GetAttribute (stringname, int scope)
The GetAttribute () method of the PageContext object can also directly get and application objects, page objects, session objects, request objects that are bound to each other, or values of Java objects. The scope parameter of this method has the same meaning as above. If the value of the SCOPE parameter is Pagecontext.request_scope, then the Getatttribute () method looks inside the current REQUEST object to see if there is a Java object named name and, if so, returns the object. If not, a null value is returned. If the value of the scope parameter is Pagecontext.application_scope, then the Getatttribute () method will be scoped to the Application object. It is used in the following ways:
<% Pagecontext.getattribute ("username", pagecontext.application_scope); Pagecontext.getattribute ("username", pagecontext.session_scope); %>
One) removeattribute (stringname, int scope)
As the name implies, the RemoveAttribute () method of the PageContext object can directly unbind the Application object, the session object, the Request object, the Page object, a specific parameter, or a Java object. The scope parameter of this method specifies the range of the action, if the value of the scope parameter is Pagecontext.session_scope, then RemoveAttribute () Method will unbind a parameter or Java object from the current session object, and if the scope parameter is a different value, the principle is the same. It is used in the following ways:
<% Pagecontext.removeattribute ("username", pagecontext.application_scope); Pagecontext.removeattribute ("username", pagecontext.session_scope); %>
Findattribute (Stringname)
The Findattribute () method of the PageContext object looks for a parameter in the Application object, the session object, the Request object, the Page object, or a Java object that is bound to those objects. The search order is the smallest page object in scope, followed by the request object, the session object, and finally the Application object, which is the value of the first Java object that meets the requirements. It is used in the following ways:
Getattributescope (Stringname)
Use this method to know the specific parameters or the Java object and which JSP object is bound together. The return value of the method is an integer constant, which may be one of the following 4 constants:
L Pagecontext.page_scope
L Pagecontext.request_scope
L Pagecontext.session_scope
L Pagecontext.application_scope
If the return value is Pagecontext.session_scope, it indicates that the Java object and the SESSION object are bound together. If it is a different return value, the reader can do so in turn. It is used in the following ways:
Intscope=pagecontext.getattributescope ("username");
Getattributenameinscope (Intscope)
This method can obtain which parameters or Java objects are bound inside a particular JSP object, and the scope parameter specifies the scope of the method. The return value of this method is an enumeration object (enumeration). Its usage example is as follows:
The JSP snippet above will return the parameter that is bound in the Session object or the name of the Java object.
Release ()
This method will release the resources that the PageContext object occupies.
Forward (StringUrl)
This method can implement the redirect Function of Web page, similar to the Response.sendredirect () method.
) include (Stringrelativeurlpath)
This method allows the current file to be included in an external file, similar to the include compiler directive and the <jsp:include> Operation directive of the JSP.
From the introduction of the PageContext object, it should be easy to see that the PageContext object has two main functions, namely, to get the internal objects of each JSP, and to manipulate parameters or Java objects that are bound together with some JSP objects directly.
Page Object
This object is a reference to the page instance. It can be seen as a representation of the entire JSP page.
The Page object is a synonym for this object.
Exception Object
The exception object wraps the exception information that was thrown from the previous page. It is often used to generate an appropriate response to an error condition