1

I want to store id in httpsession when a button is clicked so that I can use it in another servlet.

HttpSession session = request.getSession();
<button>
   <a href = "\"http://localhost:8080/WhatASap/createConversation\""
            + "style=\"text-decoration:none\""
            + "onclick=\"session.setAttribute(\"ID\","+ ID + ")\">
        Chat 
   </a>
</button>

When I am clicking the button ID is not getting set in httpsession.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • The onclick event happens in the browser - there is no HttpSession object. Pass ID to the servlet which handles the href URL. – Andrew S Aug 22 '18 at 16:16

1 Answers1

3

You cannot add variables into HttpSession from javascript. You'll have to do it in the servlet. Anything you write under onclick will be executed by the client browser's javascript engine which has no knowledge about your server.


As in your case, parse any variables to your createConversation servlet.

<a href='http://localhost:8080/WhatASap/createConversation/?ID=<%= ID %>'>

Next, in your servlet;

HttpSession session = request.getSession();
session.setAttribute("ID", request.getParameter("ID"));
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80