[JSP Advanced] Nine built-in objects of JSP, you don't know this?

foreword

We have learned the basics of JSP before. Today, we will continue to learn the nine built-in objects of JSP. This is also the key part of JSP and the focus of the interview. You must practice it yourself after reading it. At the same time, those who do not know the basics of JSP can go to the previous content and continue.

Portal: [JSP Introduction] Only know HTML but not JSP?

Next, let's officially learn the nine built-in objects of JSP!

Getting to Know the Built-in Objects

The so-called built-in objects are nine objects that can be used directly in JSP without creating

The nine built-in objects are:

(1) out(JspWriter): Equivalent to response.getWriter(), used to send text data to the client;
(2) config(ServletConfig): Corresponding to ServletConfig in "True Body";
(3) page(当前JSP的真身类型): "this" of the current JSP page, that is, the current object;
(4) pageContext(PageContext): The page context object, which is the last unmentioned domain object;
(5) exception(Throwable): This object can only be used in error pages;
(6) request(HttpServletRequest): The object of the HttpServletRequest class;
(7) response(HttpServletResponse): The object of the HttpServletResponse class Object;
(8) application(ServletContext): The object of the ServletContext class;
(9) session(HttpSession): The object of the HttpSession class, which cannot be used in every JSP page. If it is set in a JSP page <%@pagesession=”false”%>, it means that the page cannot use the session.

While built-in objects are the focus, not all built-in objects are commonly used.

Built-in objects that are rarely used: config, page, exception.

There are two objects that not every JSP page can use: exception, session.

In-depth analysis of nine built-in objects

1. The request object

The request object is an object of javax.servlet.httpServletRequesttype . This object representsClient's request information, which is mainly used to accept data sent to the server through the HTTP protocol. (including header information, system information, request method, and request parameters, etc.). The scope of the request object is one request.

2. The response object

response represents yesclient's response, mainly to pass the object processed by the JSP container back to the client. The response object also has scope, which is only valid within the JSP page.

3. session object

The session object isObjects automatically created by the server related to user requests. The server generates a session object for each user, which is used to save the user's information and track the user's operation status. The session object internally uses the Map class to save data, so the format for saving data is "Key/value": The value of the session object can make complex object types, not just limited to string types.

4. application object

application objects cansave information on the server, until the server shuts down, otherwise the information saved in the application object will be valid throughout the application. Compared with the session object, the application object has a longer life cycle and is similar to the "global variable" of the system.

5. out object

out object is used inOutput information in a web browser, and manages output buffers on the application server. When using the out object to output data, you can operate on the data buffer, clear the residual data in the buffer in time, and make buffer space for other outputs. After the data output is completed, the output stream should be closed in time.

6. pageContext object

The role of the pageContext object isget any range of parameters, through which you can get the out, request, response, session, application and other objects of the JSP page. The creation and initialization of the pageContext object is done by the container, and the pageContext object can be used directly in the JSP page.

7. config object

The main role of the config object is toGet server configuration information. A config object can be obtained through the getServletConfig() method of the pageConext object. When a servlet is initialized, the container passes certain information to the servlet through the config object. Developers can provide initialization parameters for servlet programs and JSP pages in the application environment in the web.xml file.

8. page object

page objectrepresents the JSP itself, is only legal within a JSP page. The page implicit object essentially contains the variables referenced by the current servlet interface, similar to the this pointer in Java programming.

9. exception object

The purpose of the exception object is toDisplay exception information, isErrorPage="true"can only be used in the page that contains , using this object in the general JSP page will not compile the JSP file. The exception object, like all objects in Java, has a system-provided inheritance structure. The exception object defines almost all exception conditions.

In a Java program, you can use try/catchkeywords to handle exceptions; if an uncaught exception occurs in a JSP page, an exception object will be generated, and the exception object will be sent to the error page set in the page directive, and then Handle the corresponding exception object in the error page.

The nine built-in objects of JSP are divided into four categories:

(1) Input and output objects: outobject, responseobject, requestobject
(2) Communication control object: pageContextobject, sessionobject, applicationobject
(3) Servlet object: pageobject, configobject
(4) Error handling object: exceptionobject

JSP case demonstration

Old rules Let's write a simple JSP to demonstrate the usage of commonly used built-in objects.

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String url = request.getRequestURL().toString();//HttpServletRequest对象
		response.getWriter().println(url);//HttpServletResponse对象
	%>
	<%
		out.println("<br>ABCCC");//等同于response.getWriter()
		session.setAttribute("user", "张三");//HttpSession类的对象
		out.println((String)session.getAttribute("user"));
	%>
	<%
		String cp = application.getInitParameter("copyright");//ServletContext
		out.println("<hr/>");
		out.println(cp);
	%>
	<%
	//通过使用pageContext获取其他对象
		pageContext.getRequest();
		pageContext.getResponse();
		pageContext.getSession();
		pageContext.getServletContext();
	%>
</body>
</html>

Output page:
insert image description here
Study the corresponding relationship according to the output page.

Four domains of JSP:

There are three major domains in Servlet and four domains in JSP:

ServletContext: the entire application.
session: the entire session (only one user in a session).
request: A request chain.
pageContext: A jsp page, this domain is to share data between the current jsp page and the tags used in the current jsp page.

The validity periods are:

(1) application(ServletContext): Saved on the entire server, available to all users. It doesn't work after restarting the server.
(2) session: Valid in another session. Both server jump and client jump are valid. Closing and reopening the page does not work.
(3) request: Only valid in one request, valid after the server jumps. Client hop is invalid.
(4) page(pageContext): Save attributes only in one page. Invalid after jump.

Get other built-in objects

A pageContext object is equal to all built-in objects, i.e. 1 when 9. This is because the other 8 built-in objects can be obtained using the pageContext object:

(1) JspWriter getOut(): Get the out built-in object;
(2) ServletConfig getServletConfig(): Get the config built-in object;
(3) Object getPage(): Get the page built-in object;
(4) ServletRequest getRequest(): Get the request built-in object;
(5) ServletResponse getResponse(): Get the response built-in object;
(6) HttpSession getSession(): Get session built-in object;
(7) ServletContext getServletContext(): get application built-in object;
(8) Exception getException(): get exception built-in object;

Epilogue

That's it for today, it's short, but important. Remember to practice more after reading it.

Continuing to update…

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/123084478