Sao a tutti... sto cercando di realizzare un contatore di accessi usando i cookie, ecco il codice:
package cookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class Prova extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Cookie[] cookies = req.getCookies();
int counter = 1;
Cookie cookie = null;
if (cookies.length > 0) {
int i = 0; boolean found = false;
while (i < cookies.length && !found) {
cookie = cookies[i];
if ((cookie.getName()).equals("PersonalCounter")) {
counter = (new Integer(cookie.getValue())).intValue() + 1;
cookie.setValue(counter+"");
found = true;
}
}
if (!found) {
cookie = new Cookie("PersonalCounter", "1");
}
}
else {
cookie = new Cookie("PersonalCounter", "1");
}
res.addCookie(cookie);
out.println(counter);
}
}
Non mi da nessun errore, il problema e che quando vado a richiamare la servlet si blocca senza stampare nulla. Grazie in anticipo.

Rispondi quotando

