1
function redirect() {
    var category = document.getElementById("category").value;
    <% session.setAttribute("count", category); %>
}

This code gives an error in the setAttribute section. (category cannot be resolved to a variable) How can I fix it?

Volkan Şahin
  • 1,181
  • 2
  • 10
  • 15
  • 2
    Possible duplicate of [How to access Session variables and set them in javascript?](http://stackoverflow.com/questions/15519454/how-to-access-session-variables-and-set-them-in-javascript) – RamPrakash Feb 24 '17 at 12:15
  • The key is to realize, **where** and **when** which code is executed - JSP on the server when the page is requested and rendered (i.e. **before** the response is sent to the browser) and Javascript in the browser, **after** the browser receives the **already generated** response. **TL;DR:** To set any server-side value (and session is a server-side object) from Javascript, use [AJAX](https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax). – Jozef Chocholacek Feb 24 '17 at 13:11

0 Answers0