Write a Servlet Application that uses a cookie to track the number of times a user visits a web site
2017-05-02 12:46
501 查看
编写一个用cookie来跟踪用户访问网站次数的servlet
javaee学习过程中cookie的使用
Cookie cookies[] = request.getCookies();
Cookie myCookie = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("count")) {
myCookie = cookies[i];
break;
}
}
}
if (myCookie != null) {
int count = 0;
count = Integer.parseInt(myCookie.getValue());
count++;
myCookie.setValue(String.valueOf(count));
myCookie.setMaxAge(60 * 60 * 24 * 30);//设置最大保存时间
response.addCookie(myCookie);
out.print("您这是第" + myCookie.getValue() + "次访问");
}else{
Cookie cookie = new Cookie("count", "1");
cookie.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(cookie);
out.print("您这是第1次访问");
}
javaee学习过程中cookie的使用
Cookie cookies[] = request.getCookies();
Cookie myCookie = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("count")) {
myCookie = cookies[i];
break;
}
}
}
if (myCookie != null) {
int count = 0;
count = Integer.parseInt(myCookie.getValue());
count++;
myCookie.setValue(String.valueOf(count));
myCookie.setMaxAge(60 * 60 * 24 * 30);//设置最大保存时间
response.addCookie(myCookie);
out.print("您这是第" + myCookie.getValue() + "次访问");
}else{
Cookie cookie = new Cookie("count", "1");
cookie.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(cookie);
out.print("您这是第1次访问");
}
相关文章推荐
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- CKFinder 2.0.2 去除 This is the demo version of CKFinder. Click here to visit our web site
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- Create a servlet that sends a zip file to the user
- Write a method to count the number of 2s between 0 and n
- What technical details should a programmer of a web application consider before making the site public?
- 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- 内存错误:CRT detected that the application wrote to memory after end of heap buffer
- unable to start debugging on the web server iis does not list an application that matches the launched url
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- tally the number of users logged onto the asp.net web site
- Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path 解决(VS2010+IIS7.5)
- 内存错误:CRT detected that the application wrote to memory after end of heap buffer
- CRT detected that the application wrote to memory after end of heap buffer
- CRT detected that the application wrote to memory after end of heap buffer 问题(转)
- Write a method that counts the number of occurrences of a given character in a string
- CRT detected that the application wrote to memory after end of heap buffer