Wu Yuxiong--Natural JAVA development JSP-Servlet study notes: response object-increase Cookie

<%-- 
    Document   : addCookie
    Created on : 2020-4-12, 8:16:06
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        < title > Add Cookie </ title > 
    </ head > 
    < body > 
        <% 
// Get request parameter
             String name = request.getParameter ( " name " );
 // Create a cookie with the requested parameter as the value Object 
            Cookie c =  new Cookie ( " username " , name);
 // Set the lifetime of Cookie object 
            c.setMaxAge ( 24  *  3600 );
 //  Add Cookie object to the client
            response.addCookie (c);
        %>
    </body>
</html>

 

 

<%-- 
    Document   : readCookie
    Created on : 2020-4-12, 8:23:18
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content= "text / html; charset = UTF-8" > 
        < title > Read Cookies </ title > 
    </ head > 
    < body > 
        <% 
// Get all Cookie 
            Cookies reserved by this station on the client [] cookies = request.getCookies ();
 // Traversing each cookie on the client
             for (Cookie c: cookies) {
                 // If the name of the cookie is username, it indicates that the cookie is a cookie that needs to be accessed
                 if (c.getName (). equals ( " username " )) { 
                    out.println (c.getValue ());  
                }
            } 
        %>
    </body>
</html>

 

Guess you like

Origin www.cnblogs.com/tszr/p/12683637.html