SoFunction
Updated on 2025-04-05

JSP Tutorial (III)--Use of "predefined variables" in JSP

In order to simplify the code in JSP expressions and scriptlets, 8 types of automatically defined variables are provided, sometimes called implicit objects (inherent objects). They are: request, response, out, session, application, config, pageContext, and page. Let's learn more about them below.

request

Related to request is the HttpServletRequest class, which allows you to get the parameters of the request (via getParameter method), the type of the request (GET, POST, HEAD, etc.), and the introduced HTTP header (cookies, Referer, etc.). Strictly speaking, request is a subclass of the ServletRequest class rather than the HttpServletRequest class. In fact, if the request's protocol is not HTTP, it will hardly work.

response

The response to the client is connected to the HttpServletResponse. Note that because the output stream is buffered, HTTP status codes and response headers can be set, although it is not allowed to be sent to the client in standard servlets.

out

Here, use the PrintWriter class to send output to the client. However, in order to make the response object valid, a buffered version of the PrintWrite class can be used. Using the session's property page directive, you can define the buffer size yourself, and even turn off the buffer after using the buffer property. Please also note that out is only used in scriptlets, because JSP expressions are automatically placed into the output stream, so it is rarely necessary to declare out explicitly.

session

Apply the HttpSession class associated with request. Because the session is created automatically, such variables can still be bound even if there is no introduced session. An exception is that if you close the session with page directive, then try to use the session will cause an error (when converting the JSP page to the servlet).

application

Use the ServletContext class, get by using getServletConfig().getContext().

config

Is an object of the ServletConfig class.

pageContext

This is a new class PageContext in JSP, used to refine the characteristics of specific servers, such as improving the execution efficiency of JspWriters. If you access it through this class instead of direct, your code will still run in the "rules" of the JSP/servlet engine.

page

Not very useful in JAVA, it is just the time used to save the script language when it is not JAVA.