我已经在我的JSP中初始化了这样的cooke,
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
String loc = "/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/"+timeStamp;
Cookie thecookie = new Cookie("thecookie", loc);
thecookie.setMaxAge(60*60*24);
response.addCookie(thecookie);
因此,创建的所有cookie将具有相同的名称,但对于不同的用户访问时间具有不同的值。
在java servlet的结尾我想停用/杀死在java servlet开头传递的cookie,并说如果他们是并行使用系统的5个用户,我怎么知道哪个cookie要为哪个用户停用在my.java的最后?
例如,考虑User 1 : cookie name `thecookie` value `AAA`
User 2 : cookie name `thecookie` value `BBB`
User 3 : cookie name `thecookie` value `CCC`
User 4 : cookie name `thecookie` value `DDD`
User 5 : cookie name `thecookie` value `EEE`
现在,在my.java servlet的末尾,我想要在my.java的开头分配值CCC而不干扰其他cookie值的cookie,如何执行此操作?
答案 0 :(得分:27)
您使用错误的方法阅读Cookie ..
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cookieName")) {
//do something
//value can be retrieved using #cookie.getValue()
}
}
}
使用它。无需检测特定用户的cookie,只需将其停用即可。
答案 1 :(得分:3)
Cookie[] cookies = request.getCookies();
int i = 0;
for (Cookie cookie : cookies ) {
Sytem.out.println(cookies[i].getName());
Sytem.out.println(cookies[i].getValue());
i++;
}
答案 2 :(得分:3)
准备使用通用方法:
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
return cookie;
}
}
}
return null;
}
答案 3 :(得分:1)
@InjectMocks
答案 4 :(得分:0)
在Kotlin中,它会更短:
=countif("INDIRECT(A8)"&!H:H, "=✅")
答案 5 :(得分:0)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = "";
String password = "";
// Get an array of Cookies associated with this domain
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("username")) {
username = cookies[i].getValue();
}
if (cookies[i].getName().equals("password")) {
password = cookies[i].getValue();
}
}
}
AccountDAO l = new AccountDAO();
Account a = l.getAll(username, password);
if (a != null) {
request.getRequestDispatcher("Detail.jsp").forward(request, response);
} else {
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}