How can i access javascript variables in JSP?

JavaScript variable is on client side, JSP variables is on server side, so you can’t access javascript variables in JSP. But you can store needed data in hidden fields, set its value in client and get it on server over GET or POST.

Client side:

<script type="text/javascript">
var el = document.getElementById("data");
el.value = "Needed_value";
</script>
<form action="./Your_JSP.jsp" method="POST">
<input id="data" type="hidden" value="" />
<input type="submit" />
</form>

server side:

<%
if (request.getParameter("data") != null) { %>
 Your value: <%=request.getParameter("data")%>
<%   
} 
%>

Leave a Comment