request session application


The session the Application 2.request
    2.1 of these three objects inside the server can help us save the data and transfer data
                  type (Interface) object name
           the HttpServletRequest request
           the HttpSession the session
           the ServletContext the Application

    2.2 doGet or doPost how to get these three objects in
        the request object
            in doGet doPost method parameters or
        session object
            the HttpSession session Request.getSession = ();
        file application objects
            request.getServletContext ();
            or
            this.getServletContext ();
            or
            request.getSession () getServletContext ().;
            Or
            this.getServletConfig () getServletContext ();.

        Note: Either way, get an application object are the same

    life cycle of three 2.3 scope and object
        request object
            lifecycle:
                each time the client requesting the server will create a new request object, after the end of the visit, the request object is destroyed
            scope:
                using request / store data can only play a role in the request for such a jump when the internal server (because the internal Jump clients send only one request).
            Note: the client redirection does not work, because the client will be redirected to issue multiple requests (more new request object)

        session objects (session)
            to get the session object the three ways:

            // If the request has a corresponding session, this session is returned if there is no corresponding session request, it creates a new session and return to
            the HttpSession session = request.getSession (to true);

            // If there is a corresponding request the session, the session is returned, if no corresponding session request, it returns null
            Session = request.getSession the HttpSession (false);

            // equivalent to request.getSession (to true);
            the HttpSession session = request.getSession ();

            life cycle:
                getSession () method first created session object is called.

                session.invalidate () call will be destroyed when the session

                session if automatic time-out, then, will be destroyed
                session default timeout of 30 minutes
                session timeout can be set in code or set in web.xml
                    code settings: session. setMaxInactiveInterval (5); 5 seconds

                    web.xml provided:
                        one minute timeout, negative or 0 for no time limit
                        <the session-config>  
                            <-the session timeout>. 1 </-the session timeout>  
                        </ session-config>
                session timeout refers to: the client does not interact with the time server

            scope:
                . a work session in a session request can have multiple requests, the data in the session, and can be shared by multiple requests.
            
            Thoughts: after each request object is created, the server know how this request and session object which corresponds?
            
            Note: Note that the server normally closed and closed improperly influence on the session objects (session to be serialized problem)
 when non-normal shutdown tomcat, tomcat session will save to your hard drive, save it to a file after serialization session, when the server starts again deserialize, then the session will be extremely anti-serialization
            
        application objects
            ServletContext is called the application context / servlet context
            lifecycle:
                start tomcat server time, it will be created
                closed tomcat server and they will be destroyed
                and each item during the run, there will be one and only one application objects

            Range: the entire project during operation, only one application object, so this object is shared by all users, everyone can to keep the value of the object which can also be a chance to object because this project is only one.


    2.3 these three objects / store data
        stored-value:
        request.setAttribute ( "Key", value);
        session.setAttribute ( "Key", value);
        application.setAttribute ( "Key", value);
        
        value:
        the request.getAttribute ( "Key");
        session.getAttribute ( "Key");
        application.getAttribute ( "Key");

Guess you like

Origin www.cnblogs.com/yxj808/p/12018621.html