JSP data interaction (2)------cookie

1. What is a cookie (javax.servlet.http)

Answer: 1. The cookie was invented by Netscape and is the most commonly used way to track user sessions;

      2. It is generated by the server and sent to the client browser, and the browser will save it as a file in a certain directory;

      3. A cookie is generally some text information or selection entered by a user when browsing a website. When the user visits the website next time, the server will first check whether there is saved cookie information from the client, and then present it according to the content of the cookie. specific pages to the user;

      4. The most typical cookie application scenario is to save some account numbers and passwords of the user logging in to the website. The user can choose to log in automatically next time to simplify the operation, but there may be security problems in the data;

2. Reflection of the role of cookies

Answer: 1. Tracking of specific objects, such as the number of visits by the visitor, the last visit time, the path, etc.; 

      2. Count the number of page views; 

      3. Record the user's login information within the validity period of the cookie; 

      4. Realize various personalized services, such as displaying different content in different styles for different user preferences; 

3. Using cookies in JSP

Answer: 1. Create cookie object

           Cookie newCookie=new Cookie(String name,String value);

      2. Write a cookie

          response.addCookie(newCookie);

4. Common methods of cookies

Answer: 1. void setMaxAge(int expiry) : Set the validity period of the cookie in seconds; 

      2. void setValue(String value) : After the cookie object is created, assign a new value to the cookie object; 

      3. String getName() : get the name of the cookie;

      4. String getValue() : Get the value of the cookie;

      5. int getMaxAge() : Get the valid time of the cookie, in seconds;

5. Access to cookies

Answer: //First create a setCookie.jsp page

<body>

<%

response.addCookie(new Cookie("userName", "Jack"));

response.addCookie(new Cookie("password", "123456"));

response.sendRedirect("getCookie.jsp");

%>

</body>

      //This is the getCookie.jsp page

<body>

<%

Cookie[] cookies = request.getCookies();

String user = "";

String pwd = "";

if (cookies != null) {

for(int i=0;i<cookies.length;i++){

if(cookies[i].getName().equals("userName")){

user=cookies[i].getValue();

}else if(cookies[i].getName().equals("password")){

pwd=cookies[i].getValue();

}

}

}

%>

Username: <%=user %><br/>

Password: <%=pwd %>

</body>

      pay attention: 

          1. Write the cookie through the addCookie() method of the response object;

          2. Obtain the cookie array through the getCookie method of the request object, and then traverse and output the value of the cookie;

          3. Cookies are saved by name/value, so you must first call the getName() method of the cookie object to check the name of each array member until you find the cookie we need, and then use the getValue() method to get the corresponding name the value of; 

6. Verify that the sessionid is stored on the client side in the form of a cookie

Answer: code in //setCookie

<%

    session.setAttribute("test", "hello");

    response.sendRedirect("getCookie2.jsp");

 %>

      //code in getCookie

Sessionid obtained by getId method:

<%=session.getId()%>

<br />

<%

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (int i = 0; i < cookies.length; i++) {

out.print("Cookie中的sessionid : "+cookies[i].getValue());

}

}

%>

      After comparison, the two are the same, indicating that the sessionid is also stored in the client in the form of a cookie; 

      pay attention: 

      If you do not use response redirection, but use getRequestDispatcher().forward(request,response) to forward, you will not see the JSESSIONID in the cookie, because the redirection is to redirect the page to a new address. Before, the response to the previous request has been made, and the cookie is written on the client side; if the forwarding method is used, the previous request is not responded to before the jump, so the cookie is not written on the client side, this time as long as Enter the URL directly, and jump to the page again, and you can see the cookie written in the last response;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324590809&siteId=291194637