Q:

How to use session.setAttribute("id", id) onclick a button in java servlet?

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.

A:

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"));

