You cannot do that since javascript executes on client & JSP executes on server side.
If you want to set javascript variable to JSP session, then you pass this variable through the URL like this
var number = 7;
window.location="http://example.com/index.jsp?param="+number;
Now receive this var in your JSP page like this
String var = request.getParameter("param");
Now set it in session
session.setAttribute("test", var);
EDIT :
var number = 7;
<%session.setAttribute("test", number);%>
In the above code, server will only execute the code inside <% %>. It does not know anything outside of the JSP tags. So, it will also dont know about your javascript variable number
.
Server executes the code & the result will be sent to the browser, then your browser will execute that javascript code var number=7;
.
Hope, now it is clear for you.