This article simply uses cookie technology to simply limit the number of likes, and cannot prevent tourists from malicious likes.
OK, let’s not talk about it anymore, let’s take a look at the basics:
ajax+springMVC+cookie
You can do whatever you want in the middle frame. The author uses springMVC here. As long as you get HttpServletRequest and HttpServletResponse, you can operate cookies.
What is a cookie
A cookie is a variable stored in the visitor's computer. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve the value of a cookie.
The second reading mentioned that cookies are stored in http request, which provides us with the possibility of operating cookies in Java.
Main uses of cookiesEdit
The server can use the arbitrary nature of the information contained in cookies to filter and maintain this information regularly to determine the status in HTTP transmission. The most typical application of cookies is to determine whether the registered user has logged into the website. The user may be prompted to retain user information the next time he enters this website to simplify login procedures. These are the functions of cookies. Another important application is the processing of "shopping cart". Users may select different products on different pages of the same website over a period of time, and this information will be written into cookies to extract information at the time of last payment.
After basically understanding cookies, let’s take a look at how Java operates cookies
Create a lifeless cookie, that is, a cookie that disappears as the browser closes. The code is as follows:
HttpServletRequest request HttpServletResponse responseCookie cookie = new Cookie("cookiename","cookievalue");response.addCookie(cookie);Create a life cycle cookie that can set its life cycle
cookie = new cookie("cookiename","cookievalue");//This method receives an integer in seconds, which 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();//Set the path, this path, that is, the cookie can be accessed under the project. If the path is not set, then only the cookie path and its subpath can be accessed by setting the cookie.setPath("/");response.addCookie(cookie);Read the cookie, read the cookie code as follows
Cookie[] cookies = request.getCookies();// This way you can get an array of cookies for(Cookie cookie: cookies){cookie.getName();// get the cookie namecookie.getValue(); // get the cookie value}springMVC specific restriction operation
/*** Like operation** @param comment* @param request* @param response* @return*/@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 obtained based on the name of the cookie getCookieByName() method can refer to the encapsulated cookie at the link on the code block cookie = getCookieByName(request, comment.getCommentId() + "");// Determine whether the cookie is empty if (cookie != null) {// The cookie is not empty, prompting that it has been liked // The foreground passes the value map.put(TalkingConst.ATTRIBUTE_MSG, "You have liked it, please take a break~");map.put(TalkingConst.ATTRIBUTE_NAME_RESULT, Boolean.FALSE);} else {// Database operation, add commentService.love(comment);// Create cookie.addCookie() method. You can refer to the encapsulated addCookie(response, comment.getCommentId() + "", "", );map.put(TalkingConst.ATTRIBUTE_NAME_RESULT, Boolean.TRUE);}return map;}At this point, the number of likes restricted by simple cookies is basically completed. The front desk code was not placed because the style of each like was different.
The above mentioned sharing with you the relevant knowledge on using cookies to limit the number of likes in Java, and I hope it will be helpful to you.