I think the best way to start understanding jsps is that they are secretly being interpreted into java classes. But more specifically: They are being put into a function that is writing out the html line by line, after evaluating each line. Hence why you can have the <% %> tags. Now, because of that, the <% %> tags are actually lines that are evaluated but not written to the html. The evaluations are stored in the scope of the resulting java method. So what ends up happening looks something like:
public void SendHtmlPage() {
int x=20;
int y=30;
Print("<script>");
Print("var p=\"${x}\"\;"); //This gets evaluated to getSession.getAttribute("x")
//Since there is no x in the session, it returns null
Print("var q=\"${y}\"\;"); //Same here
Print("</script>);
}
So. That is why x and y aren't known inside your scriptlet. However, there are ways to save the method scoped variables to session scope. Check this out: I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error
Good luck and happy coding. :)