JSP nine large built-in objects:
Out object: It is an instance of the JspWriter class and is an object commonly used to output content to the client.
Request object: It represents the client's requests for information that is encapsulated in the request object. It is also an instance of the HttpServletRequest class and has a request domain, which is always valid until the client's request is completed.
Common methods:
Request.setcharacterencoding ("UTF-8");//Set the code to solve the Chinese garbled problem.
Request.setattribute ("", "");//The former is the property that needs to be set, the latter is the desired value.
Request.getattribute ();
Request.getrequestdispatcher ("index.jsp"). Forward (request, response);//requests are forwarded to the Index.jsp page, and the request object is always valid.
Response object: It contains information about the response to a customer request and is also an instance of the HttpServletResponse class. Like request, it has the scope of the page, that is, when accessing a page, the page's response object is only valid for this visit, and the execution is not valid.
Common methods:
Response.setcontenttype ("text/html", "charset=" Utf-8 ");//sets the Mimi type of the response.
Response.sendredirect ("index.jsp");//Request redirection, if there are multiple redirects, then the object data is not saved, that is, null.
Differential analysis of request forwarding and request redirection:
-
Request redirection: Client behavior, Response.sendredirect (), is essentially equivalent to two requests, the previous request object is not saved, and the address bar's URL address changes.
-
Request Forwarding: Server behavior, Request.getrequestdispatcher (). Forward (req, resp); is a request, the request object will be saved after forwarding, the Address bar URL address will not change.
Session object: When we first visit the page, the session object is automatically loaded and created. It is an instance object of the HttpSession class.
What is a session? Why do you use a Session object?
-
Session: When a client opens a browser to connect to the server to start, the client closes the browser server to end this process, which we call a session.
-
In the actual situation, the customer in the process of accessing the server, will not be able to switch between several pages of the server, when it is necessary for the service side to know that the current operation of the customer is not operating the previous pages of the customer, that is, it must be clear whether the same person in action. So the concept of Session object is introduced.
Application object: It realizes the sharing of data between users, can hold global variables, it is an instance of the ServletContext class. Its lifecycle starts at the start of the server and terminates at the server's shutdown.
Note:
① the same property of the Application object can be manipulated in the connection between the user's back-and-forth connection or a different user.
② the operation of application object properties anywhere will affect the access of other users to this.
Instructions and actions for JSP:
① Three compile Directives: page, include, Taglib.
② Seven action commands: Jsp:forward, Jsp:param, Jsp:include, Jsp:plugin, Jsp:usebean, Jsp:setproperty, Jsp:getpproperty.
JSP directives:
The include directive differs from the Include Action directive:
The life cycle of the session
The "activity" idea of the session life cycle:
① a new page that is opened by a hyperlink in a session belongs to the same session.
② only the current session page does not close all, reopen the new browser window to access the same project resource when it belongs to the same session.
③ unless all pages of this session are closed and then re-accessed by a JSP or the servlet will create a new session.
Note:
Note that the original session is still there, but the old SessionID still exists on the service side, but no more clients will carry it and then hand it over to the server check.
Note:
① when the Web container is started, the filter is loaded and started.
② instantiation is only instantiated once, meaning that it is used only once in the Web container.
Ideas:
① first determines if the servlet instance exists and jumps into step ③ if it exists.
② initialization phase, the Init initialization method is done after the instance is created.
The ③ response phase, in response to client requests, invokes the service () method. The service () method selectively executes the doget () method or the Dopost () method based on the submission method.
④ Terminate phase, call the Destroy () method to destroy, the server shuts down.
Web Listener
A Web listener is a special class that has a servlet specification definition that listens to client requests and service-side operations, while also listening, including objects such as ServletContext, HttpSession, ServletRequest, and so on.
Classification of listeners
Event Listener for HttpSession
Event Listener for ServletContext
Event Listener for ServletRequest
Note:
These three listening events, respectively, implement the corresponding interface, that is, the diagram of 1. N. Now that the interface is implemented, it is necessary to implement its corresponding method. The main use is also.
Event listeners bound to the state of an object in a httpsession domain
Note:
Why do you use the session passivation?
First of all, under normal circumstances, our session is stored in the server session, but if we have a lot of users, the session is still stored in the server, then the server overhead is very large, will directly affect the use of our web application. Therefore, it is necessary to use the session passivation mechanism, which is the essence of the server is not used by the session object is temporarily serialized into the system files, when it is to be used and then deserialized into the server memory.
Model1 and Model2
Note:
Model1 is one of the earliest development patterns that can embody the layered thought, and simply, Model1 is a kind of jsp+javabean development mode.
Cons: maintainability and scalability are relatively poor.
Ideas:
① first submits a request to the servlet by a JSP page.
② then instantiates the object of a model layer in our control layer servlet or invokes some of the functionality of the model layer.
③ is then accessed by the model layer to read our database layer.
④ when the results are read, the results are returned to our control layer.
After the ⑤ control layer has obtained this result, the user will be presented with different JSP pages based on this result.
Finish
Java Interview Basics section II