ServletObjects such as servletcontext, servletrequest, and servletresponse provided by servlet containers can be accessed.
InJSPHow to access these objects? For JSP, these objects are called built-in objects or implicit objects,Each object is referenced by a fixed reference variable.. JSP can directly reference these objects through these fixed reference variables without any variable declaration.
The following table lists the mappings between reference variables and types of all built-in objects.
Nine built-in JSP objects
Reference variable of built-in object |
Built-in object type |
Request |
Javax. servlet. http. httpservletrequest |
Response |
Javax. servlet. http. httpservletresponse |
Session |
Javax. servlet. http. httpsession |
Application |
Javax. servlet. servletcontext |
Page |
Java. Lang. Object (equivalent to this in servlet) |
Out |
Javax. servlet. jsp. jspwriter |
Config |
Javax. servlet. servletconfig |
Pagecontext |
Javax. servlet. jsp. pagecontext |
Exception |
Java. Lang. throwable |
The following describes the meaning of each built-in object one by one:
Request: Javax. servlet. http. httpservletrequest instance. This object encapsulates a request, and client request parameters are encapsulated in this object. This is a common object, which must be used to obtain client request parameters. Common methods include:
String getParameter(String name)String[] getParameterValues(String name)Enumeration getParameterNames()Map getParameterMap()void setAttribute(String name,Object o)Object getAttribute(String name)void setCharacterEncoding(String env)......
Response: Javax. servlet. http. httpservletresponse instance, representing the server's response to the client. This object is rarely used for direct response. More often, an out object is used unless a non-character response is generated. In fact, response objects are often used for redirection. Common methods include:
ServletOutputStream getOutputStream()throws IOExceptionvoid sendRedirect(String location) throws IOException
Session: Javax. servlet. http. httpsession instance. This object represents a session. When the client browser establishes a connection with the server site, the session starts. When the client closes the browser, the session ends. Common methods include:
Object getAttribute(String name)void setAttribute(String name,Object value)
Application: An instance of javax. servlet. servletcontext. This instance represents the web application to which the JSP belongs and can be used for JSP pages or exchanging information between servlets. Common methods include:
Object getAttribute(String name)void setAttribute(String name,Object obj)String getInitParameter(String name)
Page: Indicates the page itself, which is usually of little use. That is, this in the servlet. Its type is the generated servlet class. This can be used wherever the page is used.
Out: Javax. servlet. jsp. jspwriter instance. This instance represents the output stream of the JSP page and is used to output content to form an HTML page.
Config: Javax. servlet. servletconfig instance. This instance represents the configuration information of the JSP. Common methods include:
String getInitParameter(String name)Enumeration getInitParameterNames()ServletContext getServletContext()
In fact, JSP pages do not need to be configured, so there is no configuration information. Therefore, this object is more effective in servlet.
Pagecontext: Javax. servlet. jsp. pagecontext instance. This object represents the Page Context of the JSP. You can use this object to access the shared data on the page. Common methods include:
ServletContext getServletContext()ServletConfig getServletConfig()
Exception: Java. Lang. throwable instance, which indicates exceptions and errors on other pages. This object can be used only when the iserrorpage attribute of the compiled command page is true when the page is an error handling page. Common methods include:
String getMessage()void printStackTrace()