package com.davic.working.testing;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.system.server.ServerConfig;
public class CookieCounter extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
boolean cookieFound = false;
Cookie thisCookie = null;
response.setContentType("text/html;charset=gb2312;");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
System.out.println("length:" + cookies.length);
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
thisCookie = cookies[0];
if (thisCookie.getName().equals("CookieCount")) {
cookieFound = true;
break;
}
}
}
if (cookieFound == false) {
thisCookie = new Cookie("CookieCount", "1");
thisCookie.setMaxAge(10);
response.addCookie(thisCookie);
}
if (cookieFound) {
int cookieCount = Integer.parseInt(thisCookie.getValue());
cookieCount++;
thisCookie.setMaxAge(10);
thisCookie.setValue(String.valueOf(cookieCount));
response.addCookie(thisCookie);
out.println("<p>这是您在近" + 10
* (Integer.parseInt(thisCookie.getValue()) - 1) + "秒内登陆"
+ thisCookie.getValue() + "次系统");
} else {
out.println("<p>" + "你10内都没有登陆进系统");
}
}
}