I want to access Javascript variables in JSP code. How can I do that?
simple, you can't! JSP is server side, javascript is client side meaning at the time the javascript is evaluated there is no more 'jsp code'.
Here, To pass data to another JSP page,You can try like this. Now You can use this value in another JSP page like. String test = request. getParameter(test);
You can set it in some hidden fields and then you can send it over the form submision. Something like this: <input type="hidden" value="returnVariable()" name="javaScriptVariable" /> ?
In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.
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")%>
<%
}
%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With