Java USES cookies to limit thumb up times of of

  • 2020-04-01 04:38:57
  • OfStack

This paper simply USES the Cookie technology to simply limit the thumb up times, and it cannot prevent the malicious thumb up of tourists.

Well, without further ado, let's take a look at the basics:

Ajax + for springMVC, cookies

You are free to use the intermediate framework, springMVC is used here, as long as you get HttpServletRequest and HttpServletResponse you can operate cookies

What is a Cookie

Cookies are variables stored on the visitor's computer. This cookie is sent whenever the same computer requests a page through a browser. You can use JavaScript to create and retrieve cookie values.

As mentioned in the second reading, cookies are stored in HTTP's request, which makes it possible to manipulate cookies in Java

Cookies are mainly used for editing

The server can take advantage of the arbitrariness of the information contained in Cookies to filter and maintain this information regularly to determine the status in HTTP traffic. The most typical application of Cookies is to determine whether a registered user has logged on to a website. The user may be prompted to retain the user's information on the next visit to the website in order to simplify the login process. These are the functions of Cookies. Another important application is "shopping cart" processing. Users may choose different items from different pages on the same website over a period of time, and this information is written to Cookies to extract the information at the end of the payment.

Now that we have a basic understanding of cookies, let's take a look at how Java operates on cookies

Set up an inanimate cookie, which disappears when the browser is closed. The code is as follows:


HttpServletRequest request 
HttpServletResponse response
Cookie cookie = new Cookie("cookiename","cookievalue");
response.addCookie(cookie);

    Creates a cookie that has a life cycle. You can set its life cycle


cookie = new Cookie("cookiename","cookievalue");
//This method receives an integer in seconds that represents the maximum lifetime of the cookie. A negative value indicates that the cookie will be cleared when the browser is closed, indicating that the cookie must be cleared immediately.
cookie.setMaxAge();
//If you do not set the path, only the cookie path and its subpaths can be accessed
cookie.setPath("/");
response.addCookie(cookie);

Read the cookie, read the cookie code as follows


Cookie[] cookies = request.getCookies();//This retrieves an array of cookies
for(Cookie cookie : cookies){
cookie.getName();// get the cookie name
cookie.getValue(); // get the cookie value
}

SpringMVC specifically restricts operations



@RequestMapping(value = TalkingConst.PATH_LOVE, method = RequestMethod.POST)
public @ResponseBody Map<String, Object> love(Comment comment, HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
//The cookie getCookieByName() method based on the name of the cookie can refer to the encapsulation at the link on the code block
Cookie cookie = getCookieByName(request, comment.getCommentId() + "");
//Determines if the cookie is empty
if (cookie != null) {
//The cookie is not empty. The prompt has been liked
//The front desk by value
map.put(TalkingConst.ATTRIBUTE_MSG, " You've already liked it. Please have a rest ~");
map.put(TalkingConst.ATTRIBUTE_NAME_RESULT, Boolean.FALSE);
} else {
//Database operation, thumb up number plus
commentService.love(comment);
//The method to create cookie addCookie() can refer to the encapsulation at the link on the code block
addCookie(response, comment.getCommentId() + "", "", );
map.put(TalkingConst.ATTRIBUTE_NAME_RESULT, Boolean.TRUE);
}
return map;
}

At this point, the simple cookie limit thumb up times is basically complete. The foreground code is not there because each thumb up has a different style.

The above mentioned to share with you the Java Cookie limit thumb up times of relevant knowledge, I hope to help you.


Related articles: