Session -Cookie Java technology and the basis of Session

  At this point, learning Servlet three domain objects: ServletContext (web project), request (a request), (a client) Session! It is the same way!

  From the user begins to open the browser to operate, it began a session until the browser is closed until the end. In this process, each time a request is generated will produce a cookie, and it will produce a JESSIONID into the client creates a memory space, the next call directly to find this unique ID value. Data storage has advantages and disadvantages in both places! It should be noted that the browser is closed, session and did not destroy!

First, the session Technical Overview:

1, storing client state:

  Because Http protocol is stateless, which means that each client to access server-side resources, the server does not know who the client is, so it is necessary to identify the client's technical session state.

Session server technology to help remember the client state (distinguished clients)

2, Technical Session:

①, visit a site from opening a browser, to close the entire browser process to become a session. This technical session is to record the client's session state data.

②, technical sessions divided Cookie and Session:

Cookie: data is stored locally on the client, reducing the pressure of the storage server, security is not good, the client can clearly cookie.

Session: storing data to the server, security is relatively good, increasing the pressure on the server.

Two, Cookie technology:

1, the client sends a server cookies (can not store Chinese)

Cookie cookie=new Cookie(String name,String value)

2, set the time persistent cookie on the client:

cookie.setMaxAge(int seconds)

If you do not set the persistence time, the browser cookie will be stored in the memory, the browser is closed when cookie information is destroyed. Set up, then, will the stipulated time in memory.

3, set the cookie carrying path:

cookie.setPath(String path)

If you do not carry the path set path, cookie information generated in the cookie to access web resources where carries cookie information.

4, a cookie is sent to the client

response.addCookie(Cookie cookie)

5, delete the client's cookie

Use of the same name with the path of persistence time of the cookie covered 0

 

void the doGet public (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
        // create a Cookie object
        Cookie Cookie Cookie new new = ( "godds", "naiping");
        // Set the persistence time (from start request Servlet)
        cookie.setMaxAge (60 * 2);
        // set the cookie carrying path (only access this path before carrying)
        //cookie.setPath("/WEB07/SendCookieServlet ");
        /*cookie.setPath("/WEB07");
        cookie.setPath ( "/"); * /
        // send
        response.addCookie (Cookie);
    }

 

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //删除Cookie
        Cookie cookie = new Cookie("goods","naiping");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

 

6, the server receives the client carries cookie ways:

In the first mode request sent to the server

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取cookie数组
        Cookie[] cookies = request.getCookies();
        //遍历cookie数组通过cookie名获取cookie值
        for(Cookie c:cookies){
            if(c.getName().equals("goods")){
                System.out.println(c.getValue());
            }
        }

 

 

void the doGet public (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
        // get the current time of the object
        a Date = new new DATE a Date ();
        // creation date converting class object
        SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy -MM-dd HH SS--mm ");
        // the date the object into a string
        string = sdf.format time (dATE);
        // create a cookie object, record the current access time
        cookie cookie = new new cookie (" lastTime ", time);
        cookie .setMaxAge (60 * 10);
        // send Cookie
        response.addCookie (Cookie);
        // receiving Cookie
        cookies [] = cookies request.getCookies ();
        // define variables storage time of last access
        String lastTime = null;
        // traversal
        for (C cookies: Cookies) {
            IF (. c.getName () the equals ( "lastTime")) {
                lastTime c.getValue = ();
            }
        }
        // solution in response distortion
        response.setContentType ( "text / html; charset = 8-UTF ");
        IF (lastTime == null) {
            // first visit
            response.getWriter () write (." You are the first visit ");
        } the else {
            response.getWriter () write (." your last visit time "+ lastTime);
        }
    }

 

 

 

Three, Session technical :( a domain object)

1 Introduction:

  Session technology is server side technology, will have to create a piece of memory space to store customer data for each client-side data storage, but each time the client needs to carry an identification ID to the server to find their own memory space. So realization is based on the Session Cookie, Session requires unique identification by means of JSESSIONID Cookie storage customers.

2, to obtain the Session object:

HttpSession session = request.getSession()

  This method will get the Session object specific to the current session (the current client), if there is no server-side session of the Session object creates a new Session to return, if you already have the session Session belonging directly to the existing Session returns (in essence, according to JSESSIONID determine whether the client is a session already exists on the server)

3, access to the data field object;

4, life cycle:

①, create:

    Creating the first execution request.getSession ()

②, destruction:

    When the normally closed non-server;

    Session expired, invalid (default 30 minutes)

    Manual destroyed Session

③, Range:

  The default in one session, that is to say, a public session object to any resource in one session.

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取Session域中的内容
        HttpSession session = request.getSession();
        //获取Session中的内容
        String goods = (String)session.getAttribute("goods");
        //解决乱码
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(goods);
    }

 

 

void the doGet public (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
        // Get Session object
        the HttpSession Request.getSession the session = ();
        // Session data to the storage domain (Cookie can not store characters)
        session.setAttribute ( "Goods" "bottle");
        // get JESSIONID
        String the above mentioned id = session.getId ();
        // set the persistence time --- create a new cookie set its time and overwrite the original
        // create cookie
        cookie cookie = new new cookie ( "the JSESSIONID", ID);
        cookie.setPath ( "/ WEB08");
        cookie.setMaxAge (60 *. 3);
        // send Cookie
        response.addCookie (Cookie);
        response.getWriter () Write (. "the JSESSIONID:" + id);
        // result JSESSIONID: 0CDD85B8A6E216CA5BDA8E9729D5F230
       
    }

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159678.htm