This article simply uses the cookie technology to simply limit the number of points of praise, and can not eliminate the malicious points of tourists praise.
OK, no more long-winded, first look at the basics:
Ajax+springmvc+cookie
The middle frame you are free, the landlord here uses the SPRINGMVC, as long as obtains httpservletrequest and httpservletresponse you can operate the cookie
What is a cookie
A cookie is a variable that is stored in a 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 a cookie's value.
The 2nd reading mentions that cookies are stored in HTTP request, which provides us with the possibility to manipulate cookies in Java
Cookies Primary Purpose Edit
The server can use cookies to contain information about the arbitrary nature to filter and regularly maintain this information to determine the status in the HTTP transmission. The most typical use of cookies is to determine whether registered users have logged on to the site, users may be prompted, whether the next time to enter this site to retain user information to simplify the login procedures, these are the function of cookies. Another important application is the "shopping cart" process. Users may select different items on different pages of the same Web site for a period of time, and the information will be written to cookies to extract information at the final payment.
With a basic understanding of cookies, let's look at how Java operates cookies
Create a lifecycle-free cookie, which disappears as the browser closes, with the following code:
HttpServletRequest Request
httpservletresponse response
cookie = new Cookie ("CookieName", "Cookievalue ");
Response.addcookie (cookie);
Create a life-cycle cookie that can set his 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 means that when the browser is closed, the cookie is cleared, indicating that the cookie must be purged immediately.
cookie.setmaxage ();
Sets the path, which means that 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
cookie.setpath ("/");
Response.addcookie (cookie);
Read the cookie, read the cookie code as follows
cookie[] cookies = request.getcookies ();//So you can get a cookie array for
(cookie cookie:cookies) {
cookie.getname () ;/Get the cookie name
cookie.getvalue ();//Get the cookie value
}
SPRINGMVC Specific restriction actions
/** * Operation * @param comment * @param request * @param response * @return * * @RequestMa Pping (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&L T
String, object> (); The cookie Getcookiebyname () method based on the name of the cookie can refer to the package cookie cookie = getcookiebyname on the link at the code block (request,
Comment.getcommentid () + ""); To determine if the cookie is empty if (cookie!= null) {//cookie is not NULL, the hint has been praised//foreground value map.put (talkingconst.attribute_msg, "You have praised, please rest ~");
Put (Talkingconst.attribute_name_result, boolean.false); else {//database operations, point praise Number plus commentservice.love (comment);//Create Cookie Addcookie () method can refer to the encapsulation Addcookie (response) at the link on the code block.
Comment.getcommentid () + "", "",);
Map.put (Talkingconst.attribute_name_result, boolean.true);
} return map; }
At this point, the simple cookie limit of the number of points to praise is basically complete. The code at the front desk doesn't put it because every bit of praise is very different.
The above mentioned to share the Java with cookies to limit the number of points of praise of the relevant knowledge, hope to help.