1

I have some problems using cookies and sessions in servlets and JSP pages. Can you please show an example about how both of them can be used? I need to create a session or a cookie and use them later, in other pages, but I don't know what methods should be called and in which order. I tried in many ways but I keep getting exceptions. My codes look like...

Cookie score = new Cookie("score", "0");
response.addCookie(score);

HttpSession session = request.getSession();
session.setAttribute("score", new Integer(0));

And in JSP file I try to retrieve them in the following way:

<%>Cookie[] c = request.getCookies();
   for(Cookie i : c)
      if(i.getName().equals("score"))
        System.out.println(i.getValue()); <%>

for cookies and...

<%> System.out.println(request.getAttribute("score")); <%>

for session. But both are null.

Sargsyan Grigor
  • 533
  • 1
  • 5
  • 22
  • Please see how to create a [minimal, complete, and verifiable](http://stackoverflow.com/help/mcve) example. It will be more helpful to you (and future users) if you show us what you currently have and where specifically you are having trouble (ie: any errors, exceptions). Questions asking for examples without any code are general considered off topic. – Lexi May 02 '16 at 15:31
  • here is some code from my project related to the issue – Sargsyan Grigor May 02 '16 at 15:36

0 Answers0