1

Create a servlet that uses cookies to store the number how many times user has visited?-That's the question im trying to solve

this is my html

<html>
    <head>

    </head>
     <body>
        <form action="NewServlet">
           <input type="text" name="text1">
           <input type="submit"/>

        </form>
    </body>
</html>

this is NewServlet.java

public class NewServlet extends HttpServlet {

   public void service(HttpServletRequest req,HttpServletResponse res)throws 
IOException,ServletException
   {
       res.setContentType("text/html");

   Cookie[] cook=req.getCookies();

   String s=req.getParameter("text1");
   int a=0;
   if(cook.length>0)
   {       
   for(Cookie cookie :cook)
   {
       if(cookie.getName().equals(s))
       {
           int b=Integer.parseInt(cookie.getValue());
           b=b+1;
           cookie.setValue(String.valueOf(b));
           a++;

       }
   }
   }

   if(a>0)
   {
       res.sendRedirect("NewServlet1");
   }
   else
   {
       Cookie cok=new Cookie(s,"1");
       res.addCookie(cok);
       req.setAttribute("user", s);
       RequestDispatcher r=req.getRequestDispatcher("NewServlet1");
       r.forward(req,res);
   }       
   }

}

and this is NewServlet1.java

public class NewServlet1 extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    res.setContentType("text/html;charset=UTF-8");

   Cookie[] cookie=req.getCookies();
   String s=String.valueOf(req.getAttribute("user"));
   for(Cookie cok:cookie)
   {
       if(cok.getName().equals(s))
       {
           int a=Integer.parseInt(cok.getValue());
           res.getWriter().println("number of times visited"+a);
       }
   }


}
}

what i am doing is checking if cookie exist or not and if no then creating the cookie on NewServlet.java .But i am getting a Null-Pointer Exception (Error 500) on clicking the submit button

stacktrace

at First.NewServlet.service(NewServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Nicholas K
  • 15,148
  • 7
  • 31
  • 57

1 Answers1

1

Change if(cook.length>0) to if(cook !=null && cook.length > 0). This way if cook is null then it will not check the .length and hence avoid the NPE

Nicholas K
  • 15,148
  • 7
  • 31
  • 57