The servlet can access objects such as Servletcontext,servletrequest and Servletresponse provided by the servlet container.
So how do you access these objects in the JSP ? For JSPs, these objects are called built-in objects or suppressed objects, and each object is referenced by a fixed reference variable . JSP does not need to make any variable declarations, you can refer to these objects directly through these fixed reference variables.
The following table lists the corresponding relationships between reference variables and types for all built-in objects.
nine built-in objects for JSP
reference variables for built-in objects |
types of built-in objects |
Request |
Javax.servlet.http.HttpServletRequest |
Response |
Javax.servlet.http.HttpServletResponse |
Session |
Javax.servlet.http.HttpSession |
Application |
Javax.servlet.ServletContext |
Page |
Java.lang.Object (the equivalent of this in the servlet) |
Out |
Javax.servlet.jsp.JspWriter |
Config |
Javax.servlet.ServletConfig |
PageContext |
Javax.servlet.jsp.PageContext |
exception |
Java.lang.Throwable |
The meanings of each built-in object are described below:
Request: an instance of javax.servlet.http.HttpServletRequest that encapsulates a request, and the client's request parameters are encapsulated in the object. This is a commonly used object that must be used to get the client request parameters. The common methods are:
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: an instance of Javax.servlet.http.HttpServletResponse that represents the server's response to the client. It is often very rarely used to respond directly to this object, and it is more common to use out objects unless a non character response needs to be generated. In fact, response objects are often used for redirection, and the common methods are:
Servletoutputstream Getoutputstream ()
throws IOException
void Sendredirect (String location) throws IOException
Session: an instance of Javax.servlet.http.HttpSession that represents a session. The session begins when the client browser establishes a connection to the server site, and the session ends when the client closes the browser. The common methods are:
Object getattribute (string name)
void setattribute (string name,object value)
Application: an instance of Javax.servlet.ServletContext that represents the Web application to which the JSP belongs, available for JSP pages or for exchanging information between the servlet. The common methods are:
Object getattribute (string name)
void setattribute (string name,object obj)
string Getinitparameter (string Name
page: Representing the page itself is usually not of much use. This, in the servlet, is the type of servlet that is generated, which can be used where page is available.
out: an instance of Javax.servlet.jsp.JspWriter that represents the output stream of the JSP page, used to output content, and forms an HTML page.
Config: the instance of Javax.servlet.ServletConfig that represents the configuration information for the JSP. The common methods are:
String Getinitparameter (string name)
enumeration getinitparameternames ()
ServletContext Getservletcontext ( )
In fact, JSP pages usually do not need to be configured and there is no configuration information. Therefore, the object is more effective in the servlet.
PageContext: An instance of Javax.servlet.jsp.PageContext that represents the page context of the JSP that can be used to access shared data on a page. The common methods are:
ServletContext getservletcontext ()
servletconfig getservletconfig ()
exception: an instance of java.lang.Throwable that represents exceptions and errors on other pages. This object can be used only if the page is an error-handling page, that is, the Iserrorpage property of the compilation instruction page is true. The common methods are:
String getMessage ()
void Printstacktrace ()