One, the servlet life cycle is divided into three stages:
1. Initialize phase call init () method
2. Call the Service () method in response to customer request phase
3. Call the Destroy () method in the termination phase
After the servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization.
Theinit () method is called only once throughout the servlet's life cycle.
Second, the working principle:
The servlet receives and responds to the client request process, first the customer sends a request, the servlet responds to the request by invoking the service () method, matches the request in the service () method, chooses to call Doget,dopost, and so on. And then into the corresponding method to invoke the logical layer of the method to achieve response to the customer.
There are no doget,dopost and so on in the Servlet interface and Genericservlet, these methods are defined in HttpServlet, but all of them return error information, so each time we define a servlet, Must implement these methods such as Doget or Dopost.
Third, the small knowledge:
1. Succession relationship: Genericservlet->httpservlet->servlet
HttpServlet is a class that is specific to the HTTP protocol
2. The difference between get mode and post mode:
Get mode to explicitly submit the form, you can see the parameters in the URL (address bar) We pass
The Post method is an implicit pass-through value and is not visible. Post mode is relatively safe
3. The difference between getattribute () and Getparamter ():
GetAttribute is the return object, GetParameter returns the string
Overall: the Request.getattribute () method returns the objects that exist within the request scope, and the Request.getparameter () method is to get the data submitted by HTTP.
4. The difference between a cookie and a session:
Cookie: Take a scenario that keeps the state on the client, not very secure
Session: Take a scenario that maintains state on the server side, which is safer than cookie
general information such as landing information stored as a session, other information if necessary to keep can be placed in a cookie
5, set the session expiration time two ways:
(1) Write the following two sentences on the main page:
HttpSession session=request.getsession (true); Session.setmaxinactiveinterval (3600); // 3,600 seconds, note the server side for 3,600 seconds, not the client's
(2) Set in the project's Web. XML:
< Session-config > < Session-timeout ></session-timeout> Here 60 is 60 minutes. </session-config>
6, Sesson two ways to achieve:
By a cookie or URL rewrite
7.
1.session.setattribute () and Session.getattribute () are used in pairs, scoped to the entire session, and used when all the pages are using the data.
2.request.setattribute () and Request.getattribute () are paired, with the scope being between the request and the requested page. Request.setattribute () is used only when the next forward of the action needs to be used; Request.getattribute () represents the property that is set from the request scope. The property must be set setattribute before it can be obtained by getattribute, and the object type is set and obtained. In fact, the name and value of object in the form control are stored in a hash table, so the name of object is given here to the hash table to find the value corresponding to it. The arguments for SetAttribute () are string and object.
3.request.getparameter () is the parameter that receives the parameter, and the parameter is the page submission. Include: Parameters for form submission, URL rewriting (that is, ID in xxx?id=1), and so on, so this method does not have a parameter set (no Setparameter ()), and the receive parameter returns not an object, but a string type.
Servlet life cycle and how it works