As duas classes que vou passar tratam cookie, uma cria e a outra visualiza.
A que visualiza dá erro de NullPointerException quando uso loop. Mas o array só possui um elemento, um só cookie.
Quando não uso loop aparece: org.apache.catalina.connector.CoyoteWriter@7adf8b3d
Conferi no navegador se o cookie foi criado realmente.
Classe VisualizaCookie.java com loop
public class VisualizaCookie extends HttpServlet {
public void destroy() {
super.destroy();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Cookie listaCookies[] = request.getCookies();
Cookie nome = request.getCookies();
if (listaCookies != null) {
for (int i = 0; i < listaCookies.length; i++) {
if (listaCookies[i].getName().equals ("nome")) {
nome = listaCookies[i];
break;
}
}
}
String html = "<html><head>"
+ "<title>Visualizando um Cookie</title></head>"
+ "<body>"
+ "<h2>O Cookie chamado <strong>nome</strong>tem o valor: </h2>"
+ "<strong>" + nome[0].getValue() + "</strong>" + "</body></html>";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(out);
out.close();
}
}
A mesma classe sem usar o loop
public class VisualizaCookie extends HttpServlet {
public void destroy() {
super.destroy();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Cookie listaCookies[] = request.getCookies();
Cookie[] nome = request.getCookies();
// if (listaCookies != null) {
// for (int i = 0; i < listaCookies.length; i++) {
// if (listaCookies[i].getName().equals ("nome")) {
// nome = listaCookies[i];
// break;
// }
// }
// } else {
// System.out.println("Cookie vazio");
// }
String html = "<html><head>"
+ "<title>Visualizando um Cookie</title></head>"
+ "<body>"
+ "<h2>O Cookie chamado <strong>nome</strong>tem o valor: </h2>"
+ "<strong>" + nome[0].getValue() + "</strong>" + "</body></html>";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(out);
out.close();
}
}
A classe que cria o cookie
public class CriandoCookie extends HttpServlet{
public void destroy() {
super.destroy();
}
protected void doGet(HttpServletRequest reques, HttpServletResponse response) throws ServletException, IOException{
Cookie meuCookie = new Cookie("Nome", "Joao");
meuCookie.setMaxAge(60);
meuCookie.setSecure(false);
meuCookie.setComment("Criando um cookie e grando meu nome nele.");
response.addCookie(meuCookie);
String html = "<html><head>" +
"<title>Criando um Cookie</title></head>"+
"<body>" +
"<h2>Seu Cookie foi criado com sucesso!</h2>"+
"<a href=\"VisualizaCookie\">Clique aqui para visualizar seu primeiro Cookie</a>"+
"</body></html>";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(html);
}
public void init() throws ServletException{
super.init();
}
}
Erro do console:
java.lang.NullPointerException
at br.com.cap_08.VisualizaCookie.doGet(VisualizaCookie.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)